美文网首页Android
RecyclerView:模仿ListVIew的点击波纹效果和分

RecyclerView:模仿ListVIew的点击波纹效果和分

作者: KokutouDa | 来源:发表于2016-09-02 16:02 被阅读1529次

使用 RecyclerView 之前先需要导入依赖包,有两种方法:
方法1:直接在 build.gradle(module:app) 中写 compile 'com.android.support:recyclerview-v7:24.2.0'
方法2:File -> Project Structure -> app-> Dependencies -> 点击"+"号 -> LibraryDepencies 搜索recyclerview,找到下图高亮的library

supportrecyclerview

波纹效果

波纹效果通过 drawable文件,有个系统自带的 ripple 元素
注意:系统的波纹效果需要 API 21以上的支持。

创建 drawable 文件:

drawable/main_touchedbackground.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:color="@color/grey"
    tools:targetApi="lollipop">
    <item android:drawable="@color/white" />
</ripple>

配置<ripple>元素,其中 color 属性是点击时触发的颜色,而 <item> 配置的是正常状态时显示的颜色。(grey:#D7D7D7, white:#FAFAFA)。

创建 item,并应用到 Layout 中:

layout/item_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:orientation="horizontal"
    android:clickable="true"
    android:background="@drawable/main_touchedbackground">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:gravity="center_vertical" />
</LinearLayout>

<LinearLayout> 中配置 android:clickableandroid:background属性,clickable不设置为true的话会没有点击效果。

在主 Layout 中添加 <RecyclerView> 元素

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context="com.example.recyclerviewdemo.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:id="@+id/recyclerView">

    </android.support.v7.widget.RecyclerView>
</LinearLayout>
创建一个继承自 RecyclerView.Adapter 的类:

com.example.recyclerviewdemo.CustomAdapter

public class CustomAdapter extends RecyclerView.Adapter{

    private String[] items;

    public CustomAdapter(String[] items) {
        this.items = items;
    }

    //显示图片
    public class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;

        public ItemViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.textView);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_main, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            ((ItemViewHolder) holder).textView.setText(items[position]);
        }
    }

    @Override
    public int getItemCount() {
        return items.length;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }
}
MainActivity的配置

com.example.recyclerviewdemo.MainActivity

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private CustomAdapter adapter;
    private String[] text = {"Java", "C", "C#", "C++", "PHP", "Ruby", "R", "Swift", "Groovy",
            "Objective-C", "Lisp"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CustomAdapter(text);
        recyclerView.setAdapter(adapter);
    }
}
ripple

添加分隔线

有两种方法,一种是直接使用 DividerItemDecoration,它也是继承 RecyclerView.ItemDecoration 的。
接下来添加分隔线需要重写装饰类 RecyclerView.ItemDecoration,画分隔线:
com.example.recyclerviewdemo.CustomItemDecoration

public class CustomItemDecoration extends RecyclerView.ItemDecoration {
    public static final String TAG = "decoration";
    private Drawable dividerLine;

    public CustomItemDecoration(Context context) {
        dividerLine = context.getDrawable(R.drawable.main_divider);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - left;

        int childViewCount = parent.getChildCount();
        for (int i = 0; i < childViewCount; i++) {
            View childView = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) childView.getLayoutParams();
            //top = childView's bottom + childView's end margin
            int top = childView.getBottom() + params.getMarginEnd();
            //bottom = top + 分隔线的真实高度
            int bottom = top + dividerLine.getIntrinsicHeight();

            //画分隔线
            dividerLine.setBounds(left, top, right, bottom);
            dividerLine.draw(c);
        }
    }

    //重写此方法,防止设置的波纹背景把分隔线覆盖掉。
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //除最后一行外,向下位移分隔线
        int currentItemPosition = parent.getChildAdapterPosition(view);
        int endItemPosition = parent.getAdapter().getItemCount() - 1;
        if (currentItemPosition != endItemPosition ) {
            outRect.bottom = dividerLine.getIntrinsicHeight();
        }
    }
}

相当于是通过各个角的位置来画一个矩形。

自定义的分隔线

drawable/main_divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="1px"
        android:height="1px" />
    <solid android:color="#FF424242" />
</shape>
代码中添加配置的 ItemDecoration

com.jkxy.recyclerviewdemo.MainActivity 中添加代码

recyclerView.addItemDecoration(new CustomItemDecoration(this));
最终的显示效果:
finall

相关文章

网友评论

    本文标题:RecyclerView:模仿ListVIew的点击波纹效果和分

    本文链接:https://www.haomeiwen.com/subject/ovvhettx.html