首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在回收器视图中更改按下项目视图的可见性

在回收器视图中更改按下项目视图的可见性
EN

Stack Overflow用户
提问于 2017-09-23 00:03:58
回答 1查看 467关注 0票数 0

我在活动中做点击的处理。

代码语言:javascript
复制
    @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);

项目的布局

代码语言:javascript
复制
    <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的可见性。

编辑。回收器视图的我的适配器

代码语言:javascript
复制
    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个处理点击的选项,在回收视图中1.在ViewHolder中2.在OnBindViewHolder中3.使用界面和在活动中转移点击处理。(我用了这个选项)但我不明白选择治疗地点的原则。在哪些情况下应该选择这种方法或那种方法?如果你能解释一下,我将不胜感激。非常好。

EN

回答 1

Stack Overflow用户

发布于 2017-09-23 00:56:24

修改onclick in &查看是否,单击项目更改颜色

代码语言:javascript
复制
          @Override
        public void onClick(View view) {
            if (myClickListener != null) myClickListener.onAvaClickListener(getLayoutPosition());
AvaViewHolder.iv.setBackgroundColor(Color.rgb(100, 100, 50));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46368874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档