【问题标题】:RecyclerView highlight item on click点击时 RecyclerView 突出显示项目
【发布时间】:2015-01-15 18:01:59
【问题描述】:

我动态创建了以下RecyclerView。我想在单击时突出显示单击的项目。点击后跳转到下一个Activity。我在下面给出了背景 XML:

hRecyclerView.setBackgroundResource(R.drawable.mylistview_background); 

这不是设置方法吗?我应该在这里做什么?

        hRecyclerView = (RecyclerView) findViewById(R.id.my_history_view);

        // use a linear layout manager
        hLayoutManager = new LinearLayoutManager(this);
        hRecyclerView.setLayoutManager(hLayoutManager);
        hRecyclerView.setVerticalScrollBarEnabled(false);
        hRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        hRecyclerView.setBackgroundResource(R.drawable.mylistview_background);
   hAdapter = new HistoryAdapter(history, this);
        hRecyclerView.setAdapter(hAdapter);


        hRecyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        // do whatever
                        if(position>0) {
                            History his = history.get(position - 1);
                            Intent intent = new Intent(getApplicationContext(), TrackActivity.class);
                            intent.putExtra("from", his.src_station);
                            intent.putExtra("to", his.dest_station);
                            intent.putExtra("train_no", his.train_no);
                            intent.putExtra("train_name", his.train_name);
                            startActivity(intent);
                            overridePendingTransition(R.anim.right_in, R.anim.left_out);

                        }
                    }
                })
        );

        hRecyclerView.setItemAnimator(new DefaultItemAnimator());

mylistview_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <gradient android:endColor="#22000000" android:startColor="#a7a2288f" android:angle="270" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape>
            <gradient android:endColor="#2200ff00" android:startColor="#a2008f00" android:angle="270" />
        </shape>
    </item>


</selector>

非常感谢任何帮助!

【问题讨论】:

  • 您是否有理由不在 ViewHolder 类中使用 onClickListenner 来处理点击事件?昨天我花了很多时间为这种情况想出最好的解决方案,对我来说这样更容易。如果有什么我不知道的,请告诉我。

标签: android listview android-recyclerview


【解决方案1】:

您正在设置整个 RecyclerView 的背景(没有被“选中”)。您需要设置行视图的背景。在 HistoryAdapter 的 onCreateViewHolder 中,您将膨胀行视图。 是需要有一个可绘制选择器的背景(你可以在xml中设置它,或者在代码中)。

【讨论】:

    【解决方案2】:
    hRecyclerView.setBackgroundResource(R.drawable.mylistview_background); 
    

    首先删除这一行,它是针对整个recyclerview的。

    您的自定义行/项目布局应如下所示(将 mylistview_background.xml 放入可绘制文件夹中):

    <RelativeLayout ...
         android:background="@drawable/mylistview_background">...
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-15
      • 2023-03-25
      • 1970-01-01
      • 2020-10-22
      • 2015-10-25
      • 2014-12-21
      • 2012-03-13
      • 2019-06-12
      相关资源
      最近更新 更多