如何使用可滚动的两个列表视图来包装其内容,使Linearlayout可滚动

时间:2015-04-23 08:12:40

标签: android android-layout android-listview android-linearlayout

我知道这已被问过多次,但我还没有找到我真正喜欢的解决方案。有人可以建议我做我想做的最优雅的方式:

我目前在LinearLayout(垂直)中有两个Listview。我希望Listviews在高度时包装它们的内容,因此它们永远不需要在内部滚动。相反,我想在页面上向下滚动以查看溢出的任何内容。

提前感谢您的帮助!如果你建议完全重新构建架构,那么我也会对此持开放态度。我唯一要问的是解决方案尽可能简洁明了。 Hackish变通办法不是首选。 :)

3 个答案:

答案 0 :(得分:1)

据我所知,您正试图将ListView作为其内容所需的高度。

您可以创建一个扩展ListView的自定义列表视图,并覆盖其onMeasure方法,如下所示:

public class UnscrollableListView extends ListView {

    public UnscrollableListView(Context context) {
        super(context);
    }

    public UnscrollableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public UnscrollableListView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int maxHeightSpec = MeasureSpec.makeMeasureSpec( 
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
        super.onMeasure(widthMeasureSpec, maxHeightSpec); 
    }

}

这将使您的列表视图包含其内容。

当您需要向下滚动时,请尝试在布局中添加ScrollView以包裹LinearLayout。 最后,您的布局类似于:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <com.example.UnscrollableListView
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <com.example.UnscrollableListView
            android:id="@+id/listview2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</ScrollView>

这是你想要的吗?

但我不得不说这些代码使listview的执行就像一个带有很多相似孩子的垂直线性布局。 它无法利用回收视图来改善布局性能,因为没有视图可以回收。

您可以查看这篇文章: Performance Tips for Android’s ListView

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </ListView>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

答案 2 :(得分:0)

这样做,

listview_main.xml

    ?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ScrollView
            android:id="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <LinearLayout
                    android:id="@+id/list_view1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                </LinearLayout>

 <View
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:background="@android:color/black" />

                <LinearLayout
                    android:id="@+id/list_view2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                </LinearLayout>
            </LinearLayout>
        </ScrollView>

</LinearLayout>

Adapter1.java(Adapter2与Adapter1相同)

public class Adapter1 extends BaseAdapter {

    private Activity activity;
    private LayoutInflater mInflater;
    private ArrayList<String> arraylist1 = new ArrayList<String>();

    public Adapter1(Activity act, ArrayList<String> array) {

        this.activity = act;
        this.arraylist1 = array;
        mInflater = LayoutInflater.from(activity);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arraylist1.size();
    }

    @Override
    public Object getItem(int position) {
        return arraylist1.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return arraylist1.get(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item1, null);

            holder = new ViewHolder();

            holder.lablel = (TextView) convertView.findViewById(R.id.label);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.lablel.setText(arraylist1.get(position).toString());

        return convertView;

    }

    private class ViewHolder {
        TextView lablel = null;
    }

}

MainActivity.java

public class MainActivity extends Activity {
    private ArrayList<String> arraylist1 = new ArrayList<String>();
    private ArrayList<String> arraylist2 = new ArrayList<String>();

    private Adapter1 adapter1 = null;
    private Adapter2 adapter2 = null;

    private LinearLayout listview2;
    private LinearLayout listview1;
    private ScrollView scrollView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_main);

        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");

        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");

        listview1 = (LinearLayout) findViewById(R.id.list_view1);
        listview2 = (LinearLayout) findViewById(R.id.list_view2);
        scrollView = (ScrollView) findViewById(R.id.scrollview);

        adapter1 = new Adapter1(MainActivity.this, arraylist1);
        adapter2 = new Adapter2(MainActivity.this, arraylist2);

        listview1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                scrollView.requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

        listview1.removeAllViews();
        if (adapter1 != null && adapter1.getCount() > 0) {
            for (int i = 0; i < adapter1.getCount(); i++) {

                final View convertView = adapter1.getView(i, null, null);
                if (listview1 != null)
                    listview1.addView(convertView);
            }
        }

        listview2.removeAllViews();
        if (adapter2 != null && adapter2.getCount() > 0) {
            for (int i = 0; i < adapter2.getCount(); i++) {

                final View convertView = adapter2.getView(i, null, null);
                if (listview2 != null)
                    listview2.addView(convertView);
            }
        }

    }

}