从RecyclerView中触摸的行中获取ImageView

时间:2017-01-24 20:24:28

标签: android

我想在RecyclerView中获取触摸行的ImageVIew。

问题是所有行都具有相同的ImageView ID:

enter image description here

这是YouTube的一个例子  当触摸一行时,图标正在改变:

enter image description here

例如,当我触摸第一行时,我想要创建一个ImageView对象,并使用第一行中的ImageView执行此操作!

图片是使用TextView但不是逻辑上的那些。

更新代码

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 settingImageRow = (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(settingImageRow.getDrawable(), ContextCompat.getColor(v.getContext(), R.color.colorPrimary));
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    // Change ImageView color to icon color
                    DrawableCompat.setTint(settingImageRow.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 activity 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();
    }

}

2 个答案:

答案 0 :(得分:1)

parentView.findViewById(id)parentView内查找具有特定ID的子项。 因此,您应该能够通过调用findViewById毫无问题地找到所需的内部行视图。

您目前正在为OnTouchListener设置RowView。 如果您将视图视为:

holder.itemView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    //V is your row view, the touched one.
    ImageView settingImage = (ImageView) v.findViewById(R.id.settingImage);
    //This imageview is the imageview inside the touched row**.

    //You can also do this:
    settingImage = holder.settingImage


    return true;
}

问题不在于findViewById()和具有相同ID的视图。

你甚至可以在onTouchListener()内直接获得“someView”。

答案 1 :(得分:1)

最终解决方案

第一个问题是有一个变量settingImage,它在上层而且是最终的,所以它被另一个变量覆盖了。第二个问题是我设置颜色色调的部分。这段代码出了点问题,所以我将其删除并制作了新的解决方案:

// 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());

                //Log.d("debug","row touched =" + holder.getLayoutPosition());
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    // Change ImageView color to primary color
                    holder.settingImage.setColorFilter(ContextCompat.getColor(v.getContext(), R.color.colorPrimary));
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    // Change ImageView color to icon color
                    holder.settingImage.setColorFilter(ContextCompat.getColor(v.getContext(), R.color.iconGray));
                }


                return false;
            }
        });