【问题标题】:Recycler view click animation回收站视图点击动画
【发布时间】:2017-03-23 09:25:29
【问题描述】:

我正在尝试将一些点击动画as shown here 添加到我的回收站视图中,但未成功。这是我的一些代码。

在我的活动中

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(this, R.dimen.card_item_offset);
    mRecyclerView.addItemDecoration(itemDecoration);

    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new CustomAdapter(this);
    mRecyclerView.setAdapter(mAdapter);
}

在我的 ViewHolder 中

public CustomViewHolder(View itemView) {
    super(itemView);
    this.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /* on click stuff here */
        }
    });
}

在我的 CardView.xml 中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_loading"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground">

    <TextView
        android:id="@+id/card_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.v7.widget.CardView>

不确定问题出在哪里,但提前感谢您的帮助=)

【问题讨论】:

    标签: android android-recyclerview material-design android-cardview ripple


    【解决方案1】:

    这被称为波纹效果,您正在尝试使用背景而不是前景

    android:focusable="true"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    

    你也可以自定义

    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/colorcode">
    
        <item
            android:id="@android:id/mask"
            android:drawable="@color/colorcode" />
    </ripple>
    

    要支持旧版本,您可以这样做

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true">
            <shape android:shape="rectangle">
                <solid android:color="@color/colorcode" />
            </shape>
        </item>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@android:color/colorcode" />
            </shape>
        </item>
    </selector>
    

    【讨论】:

      猜你喜欢
      • 2017-06-04
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      相关资源
      最近更新 更多