如何在单个ListView中使用2个不同的项目实现2个不同的列表,并在listView之间具有截面分隔线



我想实现一个带有2个不同列表的列表视图,该列表使用2个不同的项目,并希望使用同一listView显示它们。

  1. 两个列表都用截面分隔线分开
  2. 上面的ListView在项目中有一个自定义选择器ImageButton
  3. 下面的ListView在其物品上拥有日期生意

您应该使用

https://github.com/commonsguy/cwac-merge

为了创建和合并不同的适配器与所需的数据。您还应该插入标头视图。

这里以下示例:

@BindView(R.id.lv_validation_errors)
ListView lvValidationErrors;
private List<Notification> notificationList = new ArrayList<>();
public MergeAdapter mergeAdapter = new MergeAdapter();
public ArrayAdapter errorAdapter;
private ArrayList<String> errorList = new ArrayList<>();
public ArrayAdapter alertAdapter;
private ArrayList<String> alertList = new ArrayList<>();

首先,我设置了ListView(使用Butthnife,可以做FindViewByid的东西)然后

errorAdapter = new ArrayAdapter(this, R.layout.adapter_error, R.id.text1, errorList);
mergeAdapter.addView(header("Erros"), false);
mergeAdapter.addAdapter(errorAdapter);

我创建了我想磨损的适配器,并添加了截面标题的视图(不要忘记将值设置为列表以防止其为null)然后

 alertAdapter = new ArrayAdapter(this, R.layout.adapter_warning, R.id.text1, alertList);
 mergeAdapter.addView(header("Advertências"), false);
 mergeAdapter.addAdapter(alertAdapter);

与上一个步骤相同,最后是:

lvValidationErrors.setAdapter(mergeAdapter);

将合并的适配器设置为listView。

为此,您必须使用类型Object

列出一个列表
List<Object> listItems = new ArrayList<>();

您两个不同的列表值添加了上面的listItems

然后用 listItems绑定适配器后,当您在BindView()中绑定物品时,如下所示。

if(listItems instanceof firstListItemModel)
  // bind item from your first list
else if(listItems instanceof secondListItemModel)
// bind item from your second list

据我所知,您想要一个垂直半划分的屏幕,其中包含ListView
您是说底部ListView完全基于上层ListView
底部ListView应根据ListView选择加载。

如果,那么您可以使用权重显示出这样的布局上的ListViews (上和底部)这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">
<ListView
        android:id="@+id/list_view1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

<ListView
        android:id="@+id/list_view2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

然后,您可以在list_view1上的CC_11上进行设置适配器,单击:

ListView upperListView=(ListView) findViewById(R.id.list_view1);
ListView bottomListView=(ListView) findViewById(R.id.list_view2);
      upperListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // You can put your logic here to fetch data according to upper ListView item clicked position. I am taking temp. data for now.
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList//your desired list here);
                    ((ListView) bottomListView.setAdapter(adapter);
                }
            });

就是这样!