在一个视图中有2个列表的Recyclerview?

时间:2016-04-12 08:35:51

标签: android android-recyclerview android-webservice

美好的一天,

我想要制作这个屏幕,显示出价的赢家和输家。

它需要看起来像这样:

TextView - Winners
Name and bid amount
Name and bid amount
Name and bid amount
--------------------
Textview - Losers
Name and bid amount
Name and bid amount
Name and bid amount
Name and bid amount

来自网络服务的回复如下:

{
  "winningBids": [
    {
      "amount": 500,
      "quantity": 1,
      "name": "Craig",
      "status": "WINNING"
    }
  ],
  "losingBids": [
    {
      "amount": 461,
      "quantity": 1,
      "name": "Bob",
      "status": "LOSE"
    },
    {
      "amount": 460,
      "quantity": 1,
      "name": "James",
      "status": "LOSE"
    }
  ]
}

我想在同一个屏幕上有2个RecyclerViews,但我不认为这会有效,因为这个列表上的输入和输出都可能非常冗长,这意味着2个不同的滚动区域。有没有一种方法可以在同一个回收者视图中同时获得输赢,标题显示赢家和输家的名单以及赢家和输家名单之间的界限?

我的屏幕目前是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"
  tools:context="com.example.app.BiddingHistoryActivity"
  tools:showIn="@layout/activity_bidding_history">

  <android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:padding="@dimen/padding_16"
    android:background="@drawable/border_bottom"
    android:layout_height="wrap_content"/>
</RelativeLayout>

我的行是名称的简单TextView和出价金额的TextView。

您对我如何实现这一点的想法将不胜感激。

谢谢

2 个答案:

答案 0 :(得分:4)

为您创建不同的ViewHolders视图:TYPE_HEADER_WINNERTYPE_HEADER_LOOSERTYPE_ITEM

按照完整适配器here的说明进行操作。

修改:

 private static final int TYPE_HEADER_WINNER = 0;
 private static final int TYPE_HEADER_LOOSER = 1;
 private static final int TYPE_ITEM = 2;

 @Override
 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_ITEM) {
        //inflate your layout and pass it to view holder
        return new VHItem(null);
    } else if (viewType == TYPE_HEADER_WINNER) {
        //inflate your layout and pass it to view holder
        return new VHHeaderWinner(null);
    } else if (viewType == TYPE_HEADER_LOOSER) {
        //inflate your layout and pass it to view holder
        return new VHHeaderLooser(null);
    }

    throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof VHItem) {
        String dataItem = getItem(position);
        //cast holder to VHItem and set data
    } else if (holder instanceof VHHeaderWinner) {
        //cast holder to VHHeaderWinner and set data for header.
    } else if(holder instanceof VHHeaderLooser){        
        //cast holder to VHHeaderLooser and set data for header.
    }
}

@Override
public int getItemCount() {
    return data.length + 2; //Since you have 2 headers
}

@Override
public int getItemViewType(int position) {
    if (position == 0)
        return TYPE_HEADER_WINNER;
    if (position == winnerCount + 1) 
        return TYPE_HEADER_LOOSER;           

    return TYPE_ITEM;
}

class VHHeaderLooser extends RecyclerView.ViewHolder {
    Button button;

    public VHHeader(View itemView) {
        super(itemView);
    }
}

class VHHeaderWinner extends RecyclerView.ViewHolder {
    Button button;

    public VHHeader(View itemView) {
        super(itemView);
    }
}

答案 1 :(得分:0)

请简单tutorial,您可以在一个RecyclerView中创建2个ViewHolder。