突出显示RecyclerView内选定的项目 [英] Highlight selected item inside a RecyclerView

查看:371
本文介绍了突出显示RecyclerView内选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦突出了RecyclerView内的项目,类似于设置在ListView选择的项目。

I'm having trouble with highlighted an item within a RecyclerView, similar to setting the selected item in a ListView.

目前,我已经建立了RecyclerView,有一个默认的布局管理,并拥有它显示所有数据的适配器。我最近刚刚拿到了 onClickListener()工作(虽然我不知道这是否应该在 onCreateViewHolder() onBindViewHolder *( - 不知道什么时候 onBindViewHolder()被调用)。

At the moment, I've set up up the RecyclerView, have a default LayoutManager, and have an adapter which displays all the data. I've just recently got the onClickListener() working (although I'm not sure if it should go in the onCreateViewHolder() or onBindViewHolder*(- not sure when onBindViewHolder() is called).

我试着摸索,和最近我已经得到的<一个href="http://stackoverflow.com/questions/27194044/how-to-properly-highlight-selected-item-on-recyclerview">question这里。不过,我完全在这个问题消失。那是哪里的onClick()办法(适配器里面,包含活动/片段里面,等?)。那是什么 viewHolderListener ?什么是为getPosition()从和它有什么作用是什么呢?从本质上讲,我已经无处从这个问题得到了,这是我能找到到目前为止最好的资源。

I've tried searching around, and the closest I've gotten is the question here. However, I'm completely lost in that question. Where is that onClick() method (inside the adapter, inside the containing Activity/fragment, etc?). What is that viewHolderListener? What is getPosition() from and what does it do exactly? Essentially, I've gotten nowhere from that question, and it was the best resource I could find so far.

下面是我目前的$ C $下建立RecyclerView:

Here is my current code for setting up the 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);
}

下面是我目前的code适配器:

Here is my current code for the 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;
    }
}

我会AP preciate任何帮助。谢谢你。

I would appreciate any help. Thank you.

推荐答案

RecyclerView 不处理项目选择或状态如的ListView 一样。相反,你必须在你的看法座手动处理这个问题。

RecyclerView does not handle item selection or states like a ListView does. Instead you have to handle this manually in your view holder.

您可以做的第一件事就是声明你的项目视图点击,在你的`ViewHolder构造:

The first thing you can do is declare your item view as clickable, in your `ViewHolder constructor :

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

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

        // ...
    } 

然后,如果你想突出的选择,你必须(使用指数列表为例)跟踪选定行的适配器,并在onBindViewHolder方式:

Then if you want to highlight the selection, you must keep track of the selected rows in your adapter (for example using a List of indices), and in your onBindViewHolder method :

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

作为一个侧面说明,您应该设置onClickListener上的onCreateViewHolder而不是onBindViewHolder父视图。该onBindViewHolder方法将被调用多次为相同的观点持有人,你会比需要执行更多的操作

As a side note you should set the onClickListener on your parent view in the onCreateViewHolder instead of the onBindViewHolder. The onBindViewHolder method will be called many times for the same view holder, and you'll perform more operations than necessary

这篇关于突出显示RecyclerView内选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆