Mat*_*ica 18 android android-recyclerview
我有这个items.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/colorPreview" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAppearance="@android:style/TextAppearance.Large"
android:textColor="@android:color/white"
android:id="@+id/colorName" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
当我单独使用它时,selectableItemBackground在我单击视图时动画.但是当我将它用于RecyclerView中的项目时,对点击的影响不再发生.我怎样才能解决这个问题?
PS:这是RecyclerView上的监听器,如果它是相关的:
public ColorListOnItemTouchListener(Context context, OnItemClickListener clickListener) {
mClickListener = clickListener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
if(childView != null && mClickListener != null) {
mClickListener.onItemLongPress(childView, index);
}
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
if(childView != null && mClickListener != null) {
mClickListener.onItemClick(childView, index);
}
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
});
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑:
public class ColorsCursorAdapter extends RecyclerView.Adapter<ColorsCursorAdapter.ViewHolder> {
private static final int layout = R.layout.color_item;
private Cursor mCursor;
public ColorsCursorAdapter(Cursor c) {
super();
this.mCursor = c;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
TextView name = (TextView) v.findViewById(R.id.colorName);
ImageView image = (ImageView) v.findViewById(R.id.colorPreview);
return new ViewHolder(v, name, image);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
mCursor.moveToPosition(position);
int color = mCursor.getInt(mCursor.getColumnIndex(ColorItem.COLUMN_COLOR));
holder.colorName.setText(Utils.getColorString(color));
holder.colorPreview.setImageDrawable(new ColorDrawable(color));
}
@Override
public int getItemCount() {
if(mCursor != null) {
return mCursor.getCount();
}
return 0;
}
public void swapCursor(Cursor c) {
mCursor = c;
notifyDataSetChanged();
}
public Cursor getCursor() {
return mCursor;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView colorName;
public ImageView colorPreview;
public ViewHolder(View root, TextView colorName, ImageView colorPreview) {
super(root);
root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//
}
});
this.colorName = colorName;
this.colorPreview = colorPreview;
}
}
}
Run Code Online (Sandbox Code Playgroud)
并使用以下命令创建适配器:
colorList.setLayoutManager(new LinearLayoutManager(this));
adapter = new ColorsCursorAdapter(null);
colorList.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
ali*_*dro 16
我将其设置为前景,而不是将其设置为背景.希望它会有所帮助.
android:foreground="?attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)
小智 8
在items.xml中,将FrameLayout设置为根布局,并为FrameLayout设置selectableItemBackground.它对我有用,但我不知道为什么.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:selectableItemBackground">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- your code -->
</RelativeLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13529 次 |
| 最近记录: |