【问题标题】:Android : Merging two types of List with different adapters, lists get merged in UIAndroid:使用不同的适配器合并两种类型的列表,列表在 UI 中合并
【发布时间】:2016-03-31 18:02:07
【问题描述】:

我正在开发一个 Android 项目,其中有两种不同类型的聊天可能性。一个用于Groups,另一个用于private 或一对一通信。现在,登录用户可以与之聊天的所有组和用户都显示在ConversationActivity 中。

为此,我在 UI 中准备了两个列表,内容将通过 Async 方法添加到每个列表中。每个列表都有一个单独的适配器,我可以使用它轻松识别单击了哪个项目。

这种机制工作得很好,只是当活动打开时,列表看起来它们是相互叠加的,显然这不是本意。所以我在 RelativeLayout 中指出,将它放在另一个之上,这也没有帮助。

如何在一个活动页面中显示两个具有两个不同适配器的列表?

activity_conversations.xml:

<?xml version="1.0" encoding="UTF-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dip"
        android:layout_above="@+id/privateRelativeLay"
        >
        <ListView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/conversationList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dip"

        android:id="@+id/privateRelativeLay">

        <ListView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/privateConversationList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </RelativeLayout>

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>

ConversationActivity.java

public class ConversationActivity extends ApplicationDrawerLoader {

  @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversations);
if (isOnline()) {
            new getConversationsForLoggedInUser(this).execute();
            new getPrivateChannelsForLoggedInUser(this).execute();

        }


    public class getPrivateChannelsForLoggedInUser extends AsyncTask<Void,Void,ResponseEntity<PrivateChannel[]>>{

 @Override
        protected void onPostExecute(ResponseEntity<PrivateChannel[]> responseEntity) {

 privateChannelConversationList = (ListView) findViewById(R.id.privateConversationList);

            privateConversationAdapter = new PrivateConversationAdapter(conversationActivity, privateMapList);

            privateChannelConversationList.setAdapter(privateConversationAdapter);
// On click adapter excluded
}


    public class getConversationsForLoggedInUser extends AsyncTask<Void, Void, ResponseEntity<RestGroupAccount[]>> {

   @Override
        protected void onPostExecute(ResponseEntity<RestGroupAccount[]> responseEntity) {
            super.onPostExecute(responseEntity);
groupAccountConversationList = (ListView) findViewById(R.id.conversationList);

            groupConversationAdapter = new GroupConversationAdapter(conversationActivity, groupAccountMapList);

            groupAccountConversationList.setAdapter(groupConversationAdapter);

// On click adpater excluded
}
}

我希望这么多信息就足够了,如果需要其他任何信息,请告诉我。你能帮忙的话,我会很高兴。谢谢你。

这是它的外观:截图:

【问题讨论】:

  • 如果有人有兴趣查看整个代码,我已将所有代码发布在此粘贴箱中:pastebin.com/huRMFAHa。谢谢.. :-)
  • 为什么要使用抽屉布局?为什么你有Relativelayouts 作为其中的包装器?您应该在问题中详细说明这些选择。
  • @wvdz :抽屉布局是显示导航抽屉。与这个问题无关。相对布局,我不知道,我想我在开始这个项目时发现了一些东西,对Android不太了解。谢谢你。 :-)

标签: java android list


【解决方案1】:

它们显示在彼此之上,因为您将它们放在具有相同定位属性(无)的单独 RelativeLayout 中。

一种解决方法是将两个 listView 都放在 LinearLayout 中。您可以为这个 LinearLayout 提供垂直或水平方向属性,具体取决于您希望如何显示它们,然后使用 layout_weight 属性来确定每个列表视图占据屏幕的哪个部分。

例如:

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

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

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

注意:fill_parent 是旧版。请改用match_parent

【讨论】:

  • 这很好用。您知道如何在两个列表之间添加分隔线,以便用户知道这是一个不同的列表吗?谢谢。
  • 我可以想到三个解决方案。您可以为 LinearLayout 提供背景颜色,并将顶部和底部边距应用于列表视图。您还可以定义一个边框资源并在每个列表视图周围放置一个边框,如下所示:stackoverflow.com/a/9238046/2947592。第三种解决方案是在列表之间放置另一个 View 对象。
  • 您能否检查一下有关向 UI 添加对象的问题:stackoverflow.com/questions/36332040/…。非常感谢.. :-)
【解决方案2】:

这是因为你的两个列表视图的高度都是 fill_parent。尝试使用包装内容,或者在您的情况下,我不知道您究竟希望如何显示两个列表视图,但我建议您使用具有所需方向的线性布局并分配权重。

RelativeLayout中的布局相互堆叠,只要覆盖整个屏幕的宽度和高度。

现在不推荐使用 fill_parent 属性值,最好使用 match_parent。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 2019-06-09
    • 2013-11-13
    • 2018-02-04
    相关资源
    最近更新 更多