【问题标题】:Two ListViews inside a Linear Layout线性布局内的两个 ListView
【发布时间】:2013-02-21 12:57:23
【问题描述】:

请帮助我在线性布局中垂直放置两个列表视图的布局。问题是如果 list1 的数据多于 list2 不可见且不可滚动,则使用 scollview 不是解决方案。

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_calendar_top"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/prevMonth"
            android:layout_width="50dp"
            android:layout_height="55dp"
            android:src="@drawable/button_events_previous" >
        </ImageView>

        <TextView
            android:id="@+id/currentMonth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.6"
            android:gravity="center_vertical|center_horizontal"
            android:text="Janauary"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#d458b1"
            android:textStyle="bold" >
        </TextView>

        <ImageView
            android:id="@+id/nextMonth"
            android:layout_width="50dp"
            android:layout_height="55dp"
            android:src="@drawable/button_events_next" />
    </LinearLayout>


        <TextView
            android:id="@+id/direction_label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="10dp"
            android:background="#DA81F5"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="PRESS RELEASES"
            android:textColor="#FFFFFF"
            android:textSize="13dip" />

        <ListView
            android:id="@+id/eventsList"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/direction_label"
            android:background="#ffffff" >
        </ListView>


        <TextView
            android:id="@+id/direction_label1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="#DA81F5"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="SPEECHES"
            android:textColor="#FFFFFF"
            android:textSize="13dip" />

        <ListView
            android:id="@+id/eventsList1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/direction_label1"
            android:background="#ffffff" >
        </ListView>


    <TextView
        android:id="@+id/empty_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

如果我使用滚动视图,那么它只会为第一行包装列表视图。

请提出建议。

编辑:

    <?xml version="1.0" encoding="utf-8"?>

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



    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_calendar_top"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/prevMonth"
            android:layout_width="50dp"
            android:layout_height="55dp"
            android:src="@drawable/button_events_previous" >
        </ImageView>

        <TextView
            android:id="@+id/currentMonth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.6"
            android:gravity="center_vertical|center_horizontal"
            android:text="Janauary"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#d458b1"
            android:textStyle="bold" >
        </TextView>

        <ImageView
            android:id="@+id/nextMonth"
            android:layout_width="50dp"
            android:layout_height="55dp"
            android:src="@drawable/button_events_next" />
    </LinearLayout>


        <TextView
            android:id="@+id/direction_label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="10dp"
            android:background="#DA81F5"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="PRESS RELEASES"
            android:textColor="#FFFFFF"
            android:textSize="13dip" />

        <ListView
            android:id="@+id/eventsList"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_below="@+id/direction_label"
            android:background="#ffffff" >
        </ListView>

        <TextView
            android:id="@+id/direction_label1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="#DA81F5"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="SPEECHES"
            android:textColor="#FFFFFF"
            android:textSize="13dip" />

        <ListView
            android:id="@+id/eventsList1"
            android:layout_width="fill_parent"
                 android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_below="@+id/direction_label1"
            android:background="#ffffff" >
        </ListView>

    <TextView
        android:id="@+id/empty_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

如果我使用它,什么都不会显示:(

阿斯米

编辑:两个列表视图的代码:

            {
                arrayofWebData.add(cn);
                listAdapter = new SelectArralAdapter(getActivity(),
                        arrayofWebData);
                List_events.setAdapter(listAdapter);
            }

第二个 ListView 如下:

  if ((year == event_year) && (event_month == month))

            {
            arrayofWebDataPress.add(cn);
            listAdapter_press = new   SelectArralAdapter_Press(getActivity(),arrayofWebDataPress);
                List_events_press.setAdapter(listAdapter_press);
                i++;

            }

两个 ListView 的数组适配器:

     class SelectArralAdapter_Press extends ArrayAdapter<PressDB> {

    private LayoutInflater inflater;

    public SelectArralAdapter_Press(Context context,
            ArrayList<PressDB> arrayofWebDataPress) {

        super(context, R.layout.speech_list_item, R.id.event_title,
                arrayofWebDataPress);
        inflater = LayoutInflater.from(context);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder_Press holder;
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.speech_list_item, null);

            holder = new ViewHolder_Press(convertView);
            convertView.setTag(holder);

        } else {

            holder = (ViewHolder_Press) convertView.getTag();
        }
        holder.populateFrom(arrayofWebDataPress.get(position));

        return (convertView);
    }

}

  class ViewHolder_Press{
    public TextView event_name = null;
    public TextView event_date = null;

    public ViewHolder_Press(View row) {
        event_name = (TextView) row.findViewById(R.id.event_title);
        event_date = (TextView) row.findViewById(R.id.event_date_time);

    }

    void populateFrom(PressDB eventsMainDB) {
        event_name.setText(eventsMainDB.press_name);
        event_date.setText(eventsMainDB.press_date + " ");
    }
}

对于第二个 Adaper:

      class SelectArralAdapter extends ArrayAdapter<SpeechDB> {

    private LayoutInflater inflater;

    public SelectArralAdapter(Context context,
            ArrayList<SpeechDB> arrayofWebData) {

        super(context, R.layout.speech_list_item, R.id.event_title,
                arrayofWebData);
        inflater = LayoutInflater.from(context);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.speech_list_item, null);

            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();
        }
        holder.populateFrom(arrayofWebData.get(position));

        return (convertView);
    }

}

class ViewHolder {
    public TextView event_name = null;
    public TextView event_date = null;

    public ViewHolder(View row) {

        event_name = (TextView) row.findViewById(R.id.event_title);

        event_date = (TextView) row.findViewById(R.id.event_date_time);

    }

    void populateFrom(SpeechDB eventsMainDB) {
        event_name.setText(eventsMainDB.speech_name);

        event_date.setText(eventsMainDB.speech_date + " ");


    }

}

【问题讨论】:

  • 你推荐哪种解决方案我尝试了相对布局,但它不起作用
  • 您可以尝试将layout_weight=1 属性放在您的ListView 上,两者的比例相同。请记住将layout_height 更改为0 而不是wrap_content
  • 你能详细说明你的问题吗?
  • y 没有给出固定尺寸或重量??

标签: android android-listview


【解决方案1】:

试试这个。

如果你想使用 xml 设计,那么你必须给出修正尺寸,或者通过使用 weight(这是最可取的) 或者通过在 dp 中给出修正尺寸

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backrepeat"
    android:weightSum="1"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_calendar_top"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/prevMonth"
            android:layout_width="50dp"
            android:layout_height="55dp"
            android:src="@drawable/button_events_previous" >
        </ImageView>

        <TextView
            android:id="@+id/currentMonth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.6"
            android:gravity="center_vertical|center_horizontal"
            android:text="Janauary"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#d458b1"
            android:textStyle="bold" >
        </TextView>

        <ImageView
            android:id="@+id/nextMonth"
            android:layout_width="50dp"
            android:layout_height="55dp"
            android:src="@drawable/button_events_next" />
    </LinearLayout>

    <TextView
        android:id="@+id/direction_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="10dp"
        android:background="#DA81F5"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:text="PRESS RELEASES"
        android:textColor="#FFFFFF"
        android:textSize="13dip" />

    <ListView
        android:id="@+id/eventsList"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:layout_below="@+id/direction_label"
        android:background="#ffffff" >
    </ListView>

    <TextView
        android:id="@+id/direction_label1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#DA81F5"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:text="SPEECHES"
        android:textColor="#FFFFFF"
        android:textSize="13dip" />

    <ListView
        android:id="@+id/eventsList1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:layout_below="@+id/direction_label1"
        android:background="#ffffff" >
    </ListView>

    <TextView
        android:id="@+id/empty_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

我在设计时得到这个屏幕。

【讨论】:

  • 您的意思是,当您运行此代码时,您将面临这个问题。
  • 否,运行成功,但是两个listview的listview都是空的
  • 也发布您的 java 代码。您可能不会在代码中的 listview 上设置另一个布局。请检查
  • 我也得到了相同的屏幕设计
【解决方案2】:

我曾经使用过:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF">

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

// first linearlayout which is holding one textview and a listview
<LinearLayout
    ">      
    <TextView 
        />        
    <ListView
        android:id="@+id/failed_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"/>
 </LinearLayout>

// second linearlayout which is holding one textview and a listview
<LinearLayout
    ">      
    <TextView 
        />        
    <ListView
        android:id="@+id/failed_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"/>
 </LinearLayout>

// third linearlayout which is holding one textview and a listview
<LinearLayout
    ">      
    <TextView 
        />        
    <ListView
        android:id="@+id/failed_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"/>
 </LinearLayout>

// and finally i have added a button in the parent linearlayout
<Button
     />

</LinearLayout>
</ScrollView>

你可以试试这个。

【讨论】:

    【解决方案3】:

    尝试如下:

    这是同一布局文件中多个 ListView 的解决方案。您可以通过为LinearLayout 内的每个布局提供相等的权重,将布局文件设置为以下布局。

    <LinearLayout android:layout_weight="1" 
                        android:layout_height="fill_parent" 
                        android:layout_width="fill_parent">
    
                    <ListView   android:id="@+id/list1" 
                                android:layout_height="fill_parent" 
                                android:layout_width="fill_parent">
    
                    </ListView>
        </LinearLayout>
    
    <LinearLayout android:layout_weight="1" 
                    android:layout_height="fill_parent" 
                    android:layout_width="fill_parent">
    
                <ListView   android:id="@+id/list2" 
                            android:layout_height="fill_parent" 
                            android:layout_width="fill_parent">
    
                </ListView>
    </LinearLayout>
    

    Java 代码

    public class MainActivity extends Activity {
    private ListView lv1 = null;
    private ListView lv2 = null;
    private String s1[] = {"a", "b", "c", "d", "e", "f"};
    private String s2[] = {"r", "s", "t", "u", "v", "w", "x"};
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.main);
        lv1 = (ListView) findViewById (R.id.list1);
        lv2 = (ListView) findViewById (R.id.list2);
    
        lv1.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s1));
        lv2.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s2));
    
    } 
    } 
    

    有关更多详细信息,您可以参考Link,它提供了相同的解决方案。

    希望对你有帮助。

    谢谢。

    【讨论】:

    • 展示我将两个相对布局组合成一些东西
    • 这使屏幕分为两部分,上面一个为 1 个列表视图,下面为另一个 1,每个部分都有自己的列表视图滚动,我可以让整个布局可以滚动而不是单独滚动列表显示。这样一旦 listview1 完成, Listview2 就会启动
    • 你可以尝试将整个布局放到滚动视图中。
    • 如果我把整个Layout放在scrollview中,是否需要禁用Listview的滚动
    • 不,不需要禁用列表视图的滚动。
    【解决方案4】:

    你可以拿这样的东西。

    <LinearLayout>
       <ScrollView>
         <LinearLayout>
           {Your All code of controls}
         </LinearLayout>
       </ScrollView>
    </LinearLayout>
    

    或者你可以将&lt;ScrollView&gt;作为父标签..
    你可以做这样的事情。据我所知...

    【讨论】:

      【解决方案5】:

      我通过使用节标题并将两个列表视图组合成一个列表视图并通过节标题对它们进行分类来解决了这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-28
        • 2017-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多