Bạn có thể thực hiện việc này bằng cách sử dụng trình xem trong lớp RecyclerAdapter của mình.
//this goes in your recyclerView adapter....
/**
* View holder for workouts
*/
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTitleView;
public TextView mTView1;
public View homeView;
public MyViewHolder(View itemLayoutView) {
super(itemLayoutView);
this.homeView = itemLayoutView;
this.mTitleView = (TextView) itemLayoutView.findViewById(R.id.workoutItemTitleText);
this.mTView1 = (TextView) itemLayoutView.findViewById(R.id.workoutItemWorkoutSets);
homeView.setOnClickListener(new ON)
}
}
//other code.....
@Override
public void onBindViewHolder(WorkoutViewHolder viewHolder, int position) {
viewHolder.mTView1.setText("");
viewHolder.mTitleView.setText("");
//pass the position to the view in the onclick listener
viewHolder.homeView.setId(position);
viewHolder.homeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//this list item has been clicked, do something. maybe use an interface or something to notify your activity/fragment.
}
});
}
//the rest of your code.....
Trong phương thức onBindViewHolder, bạn có thể triển khai logic hoạt ảnh cho chế độ xem đó và chuyển nó vào hoạt động hoặc đoạn của bạn. Chỉ cần tạo giao diện tùy chỉnh cho bộ điều hợp recyclerview của bạn và tạo một phương thức như setMyOnItemClickListener(new MyOnItemClickListener)
. Sau đó, khi một mục được nhấp, hãy thông báo cho giao diện tùy chỉnh của bạn và thực hiện một số thao tác với nhấp chuột.
Nguồn
2014-11-17 02:06:22
Cảm ơn Nathan, tôi sẽ xem xét điều này càng sớm càng tốt. Điều này có vẻ ít cồng kềnh và cồng kềnh, vì vậy tôi sẽ thử điều đó và cho bạn biết ngay =) – Ted
Làm việc như một sự quyến rũ =) – Ted
@Ted vui khi nghe nó! –