我有一个布局。此布局包含3个列表视图,其中列表视图中wrap_content数据的高度不固定。当Listview有一个liitel巨大的数据时,数据在下面,数据无法看到,所以我想滚动所有三个Listview的视图,这是如何可能的。
有人知道这是怎么回事吗?
这是我的视图,它包含3个Listview,现在它的数据较少,但当数据变得巨大时,最后一个Listview就会出现问题。我想滚动灰色视图..。

发布于 2011-10-20 13:49:15
在xml文件中使用线性布局而不是listview。这是您的xml文件。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/scr" android:layout_height="fill_parent"
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/r2" android:orientation="vertical"
android:layout_height="fill_parent" android:paddingTop="100dip"
android:paddingBottom="100dip">
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/l1" android:orientation="vertical"
android:layout_height="wrap_content" android:layout_marginTop="10dip"
android:background="#00B2EE">
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/l2" android:orientation="vertical"
android:layout_height="wrap_content" android:layout_marginTop="10dip"
android:background="#00EE76">
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/l3" android:orientation="vertical"
android:layout_height="wrap_content" android:layout_marginTop="10dip"
android:background="#7171C6">
</LinearLayout>
</LinearLayout>
</ScrollView>并放入另一个xml,该xml将由list膨胀。
这是您的主activity类。
package com.list.add;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class NewlistActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout l1,l2,l3;
ScrollView scrollView;
ViewGroup contentView;
List<String> list = new ArrayList<String>();
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1 = (LinearLayout) findViewById(R.id.l1);
l2 = (LinearLayout) findViewById(R.id.l2);
l3 = (LinearLayout) findViewById(R.id.l3);
scrollView = (ScrollView) findViewById(R.id.scr);
contentView = (ViewGroup) findViewById(R.id.r2);
scrollView.setOnTouchListener(new ScrollPager(scrollView, contentView));
scrollView.post(new Runnable() {
public void run() {
scrollView.scrollTo(0, contentView.getPaddingTop());
}
});
list.add("Parth");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Parth");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Shah");
list.add("Parth");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Parth");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Shah");
list1.add("Parth");
list1.add("Parth");
list1.add("Parth");
list1.add("Parth");
list1.add("Parth");
list1.add("Parth");
list2.add("Kalpesh");
list2.add("Kalpesh");
list2.add("Kalpesh");
list2.add("Kalpesh");
list2.add("Kalpesh");
list2.add("Kalpesh");
list2.add("Kalpesh");
System.out.println(list);
System.out.println(list1);
System.out.println(list2);
for (int i=0; i<list.size(); i++)
{
LayoutInflater inflater = getLayoutInflater();
View vi = inflater.inflate(R.layout.raw, null);
TextView tv = (TextView) vi.findViewById(R.id.textView1);
tv.setText(list.get(i));
l1.addView(vi);
}
for (int i=0; i<list1.size(); i++)
{
LayoutInflater inflater = getLayoutInflater();
View vi = inflater.inflate(R.layout.raw, null);
TextView tv = (TextView) vi.findViewById(R.id.textView1);
tv.setText(list1.get(i));
l2.addView(vi);
}
for (int i=0; i<list2.size(); i++)
{
LayoutInflater inflater = getLayoutInflater();
View vi = inflater.inflate(R.layout.raw, null);
TextView tv = (TextView) vi.findViewById(R.id.textView1);
tv.setText(list2.get(i));
l3.addView(vi);
}
}
}并像这样创建一个scroller类:
public class ScrollPager implements OnTouchListener
public ScrollPager(ScrollView aScrollView, ViewGroup aContentView)
{
mScrollView = aScrollView;
mContentView = aContentView;
scroller = new Scroller(mScrollView.getContext(), new OvershootInterpolator());
task = new Runnable()
{
public void run()
{
scroller.computeScrollOffset();
mScrollView.scrollTo(0, scroller.getCurrY());
if (!scroller.isFinished())
{
mScrollView.post(this);
}
}
};
}
public boolean onTouch(View v, MotionEvent event)
{
// Stop scrolling calculation.
scroller.forceFinished(true);
// Stop scrolling animation.
mScrollView.removeCallbacks(task);
if (event.getAction() == MotionEvent.ACTION_UP)
{发布于 2011-10-05 15:28:53
您应该在XML布局中的每个ListView中使用权重属性android:layout_weight="1"。因此,它将为每个列表视图划分相同的屏幕空间,并且您的滚动将为每个ListView工作。
发布于 2011-10-14 18:58:05
如果在垂直或水平堆栈中呈现内容,则应该使用LinearLayout,然后使用layout_weight属性来控制容器中各个行/列的比例。
如果您想要屏幕大小,请将layout_width和layout_height设置为fill_parent,否则将无法获得所有可用的屏幕尺寸。如果你尝试使用wrap_content作为高度,除非你求助于额外的布局约束,例如minHeight,否则一切都会崩溃。
我们在任何地方都使用它,而且它非常可靠。对于三个项目,您可以使用1/1/1或3/3/3。
权重也不必相等!您可以按您希望的方式划分比例;权重只是整个跨度(宽度/高度)的相对比率。例如,如果你想要中间元素大小的两倍,使用1/2/1;如果你想要它的40%,使用30/40/30或3/4/3。
一个很好的“技巧”是在一行/列上使用layout_weight=1 (其他默认设置为0),它将“填充”任何剩余的空格。这是一种常见的布局方案。
如果您需要堆栈是可滚动的,您可以将其放入ScrollView中。在这种情况下,您必须将LinearLayout的layout_height设置为wrap_content,并且您将根据布局系统的突发奇想进行主题折叠(这意味着您需要使用最小/最大约束)。
https://stackoverflow.com/questions/7657961
复制相似问题