RecyclerView 加载更多与 CoordinatorLayout 的简单实现

ruptins 8年前
   <p>RecyclerView 加载更多与 CoordinatorLayout 的简单实现</p>    <p>在2015年Google的年度大会上,已经升级了新的Support Library,视觉效果及用户体验毋庸置疑,所以今天主要简单主要介绍Material Design中最主要的二个控件,RecyclerView的上拉加载更多和CoordinatorLayout的结合使用,废话不多说,先上图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/a418df718b9874723aecd5a27d5cb40a.gif"></p>    <h3><strong>CoordinatorLayout</strong></h3>    <p>CoordinatorLayout 从字面上理解,它是一个协调view,主要处理子view之间的相互依赖关系,增加交互体验,通过layoutDependsOn()来判断子view(child)是否依赖dependency,通过onDependentViewChanged()来判断子view(child)在所依赖view发生改变后的是否要做处理,好,我们看一下源码:</p>    <pre>  <code class="language-java">/**   * Determine whether the supplied child view has another specific sibling view as a  * layout dependency.  * /  public boolean layoutDependsOn(CoordinatorLayout parent, V child, View dependency) {              return false;          }      /**  * Respond to a change in a child's dependent view  */  public boolean onDependentViewChanged(CoordinatorLayout parent, V child, View dependency) {              return false;          }</code></pre>    <p>如果有自定义功能我们可以extends CoordinatorLayout.Behavior,今天我们介绍简单使用,后续会对CoordinatorLayout.Behavior做详细探讨:</p>    <ol>     <li> <p>首先要在Gradle中加入Library</p> </li>    </ol>    <pre>  <code class="language-java">compile 'com.android.support:design:24.2.1'</code></pre>    <ol>     <li>在布局文件中加入CoordinatorLayout,如果是新建项目的话选择Navigation Drawer Actiivty模版会包含,这里加入了开源库NavigationTabStrip对 <a href="/misc/goto?guid=4959721243376871024" rel="nofollow,noindex">demo</a> 中的viewpage做绑定</li>     <li>需要注意的是,如果要实现图片中的Toolbar隐藏效果必须加入,Toolbar其实就是我们的上面提到的子view</li>    </ol>    <pre>  <code class="language-java">app:layout_scrollFlags="scroll|enterAlways"</code></pre>    <ol>     <li> <p>加入所依赖的behavior可以用ViewPager</p> </li>    </ol>    <pre>  <code class="language-java"><android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:app="http://schemas.android.com/apk/res-auto"      android:id="@+id/vp"      android:layout_width="match_parent"      android:layout_height="match_parent"      app:layout_behavior="@string/appbar_scrolling_view_behavior"      >  </android.support.v4.view.ViewPager></code></pre>    <ol>     <li> <p>同样必须加入</p> </li>    </ol>    <pre>  <code class="language-java">app:layout_behavior="@string/appbar_scrolling_view_behavior"</code></pre>    <ol>     <li>ok,至于下拉加载更多用SwipeRefreshLayout</li>    </ol>    <pre>  <code class="language-java"><android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/swiprefreshlayout"      android:layout_width="match_parent"      android:layout_height="match_parent">        <com.recyclerviewmore.widget.RecyclerViewUpRefresh          android:id="@+id/recyclerview"          android:layout_width="match_parent"          android:layout_height="match_parent"></com.recyclerviewmore.widget.RecyclerViewUpRefresh>    </android.support.v4.widget.SwipeRefreshLayout></code></pre>    <h3><strong>RecyclerView</strong></h3>    <p>以上简单实现了CoordinatorLayout的效果下面再介绍RecyclerView的上拉加载更多,与CoordinatorLayout配合使用,其实相对与大家比较熟悉的listview来说RecyclerView更灵活,是完美的替代品,所有效果都可以自定义 布局管理(LayoutManager)下面是最简单的用法,要特殊需求需要自定义LayoutManager</p>    <ol>     <li>布局管理(LayoutManager)下面是最简单的用法,要特殊需求需要自定义LayoutManager</li>    </ol>    <pre>  <code class="language-java">mRecyclerView.setLayoutManager(newLinearLayoutManager(getActivity()));    mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));</code></pre>    <ol>     <li>设置分割线(ItemDecoration) 要extends RecyclerView.ItemDecoration</li>     <li> <p>ItemAnimator 动画效果 (同样RecyclerView 实现 RecyclerView.Adapter可以实现丰富的ui效果,上拉加载更多就是利用Adapter中的viewtype来实现)</p> </li>     <li> <p>重写RecyclerView的onScrollStateChanged</p> <pre>  <code class="language-java">@Override      public void onScrollStateChanged(int state) {          super.onScrollStateChanged(state);          if (state == RecyclerView.SCROLL_STATE_IDLE && loadMoreListener != null && !isLoadingData && canloadMore) {              LayoutManager layoutManager = getLayoutManager();              int lastVisibleItemPosition;              if (layoutManager instanceof GridLayoutManager) {                  lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();              } else if (layoutManager instanceof StaggeredGridLayoutManager) {                  int[] into = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()];                  ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(into);                  lastVisibleItemPosition = last(into);              } else {                  lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();              }                if (layoutManager.getChildCount() > 0                      && lastVisibleItemPosition >= layoutManager.getItemCount() - 1) {                  if (loadingMoreFooter != null) {                      loadingMoreFooter.setVisible();                  }                  isLoadingData = true;                  loadMoreListener.onLoadMore();              }          }      }</code></pre> </li>     <li> <p>根据viewType来显示FooterView</p> <pre>  <code class="language-java">@Override    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {      if (viewType == FOOTER) {        return new SimpleViewHolder(loadingMoreFooter);      }      return adapter.onCreateViewHolder(parent, viewType);    }</code></pre> </li>     <li> <p>绑定数据</p> <pre>  <code class="language-java">@Override  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {    holder.itemView.setOnClickListener(this);    holder.itemView.setOnLongClickListener(this);    if (adapter != null) {      int count = adapter.getItemCount();      if (position < count) {        adapter.onBindViewHolder(holder, position);        return;      }    }  }</code></pre> </li>    </ol>    <p>另外事件处理需要自定义 初来乍到,有写的不合理之处请多指正,具体简单实现见 <a href="/misc/goto?guid=4959721243376871024" rel="nofollow,noindex">demo</a></p>    <p> </p>    <p>来自:https://jiandanxinli.github.io/2016-10-18.html</p>    <p> </p>