gpt4 book ai didi

android - 一个 View 中有 2 个列表的 Recyclerview?

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:40 26 4
gpt4 key购买 nike

你好,

我有这个屏幕,我想在其中显示出价的赢家和输家。

它需要看起来像这样:

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 个 RecyclerView,但我认为这行不通,因为这个关于获胜和失败的列表可能会很长,这意味着有 2 个不同滚动区域的部分。有没有一种方法可以让我在同一个 recyclerview 中同时显示赢家和输家,标题显示赢家和输家列表以及赢家和输家列表之间的界线?

我的屏幕当前是:

<?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。

非常感谢您对我如何实现这一点的想法。

谢谢

最佳答案

为不同的 View 创建不同的 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);
}
}

关于android - 一个 View 中有 2 个列表的 Recyclerview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36567782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com