【问题标题】:3 vertical listViews in one scrollview一个滚动视图中的 3 个垂直列表视图
【发布时间】:2014-11-13 15:05:10
【问题描述】:

我需要实现这样的屏幕:

所以,我用 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 中不能有 ListView。使用 ONE ListView 并为每一行添加不同的视图。
  • 同意。或者,使用一个ListView 和我的MergeAdapter:github.com/commonsguy/cwac-merge。这与首选项屏幕非常相似,它被实现为单个 ListView,无论其内容最终是什么。
  • @Carnal ,你可以在滚动视图中有一个列表视图。是的,这不是最佳实践,也可能不是实现它的最佳方式,但您可以做到。不要只是简单地说“ScrollView 中不能有 ListView”。请参阅下面的答案。

标签: android listview android-listview android-scrollview


【解决方案1】:

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

这是我之前的做法,“listScanners”是我的列表视图:

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 意味着线性布局包含一个可以滚动的视图,这意味着它可以在线性布局中具有一定的大小,但是,当您滚动它时,它可能包含更多。这是布局:

<?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 . . . 
    }
}

【讨论】:

    【解决方案2】:

    我建议您更好地查看ListView 的文档,或者如果您想进一步了解RecyclerView。后一种绝对可以让您获得更大的灵活性和可维护性。

    不管怎样,一个基本的解决方案是只使用一个 ListView/RecyclerView,它可以稍微扁平化你的 UI 层次结构。然后在您的适配器中,您可以决定容器将要呈现的视图类型。 在您的设计中,我会确定 2 种类型:

    • TYPE_HEADER
    • TYPE_CHECKBOX

    在 getView(...) 中设置好这些内容后,您可以根据位置决定并键入要实例化的视图(看看如何有效地重用它们)并绑定。

    如果您可以尝试避免复杂性,那么这些将变得无法维护:)

    【讨论】:

    • RecyclerView 看起来很有希望!由于他要显示的列表肯定不适合屏幕,而且还有多个列表。
    • 此外,如果您要使用 3 个外观和行为相似的列表或 RecycledViews,RecycledViewPool 看起来是个好主意。 developer.android.com/reference/android/support/v7/widget/…
    猜你喜欢
    • 2012-07-31
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 2015-12-21
    • 1970-01-01
    • 2013-01-21
    相关资源
    最近更新 更多