一个视图中有 2 个列表的 Recyclerview?
Recyclerview with 2 lists in one view?
美好的一天,
我有这个屏幕,我想在其中显示出价的赢家和输家。
它需要看起来像这样:
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。
非常感谢您对我如何实现这一点的想法。
谢谢
为您创建不同的 ViewHolders
不同的视图:TYPE_HEADER_WINNER
、TYPE_HEADER_LOOSER
、TYPE_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);
}
}
请简单 tutorial,您可以在一个 RecyclerView 中创建 2 个 ViewHolder。
美好的一天,
我有这个屏幕,我想在其中显示出价的赢家和输家。
它需要看起来像这样:
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。
非常感谢您对我如何实现这一点的想法。
谢谢
为您创建不同的 ViewHolders
不同的视图:TYPE_HEADER_WINNER
、TYPE_HEADER_LOOSER
、TYPE_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);
}
}
请简单 tutorial,您可以在一个 RecyclerView 中创建 2 个 ViewHolder。