所以当我点击不同的recyclerview行时,我需要开始一项活动。我只是弄清楚我是如何尝试这个代码的 但似乎无法正常工作
class MyViewholder extends RecyclerView.ViewHolder implements View.OnClickListener{
private final Context context;
public MyViewholder(View itemView) {
super(itemView);
context = itemView.getContext();
}
@Override
public void onClick(View view) {
final Intent intent;
switch (getAdapterPosition()){
case 0:
intent = new Intent(context, CalendarControllerFrancisco.class);
break;
case 1:
intent = new Intent(context, CalendarControllerB.class);
break;
case 2:
intent = new Intent(context, CalendarControllerNa.class);
break;
default:
intent = new Intent(context, CalendarController.class);
break;
}
context.startActivity(intent);
公共类LocationAdapter扩展了RecyclerView.Adapter {
private ArrayList<LocationModel> dataSet;
Boolean check = false;
public class MyViewHolder extends RecyclerView.ViewHolder {
//private final Context context;
TextView locat;
public MyViewHolder(View itemView) {
super(itemView);
// context = itemView.getContext();
this.locat = (TextView)itemView.findViewById(R.id.wordtext);
}
}
public LocationAdapter(ArrayList<LocationModel> data) {
this.dataSet = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_row,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
TextView word1 = holder.locat;
word1.setText(dataSet.get(position).getLocations());
}
@Override
public int getItemCount() {
return dataSet.size();
}
答案 0 :(得分:0)
您需要为一个组件添加 setOnClickListener事件。
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
TextView word1 = holder.locat;
word1.setText(dataSet.get(position).getLocations());
word.setOnClickListener(holder);
}
答案 1 :(得分:0)
RecyclerItemClickListener
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
将ClickListener定义为与ListView onItemClickListener
相同
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
// TODO Handle item click
}
})
);
答案 2 :(得分:0)
这是错误的:
@Override
public void onClick(View view) {
final Intent intent;
switch (getAdapterPosition()){
case 0:
intent = new Intent(context, CalendarControllerFrancisco.class);
break;
case 1:
intent = new Intent(context, CalendarControllerB.class);
break;
case 2:
intent = new Intent(context, CalendarControllerNa.class);
break;
default:
intent = new Intent(context, CalendarController.class);
break;
}}
ViewHolder是适配器用来根据要显示的RecyclerView项目任意填充项目数据的容器。因此,它不应该访问项目列表中的任何位置数据以识别自身。你是混乱的概念。
尝试用以下内容替换上述内容:
class MyViewholder extends RecyclerView.ViewHolder implements View.OnClickListener {
private final Context context;
int type;
public MyViewholder(View itemView) {
super(itemView);
context = itemView.getContext();
}
public void bind(int type) {
this.type = type;
}
@Override
public void onClick(View view) {
final Intent intent;
switch (this.type){
case 0:
intent = new Intent(context, CalendarControllerFrancisco.class);
break;
case 1:
intent = new Intent(context, CalendarControllerB.class);
break;
case 2:
intent = new Intent(context, CalendarControllerNa.class);
break;
default:
intent = new Intent(context, CalendarController.class);
break;
}
}
}
然后,在您的Adapter中使用onBindViewHolder回调来访问项集合并为每个项目的创建分配它们的类型。 类似的东西:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.bind(myItemList.get(position).type());
}