我在“活动”中点击了处理。
@Override
public void onClick(View view) {
switch (view.getId()) {
final RecyclerView avaRecycler = (RecyclerView) view2.findViewById(R.id.ava_recycler);
avaRecycler.setLayoutManager(new GridLayoutManager(PersonEditActivity.this, 3));
avaRecycler.setHasFixedSize(true);
final AvaChooseRecyclerAdapter avaChooseRecyclerAdapter = new AvaChooseRecyclerAdapter(PersonEditActivity.this, new AvaChooseRecyclerAdapter.AvaViewHolder.MyClickListener() {
@Override
public void onAvaClickListener(int position) {
Toast.makeText(view2.getContext(), "Выбрана ава " + position, Toast.LENGTH_SHORT).show();
chosenId = (int) avaRecycler.getAdapter().getItemId(position);
avaRecycler.getAdapter().notifyItemChanged(position);
}
});
avaRecycler.setAdapter(avaChooseRecyclerAdapter);
项目布局
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ava_item"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ava_item_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:srcCompat="@drawable/avatars_man" />
<ImageView
android:id="@+id/ava_badge_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:visibility="invisible"
app:layout_constraintRight_toRightOf="@+id/ava_item_imageview"
app:layout_constraintTop_toTopOf="@+id/ava_item_imageview"
app:srcCompat="@drawable/avatars_yes" />
我无法通过点击ava_item_imageview来了解如何更改ava_badge_yes的可见性。
编辑。 我的适用于recyclerview的适配器
public class AvaChooseRecyclerAdapter extends RecyclerView.Adapter<AvaChooseRecyclerAdapter.AvaViewHolder> {
private static final String TAG = "AvaChooseAdapter";
Context context;
private AvaChooseRecyclerAdapter.AvaViewHolder.MyClickListener myClickListener;
List<Integer> resourceIds = Arrays.asList(
R.drawable.avatars_01, R.drawable.avatars_02, R.drawable.avatars_03,
R.drawable.avatars_04, R.drawable.avatars_05, R.drawable.avatars_06,
R.drawable.avatars_07, R.drawable.avatars_08, R.drawable.avatars_09,
R.drawable.avatars_10, R.drawable.avatars_11, R.drawable.avatars_12,
R.drawable.avatars_13, R.drawable.avatars_14, R.drawable.avatars_15,
R.drawable.avatars_16, R.drawable.avatars_17, R.drawable.avatars_18,
R.drawable.avatars_19, R.drawable.avatars_20, R.drawable.avatars_21,
R.drawable.avatars_22, R.drawable.avatars_23, R.drawable.avatars_24,
R.drawable.avatars_25, R.drawable.avatars_26, R.drawable.avatars_27,
R.drawable.avatars_28, R.drawable.avatars_29, R.drawable.avatars_30,
R.drawable.avatars_31, R.drawable.avatars_32, R.drawable.avatars_33,
R.drawable.avatars_34, R.drawable.avatars_35, R.drawable.avatars_36,
R.drawable.avatars_37, R.drawable.avatars_38, R.drawable.avatars_39,
R.drawable.avatars_40, R.drawable.avatars_41, R.drawable.avatars_42,
R.drawable.avatars_43, R.drawable.avatars_44, R.drawable.avatars_45,
R.drawable.avatars_46, R.drawable.avatars_47, R.drawable.avatars_48,
R.drawable.avatars_49, R.drawable.avatars_50, R.drawable.avatars_51,
R.drawable.avatars_52, R.drawable.avatars_53, R.drawable.avatars_54,
R.drawable.avatars_55, R.drawable.avatars_56, R.drawable.avatars_57,
R.drawable.avatars_58, R.drawable.avatars_59, R.drawable.avatars_60,
R.drawable.avatars_61, R.drawable.avatars_62, R.drawable.avatars_63,
R.drawable.avatars_64, R.drawable.avatars_65, R.drawable.avatars_66);
public AvaChooseRecyclerAdapter(Context context, AvaChooseRecyclerAdapter.AvaViewHolder.MyClickListener m) {
this.context = context;
myClickListener = m;
}
public static class AvaViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected MyClickListener myClickListener;
protected ImageView iv;
public AvaViewHolder(View itemView, MyClickListener myClickListener) {
super(itemView);
this.myClickListener = myClickListener;
iv = (ImageView) itemView.findViewById(R.id.ava_item_imageview);
iv.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (myClickListener != null) myClickListener.onAvaClickListener(getLayoutPosition());
}
public interface MyClickListener {
void onAvaClickListener(int position);
}
}
@Override
public long getItemId(int position) {
return resourceIds.get(position);
}
@Override
public AvaChooseRecyclerAdapter.AvaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.ava_item, parent, false);
v.setPadding(14, 14, 14, 14);
return new AvaViewHolder(v, myClickListener);
}
@Override
public void onBindViewHolder(AvaViewHolder holder, int position) {
holder.iv.setImageResource(resourceIds.get(position));
Log.d(TAG, "onBindViewHolder position: " + position + " | " + holder.toString());
}
@Override
public int getItemCount() {
return resourceIds.size();
}
public getItem(int position){
}
}
我想明白。 在论坛上写下3个选项来处理recyclerview中的点击 1.在ViewHolder中 2.在OnBindViewHolder中 3.使用界面和活动中的点击处理转移。 (我用过这个选项) 但我不明白选择按压治疗地点的原则。在哪种情况下选择这个或那个方法? 如果你解释,我将非常感激。高度。
答案 0 :(得分:0)
修改适配器&amp;中的 onclick 。看是否,点击项目更改颜色
@Override
public void onClick(View view) {
if (myClickListener != null) myClickListener.onAvaClickListener(getLayoutPosition());
AvaViewHolder.iv.setBackgroundColor(Color.rgb(100, 100, 50));
}
}