如何在回收站视图中有多个视图
Posted
技术标签:
【中文标题】如何在回收站视图中有多个视图【英文标题】:How to have multiple views in a recyclerview 【发布时间】:2016-10-25 13:12:30 【问题描述】:我想在我的 RecyclerView 中有多个视图。我想在传递“pauze”这个词时使用“card_view_row_pauze”,当没有传递“pauze”这个词时我想使用“card_view_row”。我尝试了一些东西(请参阅我在代码中添加的 cmets),但它不起作用。我在 *** 上找到了一些答案,但不幸的是,我似乎无法实现它。我该怎么做?
MyRecyclerViewAdapter.java
package com.company.app;
import android.app.Activity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MyRecyclerViewAdapter extends RecyclerView .Adapter<MyRecyclerViewAdapter .DataObjectHolder>
private ArrayList<DataObject> mDataset;
private static MyClickListener myClickListener;
private Activity mActivity;
public static class DataObjectHolder extends RecyclerView.ViewHolder implements View .OnClickListener
TextView label;
TextView dateTime;
TextView tijd;
CardView cardView;
public DataObjectHolder(View itemView)
super(itemView);
label = (TextView) itemView.findViewById(R.id.textView);
dateTime = (TextView) itemView.findViewById(R.id.textView2);
tijd = (TextView) itemView.findViewById(R.id.textView3);
cardView = (CardView) itemView.findViewById(R.id.card_view);
itemView.setOnClickListener(this);
@Override public void onClick(View v)
myClickListener.onItemClick(getAdapterPosition(), v);
ViewGroup myParent; // THIS I ADDED TO SUPPORT MULTIPLE VIEWS
public void setOnItemClickListener(MyClickListener myClickListener)
this.myClickListener = myClickListener;
public MyRecyclerViewAdapter(Activity activity, ArrayList<DataObject> myDataset)
mDataset = myDataset;
mActivity = activity;
@Override public DataObjectHolder onCreateViewHolder(ViewGroup parent,int viewType)
myParent = parent; // THIS I ADDED TO SUPPORT MULTIPLE VIEWS
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
@Override
public void onBindViewHolder(DataObjectHolder holder, final int position)
if (mDataset.get(position).getmText1().equals("Pauze")) //HERE I WANT TO USE THE "card_view_row_pauze" VIEW WHEN THE PASSED TEXT CONTAINS "PAUZE"
View viewd = LayoutInflater.from(myParent.getContext()).inflate(R.layout.card_view_row_pauze, myParent, false);
DataObjectHolder dataObjectHolderd = new DataObjectHolder(viewd);
dataObjectHolderd.label.setText(mDataset.get(position).getmText1());
dataObjectHolderd.tijd.setText(mDataset.get(position).getmText3());
else
holder.cardView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String myDescription = mDataset.get(position).getDescription();
if (myDescription.length() >= 2)
Toast.makeText(MainActivity.getMain().getApplicationContext(), myDescription, Toast.LENGTH_SHORT).show();
);
holder.label.setText(mDataset.get(position).getmText1());
holder.dateTime.setText(mDataset.get(position).getmText2());
holder.tijd.setText(mDataset.get(position).getmText3());
public void addItem(final DataObject dataObj, final int index) if (mActivity == null)return;
mActivity.runOnUiThread(new Runnable()
@Override public void run()
mDataset.add(index, dataObj);
notifyItemInserted(index);
);
public void deleteItem(int index)
mDataset.remove(index);
notifyItemRemoved(index);
@Override public int getItemCount() return mDataset.size();
public interface MyClickListener public void onItemClick(int position, View v);
DataObject.java
package com.company.app;
public class DataObject
private String mText1;
private String mText2;
private String mText3;
private String mState;
private String mDescription;
DataObject (String text1, String text2, String text3, String state, String description)
mText1 = text1;
mText2 = text2;
mText3 = text3;
mState = state;
mDescription = description;
public String getmText1() return mText1;
public void setmText1(String mText1)
this.mText1 = mText1;
public String getmText2()
return mText2;
public void setmText2(String mText2)
this.mText2 = mText2;
public String getmText3()
return mText3;
public void setmText3(String mText3)
this.mText3 = mText3;
public String getmState()
return mState;
public void setmState(String mState)
this.mState = mState;
public String getDescription()
return mDescription;
public void setDescription(String mDescription)
this.mDescription = mDescription;
【问题讨论】:
【参考方案1】:您应该使用 Pauze 和行 xml 创建两个 ViewHolder。并在 ItemViewType 中检查您的数据源中的文本值,并根据该 Inflate 特定的 ViewHolder。
这里有一些代码。
public int getItemViewType(int position)
if (products.size()==0)
return 0;
else if (viewFormat== Constants.listV)
return Constants.listV;
else if (viewFormat==Constants.gridV)
return Constants.gridV;
return super.getItemViewType(position);
和 ViewHolder:
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
if (viewType== Constants.gridV)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items,parent,false);
return new MovieViewHolder(view);
else if (viewType==Constants.listV)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.linear_item_view,parent,false);
return new OtherViewHolder(view);
else
return null;
【讨论】:
以上是关于如何在回收站视图中有多个视图的主要内容,如果未能解决你的问题,请参考以下文章
如果我在回收站视图中有数千个项目,那么如何在每个项目上设置项目单击侦听器