美文网首页android开发Android知识程序员
利用RecyclerView实现自己的类似于Expandable

利用RecyclerView实现自己的类似于Expandable

作者: sakasa | 来源:发表于2017-02-15 15:52 被阅读3732次

关于RecyclerView总感觉有许多要说的,但是又不知道从什么地方说起,今天这个不是我的第一篇关于RecyclerView上的文章,但是是我第一篇发表的,起个引子作用:
公司在项目中要实现一个类似于expandablelistview效果的控件,但是我已经开始使用recyclerview,不想用listview,然后就走上了踩坑之路了。

源码地址
最开使用的是国外培训机构出品的一个控件,但是感觉很繁琐,实现起来有问题,就自己重写了一个实现,这篇文章简单的介绍代码,没有将原理。

看一下效果图:

上代码:

1. 首先来看一下布局文件

fragment_history.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

    </data>

    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.saka.myapplication.Fragment.HistoryFragment">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="历史上的今天"
            android:textSize="30sp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="20sp"/>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_history"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


    </LinearLayout>
</layout>

假如不熟悉Databinding的朋友可以去看看我的另一篇文章

DataBinding(一)-初识

布局文件很简单,有用的只有这个recyclerview,其他的都没用。

2. 布局文件的java代码

这里是一些常规化的代码。

 /**
     * 初始化RecyclerView
     */
    private void initViews() {
        binding.rvHistory.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rvHistory.setItemAnimator(new NoAlphaItemAnimator());
        adapter = new HistoryAdapter(getActivity(), results);
        adapter.setItemClickListener(this);
        binding.rvHistory.setAdapter(adapter);
        startRequest();
    }

首先是setlayoutManager,此处我用的是垂直的布局。

然后是setItemAnimator,这个是重点,同学们要好好记。

然后是adapter,这个是实现的具体方法。一会重点讲。

startrequest是请求数据的方法,用来更新数据。

3. 看一下item的布局文件(稍微复杂)

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="historymodel"
            type="com.saka.myapplication.Models.HistoryModle.HistoryResult" />
    <variable
        name="historyclickevent"
        type="com.saka.myapplication.Models.SearchItemClick"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:onClick="@{()->historyclickevent.onClickItem()}"
            android:text="@{historymodel.title}"
            android:textColor="@color/textwhite"
            android:textSize="20sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="@{historymodel.showDetails}">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="时间:"
                    android:textColor="@color/colorAccent"
                    android:textSize="15sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{historymodel.date}"
                    android:textSize="15sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="农历:"
                    android:textColor="@color/colorAccent"
                    android:textSize="15sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{historymodel.lunnar}"
                    android:textSize="15sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="事件:"
                    android:textColor="@color/colorAccent"
                    android:textSize="15sp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@{historymodel.des}"
                    android:textSize="15sp" />
                    
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</layout>

生成的布局文件大概是这样的:

共包含三个部分,首先是标题部分,就是那个浅蓝色,虽然没什么内容都没有,但是请求到内容后就会显示文字了。

下面部分 时间、农历、事件 是需要隐藏的部分。

4 HistoryHolder内容

此处要注意databing的写法。上边的链接有详细介绍

static class HistoryHolder extends RecyclerView.ViewHolder {
        ViewDataBinding binding;
        public HistoryHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        static HistoryHolder createViewHolder(ViewGroup container, int layoutId) {
            ViewDataBinding binding = DataBindingUtil.inflate(
                    LayoutInflater.from(container.getContext()), layoutId, container, false);
            return new HistoryHolder(binding);
        }
    }

这个viewholer是一个通用的holder,是传参数传进来item布局文件的id int layoutId

5. HistoryAdapter文件

关于adapter要复写的几个方法主要讲这个onBindViewHoler方法:

 @Override
    public void onBindViewHolder(HistoryHolder holder, int position) {
        holder.binding.setVariable(BR.historymodel, historyResults[position]);
        searchItemClick=new SearchItemClick(position);
        searchItemClick.setListener(listener);
        holder.binding.setVariable(BR.historyclickevent,searchItemClick);
        holder.binding.executePendingBindings();

    }

这个地方的historymodel对应item布局文件中的variable中的
name="historymodel" type="com.saka.myapplication.Models.HistoryModle.HistoryResult" />

这个searchItemClick是一个点击事件,用来传递position参数

在Historyfragment实现了这个接口,用来隐藏/显示detail(详细内容)。

 @Override
    public void onItemClick(int resId) {
        Log.d("fff", "点击了事件" + resId);
        if (results[resId].getShowDetails() == View.GONE) {
            Log.d("fff", "显示条目");
            results[resId].setShowDetails(View.VISIBLE);
        } else {
            Log.d("fff", "隐藏条目");
            results[resId].setShowDetails(View.GONE);
        }
        adapter.notifyItemChanged(resId);
    }

此时打开应用可以看到这个时候已经可以简单的扩展收缩了

但是看清楚,在现实detail的时候效果还可以,但是隐藏detail的时候就非常蛋疼了,是下边的item完全上去之后detail的内容才会隐藏,这就很难受了。

这个时候要研究一下recyclerview的动画机制。在你不给recyclerview添加动画时,adapter.notifyItemChanged()方法会出发recyclerview的动画,一般有个默认的DefaultItemAnimator()动画。我们要做的就是重写这类,做成我们想要的动画效果。

我新建了一个动画类叫NoAlphaItemAnimator,此处我要实现的是在点击后改变item的高度,而不是删除或者添加,我们需要找到这个内部类,private static class ChangeInfo。
它就是在改变item时调用的动画,然后找到void animateChangeImpl(final ChangeInfo changeInfo)这个方法,找到里边的实现方法 public void onAnimationStart(View view)
在下边添加

 if (view.findViewById(history_more).getVisibility() == View.GONE) {
                        AlphaAnimation animation = new AlphaAnimation(0, 1);
                        animation.setDuration(300);
                        view.findViewById(history_more).startAnimation(animation);
                    } else {
                        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
                        alphaAnimation.setDuration(300);
                        view.findViewById(history_more).startAnimation(alphaAnimation);
                        view.findViewById(history_more).setVisibility(View.GONE);
                    }

添加如上代码,就可以实现隐藏了。
看一下效果:


相关文章

网友评论

    本文标题:利用RecyclerView实现自己的类似于Expandable

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