Android studio OnTouchListener for RecyclerView项目,更改项目颜色

时间:2017-01-23 21:04:08

标签: java android android-recyclerview

当您查看我的适配器时,您可以看到OnTouchListener。你可以看到我想要改变触摸元素的颜色。但是在有两条以上的线后,我可以看到当我点击一条线时我改变了所有元素的颜色。有没有办法说明#change颜色只是被点击的项目#?



public class SettingsAdapter extends RecyclerView.Adapter<SettingsAdapter.MySettingHolder> {

    private List<Settings> settingList;
    private Context context;

    public class MySettingHolder extends RecyclerView.ViewHolder {

        public int type;
        public ImageView settingImage;
        public TextView settingTitle, settingSubtitle;

        public MySettingHolder(View view) {
            super(view);
            settingImage = (ImageView) view.findViewById(R.id.settingImage);
            settingTitle = (TextView) view.findViewById(R.id.settingTitle);
            settingSubtitle = (TextView) view.findViewById(R.id.settingSubtitle);
        }
    }

    public SettingsAdapter(Context context) {
        this.context = context;
    }

    public SettingsAdapter(List<Settings> settingList) {
        this.settingList = settingList;
    }

    @Override
    public MySettingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.settings_list_row, parent, false);

        return new MySettingHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MySettingHolder holder, int position) {
        // Setting for one entry
        final Settings setting = settingList.get(position);
        //RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        holder.settingImage.setImageResource(setting.getSettingImageUrl());
        // If the settingSubtitle is empty it should be not visible and just the settingTitle
        if (setting.getSettingSubtitle().equals("")) {
            //params.setMargins(0, 18, 0, 18);
            //holder.settingTitle.setLayoutParams(params);
            holder.settingTitle.setText(setting.getSettingTitle());
            holder.settingSubtitle.setVisibility(View.GONE);
        } else {
            //params.setMargins(0, 18, 0, 18);
            //holder.settingTitle.setLayoutParams(params);
            //params.setMargins(0, 0, 0, 18);
            //holder.settingSubtitle.setLayoutParams(params);
            holder.settingTitle.setText(setting.getSettingTitle());
            holder.settingSubtitle.setText(setting.getSettingSubtitle());
        }

        // Get setting holder type
        holder.type = setting.getType();

        // OnTouchListener for holder/vector color change
        holder.itemView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // Define setting holder
                MySettingHolder holder = (MySettingHolder) (v.getTag());

                // ImageView for changing color
                ImageView imageView = (ImageView) v.findViewById(R.id.settingImage);

                // Detect if user touch or not
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    // Change ImageView color to primary color
                    DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(v.getContext(), R.color.colorPrimary));
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    // Change ImageView color to primary color
                    DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(v.getContext(), R.color.iconGray));
                }

                return false;
            }
        });

        // OnClickListener for holder
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                // Define setting holder
                MySettingHolder holder = (MySettingHolder) (v.getTag());

                // Define switch for line
                switch (holder.type) {
                    // Case 1 = Logout
                    case 1:
                        // OnCLickListener for an event
                        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which) {

                                    // Case user types Yes
                                    case DialogInterface.BUTTON_POSITIVE:
                                        // Deleting shared preferences for login activity
                                        SharedPreferences loginPreferences = v.getRootView().getContext().getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
                                        SharedPreferences.Editor editor = loginPreferences.edit();
                                        editor.clear();
                                        editor.commit();

                                        // Start new acitivity for login
                                        v.getContext().startActivity(new Intent(v.getContext(), LoginActivity.class));

                                        // Exit activity
                                        ((Activity)v.getContext()).finish();

                                        // Exit case button positive/yes
                                        break;

                                    case DialogInterface.BUTTON_NEGATIVE:
                                        // Dismiss dialog
                                        dialog.dismiss();

                                        // Exit case button negative/no
                                        break;
                                }
                            }
                        };
                        // AlertDialog for logout check
                        AlertDialog.Builder builder = new AlertDialog.Builder(v.getRootView().getContext());
                        builder.setMessage("Möchtest du dich wirklich abmelden?")
                                .setPositiveButton("Ja", dialogClickListener)
                                .setNegativeButton("Nein", dialogClickListener)
                                .show();

                        // Exit switch
                        break;

                    case 2:
                        // do other stuff
                        break;

                    default:

                        break;
                }
            }
        });

        holder.itemView.setTag(holder);
    }

    @Override
    public int getItemCount() {
        return settingList.size();
    }

}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我的工作解决方案是使用holder.getAdapterPosition()保持触发onTouch事件的项目的位置然后当使用任何notifyDataSetChange()方法更新回收器时,您可以强制仅重绘匹配的位置与你的onTouched职位。

另一个更有效的方法是仅使用以下命令更新触发onTouch事件的位置:

notifyItemChanged(positionItemTouched);