我有一个recyclerview,我想展示两个布局。为此我使用视图类型和不同的视图持有者。并相应地加载,但我只能看到项目列表而不是标题布局。
没有任何错误可以识别任何问题。正如我所做的调试发现它不会进入另一种视图类型,布局没有显示出来。
适配器:
public class TransportListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private ArrayList<Transport> list;
private TransportListFragment transportListFragment;
static final int TYPE_LOAD_TRANSPORT = 0, TYPE_LOAD_PROGRESS = 2, LOAD_TRANSPORT_ITEMS_HEADER = 1;
public TransportListAdapter(Context context, ArrayList<Transport> list,TransportListFragment transportListFragment) {
this.context = context;
this.list = list;
this.transportListFragment = transportListFragment;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (viewType) {
case LOAD_TRANSPORT_ITEMS_HEADER:
View v_order_header = inflater.inflate(R.layout.transport_rout, parent, false);
viewHolder = new LoadTransportHeader(v_order_header);
break;
case TYPE_LOAD_TRANSPORT:
View v_order_items = inflater.inflate(R.layout.transport_item, parent, false);
viewHolder = new TransportListHolder(v_order_items);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == TYPE_LOAD_TRANSPORT) {
TransportListHolder transportListHolder = (TransportListHolder) holder;
retriveAllTransports(transportListHolder, position);
}
else if (getItemViewType(position) == LOAD_TRANSPORT_ITEMS_HEADER ) {
LoadTransportHeader loadTransportHeader = (LoadTransportHeader) holder;
retriveTransportHeader(loadTransportHeader, position);
}
}
@Override
public int getItemViewType(int position) {
Object obj = list.get(position);
if (obj instanceof Transport) {
return TYPE_LOAD_TRANSPORT;
}
else if (obj instanceof LoadTransportHeader) return LOAD_TRANSPORT_ITEMS_HEADER;
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
return list.size();
}
public void retriveTransportHeader(final LoadTransportHeader holder, int position) {
}
public void retriveAllTransports(final TransportListHolder holder, int position) {
final Transport data = (Transport) list.get(position);
holder.tv_product.setText(data.getmProductName());
holder.tv_dateTime.setText(data.getmDateTime());
holder.tv_username.setText(String.valueOf(data.getmUserName()));
String url = "http://104.131.162.126/testslim/v1/src/images/" + data.getmProductImg();
Picasso.with(context)
.load(url)
.resize(400, 400)
.placeholder(R.drawable.product)
.error(R.drawable.product)
.into(holder.img_product);
}
}
请帮忙。谢谢..
答案 0 :(得分:0)
ArrayList list;
您只是传递一种类型的数据,即传输
public int getItemViewType(int position) {
Object obj = list.get(position); // list is of only type Transport
if (obj instanceof Transport) {
return TYPE_LOAD_TRANSPORT; // it will always return this
}
else if (obj instanceof LoadTransportHeader) return LOAD_TRANSPORT_ITEMS_HEADER;
return super.getItemViewType(position);
}
答案 1 :(得分:0)
将private ArrayList<Transport>
列表更改为private ArrayList<Object> list;
它显示一种类型,因为您的List仅用于Transport对象。
public int getItemViewType(int position) {
Object obj = list.get(position);
if (obj instanceof Transport) {
return TYPE_LOAD_TRANSPORT;
}
else if (obj instanceof LoadTransportHeader)
return LOAD_TRANSPORT_ITEMS_HEADER;
return super.getItemViewType(position);
}
在Activty / Fragment ..
List<Object> objects = new Arraylist();
objects.add(new Transport("","",""));
objects.add(new LoadTransportHeader("","",""));
objects.add(new Transport("","",""));
objects.add(new LoadTransportHeader("","",""));