简体   繁体   中英

Getting problem in OnTouch and onClick Events in my Adapter class of my Recycler View

I was working on my app. In the main screen of my App I have a Horizontal RecyclerView which loads image and name below the image from the Firebase. I want to send the user to another activity when the user clicks on the image and I want to highlight the image also.

My problem is I have used OnTouch method in my Adapter class to highlight the image , but when i do this it only highlight the image when clicked. I want to send the user to another activity and I want to highlight the image also. So for this , I used intent in my code. But when I use intent in my code then it successfully sends the user to another activity but the problem is after going to another activity when user clicks back then the image is still highlighted.

So the conclusion is I am getting problem in my code I want that when user clicks image then it highlight the image and sends the user to new activity without any bug.

Here is my code: -

public class TrendingSongsAdapter extends RecyclerView.Adapter<TrendingSongsAdapter.ViewHolder> {

    public static final String TAG = "RecyclerViewAdaper";
//    private ArrayList<String> mNames = new ArrayList<>();
//    private ArrayList<String> mImageUrls = new ArrayList<>();

    private List<Upload> mUploads;

    private OnItemListener mOnItemListener;

    private Context mContext;

    public TrendingSongsAdapter(Context context, List<Upload> uploads, OnItemListener onItemListener) {
//        mNames = names ;
//        mImageUrls = imageUrls;
        mUploads = uploads;
        mContext = context;
        this.mOnItemListener = onItemListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.trending_song_items_view, parent, false);


        return new ViewHolder(view, mOnItemListener);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {


        Upload uploadCurrent = mUploads.get(position);

        holder.name.setText(uploadCurrent.getName());

        Glide.with(mContext)
                .asBitmap()
                .load(uploadCurrent.getImageUri())
                .centerCrop()
                .into(holder.image);
        holder.image.setOnTouchListener(new View.OnTouchListener() {
            Rect rect;

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {


                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    holder.image.setColorFilter(Color.argb(50, 0, 0, 0));
                    rect = new Rect(view.getLeft(), view.getRight(), view.getTop(), view.getBottom());
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    holder.image.setColorFilter(Color.argb(0, 0, 0, 0));
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                    if (!rect.contains(view.getLeft() + (int) motionEvent.getX(), view.getTop() + (int) motionEvent.getY())) {
                        holder.image.setColorFilter(Color.argb(0, 0, 0, 0));
                    }
                }



                Intent intent = new Intent(mContext , D.class);
                mContext.startActivity(intent);



                return false;

            }



        });


    }


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

    public interface OnItemListener {

        void onItemClick(int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView image;
        TextView name;

        OnItemListener onItemListener;

        public ViewHolder(View itemview, OnItemListener onItemListener) {
            super(itemview);

            image = itemview.findViewById(R.id.image_view);
            name = itemview.findViewById(R.id.name);
            this.onItemListener = onItemListener;

            itemview.setOnClickListener(this);

        }

        @Override
        public void onClick(View view) {
            onItemListener.onItemClick(getAdapterPosition());
        }
    }

}

Please help me if you have any solution.

Thanks for reading my question.

Try adding else to each if condition and reset the image state, in your case you want to remove the highlight from it

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                holder.image.setColorFilter(Color.argb(50, 0, 0, 0));
                rect = new Rect(view.getLeft(), view.getRight(), view.getTop(), view.getBottom());
            }
             else{ // set here image resource}
            if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                holder.image.setColorFilter(Color.argb(0, 0, 0, 0));
            }
              else{// set here image resource} 
            if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                if (!rect.contains(view.getLeft() + (int) motionEvent.getX(), view.getTop() + (int) motionEvent.getY())) {
                    holder.image.setColorFilter(Color.argb(0, 0, 0, 0));
                }
                    else{// set here image resource}
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM