我现在尝试了很多不同的方法,但我还没有成功地将onclick方法应用到我的recycleview。我的recycleview正在工作并显示我的项目。
有人可以协助我在我的recycleview中实现Onclick方法吗?
提前谢谢。//戴夫
这是我的适配器:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ItemData[] itemsData;
public OnClickListener mOnClickListener;
public MyAdapter(ItemData[] itemsData) {
this.itemsData = itemsData;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
}
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtViewTitle;
public ImageView imgViewIcon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
}
}
// Return the size of your itemsData (invoked by the layout manager)
@Override
public int getItemCount() {
return itemsData.length;
}
以下是项目数据:
public class ItemData {
private String title;
private int imageUrl;
public ItemData(String title,int imageUrl){
this.title = title;
this.imageUrl = imageUrl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getImageUrl() {
return imageUrl;
}
我的Fragment创建项目的位置:
public class MyMedicinFrag extends Fragment {
@Override
public View onCreateView(LayoutInflater i, ViewGroup container, Bundle savedInstanceState) {
View rod = i.inflate(R.layout.fragment_mymedicin, container, false);
RecyclerView recyclerView = (RecyclerView) rod.findViewById(R.id.recyclerView);
FloatingActionButton fabBtn = (FloatingActionButton) rod.findViewById(R.id.fab);
fabBtn.show();
ItemData itemsData[] = {
new ItemData("Panodil 500 mg", R.drawable.panodil),
new ItemData("Ipren 200 mg", R.drawable.ipren),
new ItemData("Madopar 200 mg", R.drawable.madopar),
new ItemData("Diclofenac 50 mg", R.drawable.diclofenac),
new ItemData("Allopurinol 300 mg", R.drawable.allopurinol)
};
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
MyAdapter mAdapter = new MyAdapter(itemsData);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
return rod;
}}
答案 0 :(得分:0)
onCreateViewHolder()
中的使用此代码:
View view = LayoutInflater.from(mContext).inflate(R.layout.myview, parent, false);
view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
//do something on click
}
}
});