我为RecyclerView创建了一个适配器类,用于填充卡片视图布局。它工作正常,但是当我点击RecyclerView中的CardView项目时,我需要获取该CardView项的位置,并基于此编写Intent活动。同时,我需要知道如何在向右滑动时删除该CardView项。
MyAdapter
类。public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private NavigationItem[] navigationItem;
private Context mContext;
public MyAdapter(NavigationItem[] navigationItem) {
this.navigationItem = navigationItem;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.navigation_row_cardview, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
holder.txtViewTitle.setText(navigationItem[position].getTitle());
}
@Override
public int getItemCount() {
return navigationItem.length;
}
public class ViewHolder extends RecyclerView.ViewHolder {
@InjectView(R.id.hambergerRecyclerView)
public RelativeLayout recyclerView;
public TextView txtViewTitle;
public ViewHolder(final View itemLayoutView) {
super(itemLayoutView);
ButterKnife.inject(this, itemLayoutView);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.navigationItemOptionName);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// int itemPosition = RecyclerView.getChildPosition(view);
int itemPosition = recyclerView.getChildAdapterPosition(view);
String item = navigationItem.get(itemPosition);
Toast.makeText(mContext, item, Toast.LENGTH_LONG).show();
}
});
}
}
}