【问题标题】:Android how to display 2 listviews in one activity one after the otherAndroid如何在一个活动中一个接一个地显示2个列表视图
【发布时间】:2013-07-15 15:20:56
【问题描述】:

我已使用此代码在另一个之上显示 2 个列表视图。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#f00" >
</ListView>

<ListView
    android:id="@+id/listView2"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#0f0" >
</ListView>

问题在于,这会导致 2 个列表视图分别占据屏幕的一半。 我正在向这两个列表添加标题。

LevelAdapter adapter = new LevelAdapter(getActivity(),
            R.layout.list_item, weather_data);

    View header = inflater.inflate(R.layout.header2, null);
    View header2 = inflater.inflate(R.layout.header, null);
   lv1.addHeaderView(header);
   lv2.addHeaderView(header2);
    lv1.setAdapter(adapter);
    lv2.setAdapter(adapter);

我希望在第一个列表结束后显示第二个列表的标题。我该怎么做?如何使列表视图出现,以便第二个在第一个结束时开始? 谢谢

【问题讨论】:

    标签: android listview android-layout-weight


    【解决方案1】:

    activity_main.xml

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dip" >
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:text="ANDROID" />
    
            <ListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:background="#B29090" >
            </ListView>
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:gravity="center_vertical"
                android:text="IOS" />
    
            <ListView
                android:id="@+id/listView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:background="#4A9C67" >
            </ListView>
        </LinearLayout>
    
    </ScrollView>
    

    MainActivity.java

    package com.example.listviewin1xmldemo;
    
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    import android.view.View.MeasureSpec;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    
    public class MainActivity extends ActionBarActivity {
    
        private ListView mListView1, mListView2;
    
        private String [] data1 ={"Hiren", "Pratik", "Dhruv", "Narendra", "Piyush", "Priyank"};
        private String [] data2 ={"Kirit", "Miral", "Bhushan", "Jiten", "Ajay", "Kamlesh"};
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mListView1 = (ListView)findViewById(R.id.listView1);
            mListView2 = (ListView)findViewById(R.id.listView2);
    
            mListView1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data1));
            mListView2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data2));
    
            ListUtils.setDynamicHeight(mListView1);
            ListUtils.setDynamicHeight(mListView2);
        }
    
    
        public static class ListUtils {
            public static void setDynamicHeight(ListView mListView) {
                ListAdapter mListAdapter = mListView.getAdapter();
                if (mListAdapter == null) {
                    // when adapter is null
                    return;
                }
                int height = 0;
                int desiredWidth = MeasureSpec.makeMeasureSpec(mListView.getWidth(), MeasureSpec.UNSPECIFIED);
                for (int i = 0; i < mListAdapter.getCount(); i++) {
                    View listItem = mListAdapter.getView(i, null, mListView);
                    listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
                    height += listItem.getMeasuredHeight();
                }
                ViewGroup.LayoutParams params = mListView.getLayoutParams();
                params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1));
                mListView.setLayoutParams(params);
                mListView.requestLayout();
            }
        }
    }
    

    【讨论】:

    • 谢谢老兄,代码就像魅力一样......我已经尝试了这篇文章中以前的代码,它们部分工作。这应该是正确的接受顺序。对于像我这样的其他用户。请参考这个例子。这 100% 有效。其他正在部分工作。
    • 很好的解决方案 Hiren,节省了我大量的时间。谢谢。
    • @Hiren Patel android.R.layout.simple_list_item_1 在哪里?它是android系统使用的通用布局ID吗?我得到“你的内容必须有一个 id 属性为'android.R.id.list'的Listview
    • @user914425,你能复制完整的代码并运行它吗?希望对您有所帮助。
    • 经过很长时间我终于得到了解决方案。我正在尝试这最后三天,这个答案是为我准备的。感谢@HirenPatel 发布此答案
    【解决方案2】:

    这样使用:

    删除线性布局。使用相对布局并在其中放置两个列表视图,如下所示。

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scrollojt"
    android:fillViewport="true" >
    
       <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    
    <ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#f00" >
     </ListView>
    
     <ListView
    android:id="@+id/listView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/listView1"
    android:background="#0f0" >
    </ListView>
        </RelativeLayout>
      </ScrollView>
    

    添加 Utility.java

    public class Utility {
    
        public static void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                // pre-condition
                return;
            }
    
            int totalHeight = 0;
            int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
                totalHeight += listItem.getMeasuredHeight();
            }
    
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
            listView.requestLayout();
        }
    }
    

    在您的活动中

     lv1.setAdapter(adapter);
     lv2.setAdapter(adapter);
    
    Utility.setListViewHeightBasedOnChildren(lv1);
    Utility.setListViewHeightBasedOnChildren(lv2);
    

    【讨论】:

    • 对不起,这不起作用。现在他们两个一起填满了一半的屏幕。如何明确给出 listview 的高度?我也使用了线性布局,其中 layout_below 可能无效
    • Soory,不能正常工作,它在布局中包含半屏。你还有别的想法吗?请帮帮我...
    • @135 然后去自定义列表视图。
    • 我用的是Fragment,所以不知道ListFragment怎么用。
    • 你永远不应该将 ListView 放在 ScrollView 中
    【解决方案3】:

    您应该使用ExpandableListView (http://developer.android.com/reference/android/widget/ExpandableListView.html)。您将有两个部分而不是两个列表视图,但它们(包括标题)将按照您的描述对齐。

    【讨论】:

    【解决方案4】:

    在单个容器中使用多个 ListViews 不是一个好习惯。它会导致测量和性能问题。 更好的是使用带有several view types 的单个 ListView。在您的情况下,视图类型可能是:listview1_viewtypelistview2_viewtypelistview2_header_viewtype。对于第一个 ListView 的标题,您可以只使用标题。

    【讨论】:

      【解决方案5】:

      当提供权重时,高度应提供为 0dp 或 wrap_content。但你没有这样做。如下更改您的 xml 文件。我希望它对你有用。

      根据您的评论,我正在根据要求编辑我的帖子解决方案

      1- : 创建一个只有 2 行的列表视图

      1.1-: 在第一个列表视图 #1 中添加一个列表视图作为行子视图

      1.2-: 在第一个列表视图 #1 中添加另一个列表视图作为第二行子视图

      我希望通过这种方式你可以取得成功。

      【讨论】:

      • 嗨。你能详细说明我如何在这里使用框架布局吗?可能是一些示例代码?
      【解决方案6】:

      我也是 Android 新手,我得到了类似的东西(不完全是一种解决方法),但更容易,方法是制作一个布局文件并将所需数量的列表视图放入其中,并在另一个 XML 布局中包装之前在 ScrollView 中制作的布局。

      假设您将名为 two_listview_layout.xml 的布局定义为

      <RelativeLayout
      ..>
          <ListView
              android:id="@+id/listViewOne"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"/>
          <ListView
              android:id="@+id/listViewTwo"
              android:layout_width="match_parent"
              android:layout_width="wrap_content"/>
      </RelativeLayout>
      

      RelativeLayout 在这里不是强制性的,但您可以根据自己的要求和布局进行调整。

      现在制作另一个布局,该布局包含在 ScrollView 中。由于 ScrollView 只能有 1 个直接子级,因此您将拥有这个完整的布局作为其子级。

      将此(以下)布局命名为 final_layout.xml

      <FrameLayout
      ...>
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
              <include layout="@layout/two_listview_layout" />
          </ScrollView>
      </FrameLayout>
      

      现在在您的片段/活动中扩充这个 final_layout.xml,并使用在 two_listview_layout.xml 中命名的 Id 来访问任何列表视图。

      输出是这样的:(对屏幕录像机质量差表示歉意:p)预告片和评论 - 两者都是列表视图。

      【讨论】:

        【解决方案7】:

        尝试使用 ScrollView 和 LinearLayout:

        activity_main.xml

         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
           <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/action_bar1"
            android:fillViewport="true">
        
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="10dip">
        
                <ListView
                    android:id="@+id/listView1"
                    android:layout_width="match_parent"
                    android:layout_height="200dip"
                    android:layout_margin="10dip"
                    android:background="#B29090">
                </ListView>
        
        
        
                <ListView
                    android:id="@+id/listview2"
                    android:layout_width="match_parent"
                    android:layout_height="200dip"
                    android:layout_margin="10dip"
                    android:background="#4A9C67">
                </ListView>
            </LinearLayout>
        </ScrollView>
        </RelativeLayout>
        

        MainActivity.java

         private ListView list1, list2;
        private String [] data1 ={"H", "P", "D", "N", "P", "P"};
        private String [] data2 ={"K", "M", "B", "K", "A", "K"};
         @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sounds);
            list1 = (ListView) findViewById(R.id.listView1);
            list2 = (ListView) findViewById(R.id.listview2);
        
            list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,data1));
            list2.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,data2));
        
        
        
        }
        

        【讨论】:

          【解决方案8】:

          添加 layout_weight 对我有用。

          <ListView
              android:id="@+id/excerciseListView"
              android:layout_width="match_parent"
              android:layout_height="0px"
              android:divider="#B6B6B6"
              android:dividerHeight="1sp"
              android:layout_weight="3"></ListView>
          <ListView
              android:id="@+id/instructionListView"
              android:layout_width="match_parent"
              android:layout_height="0px"
              android:divider="#B6B6B6"
              android:dividerHeight="1sp" 
             android:layout_weight="1"></ListView>
          

          【讨论】:

            【解决方案9】:

            尝试相对布局:

            <?xml version="1.0" encoding="utf-8"?>
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent" >
            
            <ListView
             android:id="@+id/listView1"
             android:layout_width="match_parent"
             android:layout_height="fill_parent"
             android:layout_weight="1"
             android:layout_below="@+id/listView1"
             android:background="#f00" >
            </ListView>
            <ListView
             android:id="@+id/listView2"
             android:layout_width="match_parent"
             android:layout_height="fill_parent"
             android:layout_weight="1"
             android:background="#0f0" >
            </ListView>
            </RelativeLayout>
            

            【讨论】:

              猜你喜欢
              • 2017-08-28
              • 2011-01-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-04-25
              • 2018-11-09
              • 2018-01-13
              相关资源
              最近更新 更多