我试图在回收站视图中长按一下,更改所选文件和文件夹的背景。它适用于除视频和图像文件之外的所有类型的文件。我该如何解决这个问题?
MyRecyclerViewAdapter.java:(ViewHolder是一个内部类)
// 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>
选择了屏幕截图,但背景颜色没有变化。 AMCAT计算机编程是一个mp4文件,也没有背景颜色变化。
答案 0 :(得分:0)
您实际上正在更新背景颜色,但您的imageview覆盖整个区域,您无法看到它。
要解决这个问题,
或者您可以根据您的设计选择任何其他方法。