突出显示RecyclerView中的所选项目

时间:2014-12-09 23:17:08

标签: android android-recyclerview

我在使用RecyclerView中突出显示某个项目时出现问题,类似于在ListView中设置所选项目。

目前,我已经设置了RecyclerView,拥有默认的LayoutManager,并且有一个显示所有数据的适配器。我刚刚让onClickListener()工作了(虽然我不确定它是否应该放在onCreateViewHolder()onBindViewHolder*( - 不确定何时onBindViewHolder()被称为。)

我试过四处寻找,而我最接近的是question here。但是,我完全迷失在这个问题上。那个onClick()方法在哪里(在适配器内,包含Activity / fragment中等等?)。什么是viewHolderListener?什么是getPosition(),它究竟做了什么?从本质上讲,我从这个问题中找不到任何地方,这是迄今为止我能找到的最好的资源。

以下是我目前设置RecyclerView的代码:

// Sets up the main navigation
private void setupMainNav() {
    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mMainRecyclerNav.setHasFixedSize(true);

    // use a linear layout manager
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    mMainRecyclerNav.setLayoutManager(layoutManager);

    // Create and set the adapter for this recyclerView
    MainNavAdapter adapter = new MainNavAdapter(getActivity());
    mMainRecyclerNav.setAdapter(adapter);
}

这是我当前的适配器代码:

class MainNavAdapter extends RecyclerView.Adapter<MainNavAdapter.ViewHolder> {
    Context mContext;

    // Holds the titles of every row
    String[] rowTitles;

    // Default constructor
    MainNavAdapter(Context context) {
        this.mContext = context;

        // Get the rowTitles - the necessary data for now - from resources
        rowTitles = context.getResources().getStringArray(R.array.nav_items);
    }

    // Simple class that holds all the views that need to be reused
    class ViewHolder extends RecyclerView.ViewHolder{
        View parentView; // The view which holds all the other views
        TextView rowTitle; // The title of this item

        // Default constructor, itemView holds all the views that need to be saved
        public ViewHolder(View itemView) {
            super(itemView);

            // Save the entire itemView, for setting listeners and usch later
            this.parentView = itemView;

            // Save the TextView- all that's supported at the moment
            this.rowTitle = (TextView) itemView.findViewById(R.id.row_title);
        }
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        // Create the view for this row
        View row = LayoutInflater.from(mContext)
                .inflate(R.layout.list_navmain_row, viewGroup, false);

        // Create a new viewHolder which caches all the views that needs to be saved
        ViewHolder viewHolder = new ViewHolder(row);

        return viewHolder;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        viewHolder.rowTitle.setText(rowTitles[i]);

        // TODO: Make a better workaround for passing in the position to the listener
        final int position = i;



        // Set a listener for this entire view
        viewHolder.parentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Clicked on row: " + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return rowTitles.length;
    }
}

我将不胜感激任何帮助。谢谢。

4 个答案:

答案 0 :(得分:16)

RecyclerView不会处理项目选择或ListView之类的状态。相反,您必须在视图持有者中手动处理此操作。

你可以做的第一件事是在你的`ViewHolder构造函数中将你的项目视图声明为可点击:

    public ViewHolder(View itemView) {
        super(itemView);

        // Make this view clickable
        itemView.setClickable(true);

        // ...
    } 

然后,如果要突出显示所选内容,则必须跟踪适配器中的所选行(例如使用索引列表),并在onBindViewHolder方法中:

@Override 
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    // mark  the view as selected:
    viewHolder.parentview.setSelected(mSelectedRows.contains(i));
} 

作为旁注,您应该在onCreateViewHolder而不是onBindViewHolder上的父视图上设置onClickListener。 onBindViewHolder方法将为同一个视图持有者多次调用,并且您将执行超出必要的操作

答案 1 :(得分:6)

有一种简单的方法:

svn commit -m "Added first version of my project"

答案 2 :(得分:5)

我正在扩展@ XGouchet的答案,因为即使它让我走上了正确的道路,我仍然需要做额外的工作来支持选择AND UNSELECTING行(比如切换)。 @ScottBiggs和我都需要这种类型的支持。

代码保留在mSelectedPosition上,允许在onBindViewHoldermSelectedView中正确设置选择,这样可以在点击时正确切换选择(打开和关闭)。 / p>

private class AsanaAdapter extends RecyclerView.Adapter<AsanaViewHolder> {
    private RecyclerView mRecyclerView;

    private final List<Asana> mAsanas;
    private int mSelectedPosition;
    private View mSelectedView;

    public AsanaAdapter(RecyclerView recyclerView, List<Asana> items) {
        mRecyclerView = recyclerView;
        mAsanas = items;
        setHasStableIds(true);
        setSelected(0);
    }

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

    @Override
    public AsanaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View newView = getLayoutInflater().inflate(
                R.layout.performance_builder_list_content, parent, false);
        newView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!v.isSelected()) {
                    // We are selecting the view clicked
                    if (mSelectedView != null) {
                        // deselect the previously selected view
                        mSelectedView.setSelected(false);
                    }
                    mSelectedPosition = mRecyclerView.getChildAdapterPosition(v);
                    mSelectedView = v;
                    setTitle(mAsanas.get(mSelectedPosition).getName());
                } else {
                    // We are deselecting the view clicked
                    setTitle("");
                    mSelectedPosition = -1;
                    mSelectedView = null;
                }

                // toggle the item clicked
                v.setSelected(!v.isSelected());
            }
        });
        return new AsanaViewHolder(newView);
    }

    @Override
    public void onBindViewHolder(AsanaViewHolder holder, int position) {
        if (position == mSelectedPosition) {
            holder.itemView.setSelected(true);

            // keep track of the currently selected view when recycled
            mSelectedView = holder.itemView;
        } else {
            holder.itemView.setSelected(false);
        }
    }
}

答案 3 :(得分:2)

如果您想在点击后立即突出显示所选项目,请尝试使用此解决方案。我使用retrolambda但你也可以使用普通的Runnable对象。

1

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    @BindView(R.id.appIcon)
    ImageView appIcon;
    @BindView(R.id.appName)
    TextView appName;

    public ViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void onClick(View v) {
        v.setSelected(true);
        new Handler().postDelayed(() -> v.setSelected(false), 100);
        //or new Handler().postDelayed(new Runnable...

        //your onClick action
    }
}
  1. 在Drawable目录中创建list_item_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:drawable="@color/colorPrimary" />
        <item android:drawable="@android:color/transparent" />
    </selector>
    
  2. 将选择器设置为单个项目的根布局

    android:background="@drawable/list_item_selector"