0

I am trying to change background of selected files and folders on long click in recycler view. It is working for all kinds of files except video and image files. How can I fix that ?

MyRecyclerViewAdapter.java : (ViewHolder is an inner class)

 // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        TextView myTextView;
        ImageButton myImage;
        ViewHolder(View itemView) {
            super(itemView);
            myImage = (ImageButton) itemView.findViewById(R.id.buttonimage);
            myTextView = (TextView) itemView.findViewById(R.id.info_text);
            myImage.setOnClickListener(this);
            myImage.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }

        @Override
        public boolean onLongClick(View view) {
            if (mClickListener != null) mClickListener.onLongClick(view, getAdapterPosition());
            view.setBackgroundColor(Color.MAGENTA);  //changing background color here
            return  true;
        }
    }

    // convenience method for getting data at click position
    public String getItem(int id) {
        return mData2.get(id);
    }

    // allows clicks events to be caught
    public void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    // allows clicks events to be caught
    public void setLongClickListener(ItemClickListener itemClickListener1) {
        this.mClickListener = itemClickListener1;
    }

    // parent activity will implement this method to respond to click events
    public interface ItemClickListener {
        void onItemClick(View view, int position);
        boolean onLongClick(View view,int position);
    }

}

MainActivity :

@Override
    public boolean onLongClick(View view, int position) {
        Toast.makeText(this, "is selected", Toast.LENGTH_SHORT)
                .show();
        return true;
    }

activity_internal_storage.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dell_1.myapp3.InternalStorage">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvNumbers"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

recyclerview_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >


    <ImageButton
        android:id="@+id/buttonimage"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/folder"
        android:scaleType="fitXY"
        android:background="?android:selectableItemBackground"

        />

    <TextView
        android:id="@+id/info_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:ellipsize="end"
        android:maxLines="2"
       />

</LinearLayout>

This is what I am getting

Screenshots are selected but background color is not changing. AMCAT computer programming is an mp4 file , no background color change for it too.

  • Actually its adding background color, since Images are covering whole view, you cant see. try to set padding to imagebutton and then check. – Balu Sangem Feb 12 '18 at 07:04
  • thanks a lot, that solves the problem to an extent :) , is there any other way of fixing it ? @BaluSangem – natalie jkb Feb 12 '18 at 07:15
  • That means you have to set padding while item is selected and remove when not selected – Balu Sangem Feb 12 '18 at 07:16
  • is this the only way of doing this ? @BaluSangem its messing up the layout a bit – natalie jkb Feb 12 '18 at 07:18
  • If you are chaning only background color of imageview onlongclick , then yes. If you dont want this then you have to choose other type of design , for example change background of whole parent view(linearlayout) instead of only view etc. – Balu Sangem Feb 12 '18 at 07:20
  • maybe deleting `android:scaleType="fitXY"` from ImageButton is a better way ? @BaluSangem What do you think ? – natalie jkb Feb 12 '18 at 07:35
  • Still if image is big enough to fill ImageButton, then its a problem again – Balu Sangem Feb 12 '18 at 07:36

1 Answers1

0

You are actually updating background color, but your imageview is covering the whole area, you are not able to see it.

To solve this,

  • You can give a small amount of padding to all the views, so the user can see background color for selected items.
  • You can add another view like a checkmark, and you can make it visible when its item is selected with visibility gone for all unselected item.

or you can choose any other approach as per your design.

bhumik
  • 231
  • 2
  • 11