我正在尝试使用a RecyclerView作为横向ListView.我试图弄清楚如何突出显示所选项目.当我点击其中一个项目时,它会被选中并正确突出显示,但是当我点击另一个项目时,第二个项目会被旧项目突出显示.
这是我的onClick函数:
@Override
public void onClick(View view) {
if(selectedListItem!=null){
Log.d(TAG, "selectedListItem " + getPosition() + " " + item);
selectedListItem.setBackgroundColor(Color.RED);
}
Log.d(TAG, "onClick " + getPosition() + " " + item);
viewHolderListener.onIndexChanged(getPosition());
selectedPosition = getPosition();
view.setBackgroundColor(Color.CYAN);
selectedListItem = view;
}
Run Code Online (Sandbox Code Playgroud)
这是onBindViewHolder:
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.setItem(fruitsData[position]);
if(selectedPosition == position)
viewHolder.itemView.setBackgroundColor(Color.CYAN);
else
viewHolder.itemView.setBackgroundColor(Color.RED);
}
Run Code Online (Sandbox Code Playgroud) 我正在试验支持库的recyclerview和卡片.我有回收卡的卡片.每张卡片的右上角都有一个"x"图标可将其删除:
该卡的xml, list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/taskDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textSize="40sp"
android:text="hi"/>
<ImageView
android:id="@+id/xImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:src="@drawable/ic_remove"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
我试图用我会在使用的位置标记的行notifyItemRemoved(position)中TaskAdapter.java:
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder> {
private List<Task> taskList;
private TaskAdapter thisAdapter = this;
// cache of views to reduce number of findViewById calls
public static class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected TextView taskTV;
protected ImageView closeBtn;
public TaskViewHolder(View v) {
super(v); …Run Code Online (Sandbox Code Playgroud) android android-support-library android-cardview android-recyclerview
我有一个应用程序,我需要适应Android TV.此应用程序包含水平RecyclerView,当我按遥控器上的D-pad按钮时它不会滚动.我找到了这个解决方案,但它崩溃了.这是代码:
<ru.myapp.package.HorizontalPersistentFocusWrapper
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@null"
android:scrollbars="none"/>
</ru.myapp.package.HorizontalPersistentFocusWrapper>
Run Code Online (Sandbox Code Playgroud)
HorizontalPersistentFocusWrapper与PersistentFocusWrapper相同,但mPersistFocusVertical = false;
在这个地方发生崩溃:
@Override
public void requestChildFocus(View child, View focused) {
super.requestChildFocus(child, focused);
View view = focused;
while (view != null && view.getParent() != child) {
view = (View) view.getParent(); <<<------ Crash here
}
mSelectedPosition = view == null ? -1 : ((ViewGroup) child).indexOfChild(view);
if (DEBUG) Log.v(TAG, "requestChildFocus focused " + focused + " mSelectedPosition " + mSelectedPosition);
}
Run Code Online (Sandbox Code Playgroud)
崩溃堆栈跟踪:
java.lang.ClassCastException: android.view.ViewRootImpl cannot …Run Code Online (Sandbox Code Playgroud)