3个垂直列表在一个scrollview中查看

时间:2014-11-13 15:05:10

标签: android listview android-listview android-scrollview

我需要实现这样一个屏幕:

enter image description here

所以,我用ImageView,2 TextViews和CheckBox创建了适配器。

我需要实现3个listViews并使屏幕可滚动。

我试图实现solution,但这对我来说是行不通的 - 所以我这样做了:

  <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:fillViewport="true"
        android:orientation = "vertical" android:layout_height="match_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:orientation="vertical"
        android:weightSum="1.0"
        >
    <LinearLayout android:layout_weight="0.5"
                  android:orientation="vertical"
                  android:layout_height="fill_parent"
                  android:layout_width="fill_parent">
        <TextView
                android:text="@string/textview_settings_categories"
                style="@style/settings_label"/>
        <ListView   android:id="@+id/listView1"
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent">

        </ListView>
    </LinearLayout>

    <LinearLayout android:layout_weight="0.25"
                  android:orientation="vertical"
                  android:layout_height="fill_parent"
                  android:layout_width="fill_parent">
        <TextView
                android:text="@string/textview_settings_categories"
                style="@style/settings_label"/>
    <ListView   android:id="@+id/listView2"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">

    </ListView>
    </LinearLayout>
    <LinearLayout android:layout_weight="0.25"
                  android:orientation="vertical"
                  android:layout_height="fill_parent"
                  android:layout_width="fill_parent">
        <TextView
                android:text="@string/textview_settings_categories"
                style="@style/settings_label"/>
        <ListView   android:id="@+id/listView3"
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent">

        </ListView>
    </LinearLayout>
</LinearLayout>
</ScrollView>

我也试过this一个解决方案,但两个都不适用于我 - ScrollView无法滚动。

我还试图在另一个上面实现3个垂直ListFragments,listviews可以在里面滚动,但是scrollview不是 - 所以我看不到屏幕的底部。

2 个答案:

答案 0 :(得分:1)

这不是最佳做法,但如果你真的想要这个功能,你可以在触摸它时禁止父滚动视图的触摸事件,并在离开后重新允许它儿童容器。

这就是我以前做过的事情,其中​​&#34; listScanners&#34;是我的列表视图:

listScanners.setOnTouchListener(new View.OnTouchListener()
{
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
          v.getParent().requestDisallowInterceptTouchEvent(true);
          return false;
        }
    });

这是我的布局中与问题相关的部分:

 /* Theres more before this ... */
 <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbarSize="0dp" >

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

                <RelativeLayout
                        android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_marginTop="5dp"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:background="@null"
                        >
                    <TextView
                            android:id="@+id/emptylist1"
                            android:layout_width="450dp"
                            android:layout_height="80dp"
                            android:textColor="#6f6f6f"
                            android:textSize="20sp"
                            android:padding="3dp"
                            android:layout_marginBottom="3dp"
                            android:layout_marginLeft="10dp"
                            android:layout_marginRight="10dp"
                            android:text="No Scanners have been added..."/>
                    <ListView
                            android:id="@+id/listview_scanners"
                            android:layout_width="450dp"
                            android:layout_height="80dp"
                            android:padding="3dp"
                            android:layout_marginBottom="10dp"
                            android:layout_marginLeft="10dp"
                            android:layout_marginRight="10dp"
                            android:cacheColorHint="#00000000"
                            android:divider="#DEDEDE"
                            android:dividerHeight="1px">
                    </ListView>
                </RelativeLayout>
    /* Theres more after this ... */

正确方式:

理想情况下,你应该更喜欢这样的东西,你的布局如下所示:

在线性布局中使用它非常重要:

android:isScrollContainer="true"

isScrollContainer意味着linearlayout包含一个滚动的视图,这意味着它可以是线性布局中的特定大小,但是,当您滚动它时它可能包含更多。这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#afafaf"
          android:isScrollContainer="true">
 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:background="#FF63FF9B"
        android:layout_height="50dp"
        >

    <TextView
            android:id="@+id/textview_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="10dp"
            android:textColor="#ffffff"
            android:text="View First List"/>
</LinearLayout>

<RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_marginTop="5dp"
        android:background="@null"
        >
    <ListView
            android:id="@+id/listview1"
            android:layout_width="fill_parent"
            android:layout_height="130dp"
            android:cacheColorHint="#00000000"
            android:divider="#DEDEDE"
            android:dividerHeight="1px">
    </ListView>
</RelativeLayout>
<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:background="#FF63FF9B"
        android:layout_height="50dp"
        >

    <TextView
            android:id="@+id/textview_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="10dp"
            android:textColor="#ffffff"
            android:text="View Second List"/>
</LinearLayout>

<RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_marginTop="5dp"
        android:background="@null"
        >
    <ListView
            android:id="@+id/listview2"
            android:layout_width="fill_parent"
            android:layout_height="130dp"
            android:cacheColorHint="#00000000"
            android:divider="#DEDEDE"
            android:dividerHeight="1px">
    </ListView>
</RelativeLayout>

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:background="#FF63FF9B"
        android:layout_height="50dp"
        >

    <TextView
            android:id="@+id/textview_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="10dp"
            android:textColor="#ffffff"
            android:text="View Third List"/>
</LinearLayout>

<RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_marginTop="5dp"
        android:background="@null"
        >
    <ListView
            android:id="@+id/listview3"
            android:layout_width="fill_parent"
            android:layout_height="130dp"
            android:cacheColorHint="#00000000"
            android:divider="#DEDEDE"
            android:dividerHeight="1px">
    </ListView>
</RelativeLayout>
</LinearLayout>

我刚刚在5分钟内完成了这个,你得到的布局看起来像这样:(这只是一个草稿,显然只是为了给你一个想法)

Your layout with listviews

最后但并非最不重要的是,在显示列表的代码中,您将拥有以下内容:

public class Screen_View_Lists extends Activity
{
    BaseAdapter                 listAdapter;
    ListView                    list1,list2,list3;

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.screen_view_packages);

       list1 = (ListView) findViewById(R.id.listview1);
       list2 = (ListView) findViewById(R.id.listview2);
       list2 = (ListView) findViewById(R.id.listview3);

       listAdapter = new Adapter_List_Main(this, packages);//This is my own adapter, you probably use your own custom one as well.
       list1.setAdapter(listAdapter);

      //Setup list to support context menu
      registerForContextMenu(list1);

      //Setup list to support long click events.
      list1.setLongClickable(true);

      //Action Listener for long click on item in the list
      list1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
      {
        public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id)
        {
            //do things here       

        }
    });

    //Action Listener for short click on item in the list
    list1.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
            //do things here
        }
    });

    //And so one . . . 
    }
}

答案 1 :(得分:0)

我建议您更好地查看ListView的文档,或者如果您想更多地了解RecyclerView。后者将为您提供更大的灵活性和可维护性。

无论如何,一个基本的解决方案,只是平衡你的UI层次结构只使用一个ListView / RecyclerView。然后在适配器中,您可以决定容器将要呈现的视图类型。 在您的设计中,我将确定两种类型:

  • TYPE_HEADER
  • TYPE_CHECKBOX

一旦你在getView(...)中有了这些东西,你可以根据位置和类型决定实例化(看看如何有效地重用它们)并绑定。

如果你可以试图避免复杂性那些将无法维持:)