Android 一个滚动视图中有3个垂直列表视图

Android 一个滚动视图中有3个垂直列表视图,android,listview,android-listview,android-scrollview,Android,Listview,Android Listview,Android Scrollview,我需要实现这样一个屏幕: 因此,我用ImageView、2个文本视图和复选框创建了适配器 我需要实现3个列表视图,并使屏幕可滚动 我试图实现,但这对我来说是行不通的,所以我这样做: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:fillViewport="

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

因此,我用ImageView、2个文本视图和复选框创建了适配器

我需要实现3个列表视图,并使屏幕可滚动

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

  <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>

我还尝试了一种解决方案,但对我来说都不可行——ScrollView不能滚动


我还试图实现3个垂直的ListFragments,一个接一个,ListView内部是可滚动的,但是scrollview不是,所以我看不到屏幕的底部

这不是最佳做法,但是如果您真的想要该功能,您可以在触摸父scrollview时禁用该触摸事件,并在离开子容器后重新允许它

我以前就是这样做的,其中“listScanners”是我的listview:

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 ... */
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分钟就完成了这个,你会看到这样的布局:(这只是一个草稿,显然只是给你一个想法)

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

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 . . . 
    }
}
public class Screen\u View\u list扩展活动
{
基本适配器列表适配器;
列表视图列表1、列表2、列表3;
@凌驾
创建公共空间(捆绑冰柱)
{
超级冰柱;
setContentView(R.layout.screen\u view\u包);
list1=(ListView)findViewById(R.id.listview1);
list2=(ListView)findViewById(R.id.listview2);
list2=(ListView)findViewById(R.id.listview3);
listAdapter=newadapter\u List\u Main(这个,包);//这是我自己的适配器,您可能也使用自己的自定义适配器。
list1.setAdapter(listAdapter);
//支持上下文菜单的设置列表
registerForContextMenu(列表1);
//设置列表以支持长时间单击事件。
列表1.setLongClickable(真);
//长时间单击列表中的项目的操作侦听器
list1.setOnItemLongClickListener(新的AdapterView.OnItemLongClickListener()
{
公共布尔值长单击(AdapterView父对象、视图v、整型位置、长id)
{
//在这里做事
}
});
//短按列表中项目的操作侦听器
list1.setOnItemClickListener(新的AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView父视图、视图v、整型位置、长id)
{
//在这里做事
}
});
//所以有一个。
}
}

我建议您更好地查看的文档,或者如果您想进一步了解的话。后者肯定会让您拥有更大的灵活性和可维护性

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

  • 类型_头
  • 键入\u复选框
一旦在getView(…)中有了这些东西,您就可以根据位置和类型决定实例化哪个视图(看看如何有效地重用它们)并绑定它们


如果您可以尝试避免复杂性,则无法维护:)

不能在ScrollView中使用ListView。使用一个ListView并为每行膨胀不同的视图。同意。或者,使用一个
ListView
和我的
MergeAdapter
:。这与首选项屏幕非常相似,它被实现为一个单一的
列表视图
,而不管它的内容是什么。@Carnal,您可以在scrollview中有一个列表视图。是的,这不是最佳实践,也可能不是实现它的最佳方式,但你可以做到。不要只是简单地说,“不能在ScrollView中使用ListView。”看看下面我的答案。那个RecyclerView看起来很有希望!因为他将要显示的列表肯定不适合屏幕,也因为有多个列表。此外,如果要使用3个看起来和行为类似的列表或RecycledViews,则RecycledViewPool看起来是个不错的主意。
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 . . . 
    }
}