zoukankan      html  css  js  c++  java
  • UltimateRecyclerView的用法具体解释

    近期在用非常多第三方库的时候,发现有一些附带的demo写的不是非常全面或者样例的代码太多,凝视太少,要想使用还要去看下源代码什么的(。。

    。用第三方开源库不就是想节省时间嘛)。所以决定每周两到三篇。写一些比較热门的库的用法。让使用的人能高速上手(对我而言,要是发现库提供的功能有时不满足需求。可能会扩展下功能什么的)。不废话,以后都是直接贴代码。代码中写满凝视,这样看起来的时候方便,我也省事。
    (嫌麻烦的直接下拉到第5)

    效果图

    效果图
    1.这是使用到的布局文件。非常easy。看成时仅仅有一个UltimateRecyclerView即可 activity_recycler_view.xml

    
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="github.dzc.apptemplate.recyclerview.RecyclerViewActivity">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/action_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="RecyclerViewActivity"
                    android:gravity="center"
                    android:textColor="@android:color/white"/>
                </android.support.v7.widget.Toolbar>
    
        </android.support.design.widget.AppBarLayout>
    
        <com.marshalchen.ultimaterecyclerview.UltimateRecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/action_bar"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context="github.dzc.apptemplate.recyclerview.RecyclerViewActivity"
            ></com.marshalchen.ultimaterecyclerview.UltimateRecyclerView>
    
    
    </RelativeLayout>
    
    
    

    2.头部布局 仅仅有一张图片 head_view.xml

    
    <?

    xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/img" android:scaleType="centerCrop"> </ImageView>

    3.item的布局 string_item.xml

    
    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp">
    
    </TextView>
    
    

    4.ViewHolder

    
    package github.dzc.apptemplate.recyclerview.viewholder;
    
    import android.view.View;
    import android.widget.TextView;
    
    import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
    
    /**
     * Created by dzc on 16/1/22.
     */
    public class StringViewHolder extends UltimateRecyclerviewViewHolder{
        public TextView tv;
        public StringViewHolder(View itemView,boolean isItem) {
            super(itemView);
            if(isItem){
                tv = (TextView) itemView;
            }
        }
    }
    
    

    5.重点来了。全部凝视都在这里

    
    
    package github.dzc.apptemplate.recyclerview;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.design.widget.FloatingActionButton;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.Toolbar;
    import android.support.v7.widget.helper.ItemTouchHelper;
    import android.view.LayoutInflater;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.SimpleAdapter;
    
    import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
    import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;
    import com.marshalchen.ultimaterecyclerview.itemTouchHelper.SimpleItemTouchHelperCallback;
    import com.marshalchen.ultimaterecyclerview.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
    
    import java.util.List;
    
    import butterknife.Bind;
    import butterknife.ButterKnife;
    import github.dzc.apptemplate.BaseActivity;
    import github.dzc.apptemplate.R;
    import github.dzc.apptemplate.recyclerview.viewholder.StringViewHolder;
    
    public class RecyclerViewActivity extends BaseActivity {
    
        @Bind(R.id.recycler_view)
        UltimateRecyclerView recyclerView;
    
        View headerView;
        private StringAdapter adapter;
        private Handler handler;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_recycler_view);
            ButterKnife.bind(this);
            handler = new Handler();
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            headerView = LayoutInflater.from(this).inflate(R.layout.head_view,null);
            adapter = new StringAdapter(data);
            recyclerView.setAdapter(adapter);
    
            //为每一个item加入头部的布局   这里运用到的事实上就是RecyclerView.ItemDecoration
            //没错  可能一般都是用这个来实现item之间的分隔线的  可是线也是一个view,但这个view够大的时候,就是一个头部了
            StickyRecyclerHeadersDecoration stickyRecyclerHeadersDecoration = new StickyRecyclerHeadersDecoration(adapter);
            recyclerView.addItemDecoration(stickyRecyclerHeadersDecoration);
    
    
            ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter){
                //这种方法还有别的方法能够重载  能够控制如滑动删除等功能
    
    
                @Override
                public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                    final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;//控制拖动的方向   这里设置了智能上下拖动交换位置
                    final int swipeFlags = ItemTouchHelper.LEFT ;//控制滑动删除的方向  这里设置了仅仅能左滑删除
    //                final int swipeFlags = ItemTouchHelper.LEFT |ItemTouchHelper.RIGHT;//左右滑删除
    
                    return makeMovementFlags(dragFlags, swipeFlags);
                }
    
                @Override
                public boolean isItemViewSwipeEnabled() {
                    return super.isItemViewSwipeEnabled();//这里控制开启或关闭item能否够滑动删除的功能
                }
    
                @Override
                public boolean isLongPressDragEnabled() {
                    return super.isLongPressDragEnabled();//控制长按拖动功能
                }
            };
    
            final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
            itemTouchHelper.attachToRecyclerView(recyclerView.mRecyclerView);
            //设置头部一定要在setAdapter后面,由于这个操作会调用adapter的方法来显示头部,假设adapter为null,则出错
            recyclerView.setParallaxHeader(headerView);
            recyclerView.enableDefaultSwipeRefresh(true);//开启下拉刷新
            recyclerView.enableLoadmore();//开启上拉载入很多其它
            recyclerView.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            recyclerView.setRefreshing(false);
                        }
                    },2000);
                }
            });
        }
    
        class StringAdapter extends UltimateViewAdapter<StringViewHolder>{
            private List<String> stringList;
    
            public StringAdapter(List<String> stringList) {
                this.stringList = stringList;
            }
    
            @Override
            public StringViewHolder getViewHolder(View view) {
                return new StringViewHolder(view,false);
                //这个getViewHolder方法在内部实现中仅仅有在获取头部、载入很多其它、下拉刷新的时候会调用
                //直接设置itemView为GONE,所以不须要初始化什么的。仅仅要返回的是个ViewHolder即可
            }
    
    
            @Override
            public StringViewHolder onCreateViewHolder(ViewGroup parent) {
                View view  = LayoutInflater.from(RecyclerViewActivity.this).inflate(R.layout.string_item,null);
                return new StringViewHolder(view,true);
            }
    
    
    
            @Override
            public int getAdapterItemCount() {
                return stringList==null?0:stringList.size();
                //这里返回的是你的item的个数  不包含头部和载入view
            }
    
            @Override
            public long generateHeaderId(int position) {
    //            if (getItem(position).length() > 0)
    //                return getItem(position).charAt(0);
    //            else return -1;
                if(customHeaderView!=null){
                    position-=1;
                }
                String s = position+"";
                return s.charAt(0);
                //为每一项item生成头部的View。假设返回-1。则不生成,假如多个连续的item返回同一个id,
                //则仅仅会生成一个头部View
                //这里提取position的第一个数作为id
                //1 10 11 12 14等返回的id是一样的  为1
            }
    
            @Override
            public void onBindViewHolder(StringViewHolder holder, int position) {
                //一定要加这个推断  由于UltimateRecyclerView本身有加了头部和尾部  这种方法返回的是包含头部和尾部在内的
                if (position < getItemCount() && (customHeaderView != null ? position <= stringList.size() : position < stringList.size()) && (customHeaderView != null ? position > 0 : true)) {
                    position  -= customHeaderView==null?0:1;
                    holder.tv.setText(stringList.get(position));
                }
            }
    
            @Override
            public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
                View view  = LayoutInflater.from(RecyclerViewActivity.this).inflate(R.layout.string_item,null);
                return new StringViewHolder(view,true);
                //初始化item的头部布局  这里为了方便 就直接用StringViewHolder,实际使用能够使用不同于item的布局
            }
    
            @Override
            public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
                if(customHeaderView!=null){
                    position-=1;
                }
                ((StringViewHolder)holder).tv.setText("header  "+(position+"").charAt(0));
                ((StringViewHolder)holder).tv.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                //绑定item头部view的数据,这里提取每一个view的position的第一个数来作为头部显示数据
                //即10 11 12  13  14这些  返回的是1
                //20 21 22等是2
            }
    
            @Override
            public void onItemMove(int fromPosition, int toPosition) {
                swapPositions(data,fromPosition,toPosition);
                //假设开启的拖动移动位置的功能
                //要重写这种方法  由于假设不重写  交换的仅仅是view的位置,数据的位置没有交换 一拖动。就会变成原来的样子
                super.onItemMove(fromPosition, toPosition);
            }
    
            @Override
            public void onItemDismiss(int position) {
                remove(data,position);//控制删除的
                super.onItemDismiss(position);
            }
        }
    
    }
    
    
    

    代码我放在了github
    https://github.com/duzechao/AppTemplate

  • 相关阅读:
    jq使用手册
    MVC4 Forms 验证,部署到Hyper-v上.FormsAuthentication.SetAuthCookie无效的问题
    ASP.NET FormsAuthentication跨站点登录时绝对地址返回的问题
    FireBird.conf 常用参数说明 (FrieBird3.0.4版)
    AnkhSVN2019
    控制台程序的RedirectStandardOutput
    代替webbrowser的插件:CefSharp
    mssql分区视图
    Firebird写存储过程
    .net项目多个目标架构
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7256110.html
Copyright © 2011-2022 走看看