1. 程式人生 > >EasyRecyclerView,簡單易上手,支援多種常用的功能的RecyclerView

EasyRecyclerView,簡單易上手,支援多種常用的功能的RecyclerView

引包

compile 'com.jude:easyrecyclerview:4.4.2'

使用方法

xml中使用

<com.jude.easyrecyclerview.EasyRecyclerView
  android:id="@+id/recyclerView"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  app:layout_empty="@layout/view_empty"
  app:layout_progress="@layout/view_progress"
app:layout_error="@layout/view_error" app:recyclerClipToPadding="true" app:recyclerPadding="8dp" app:recyclerPaddingTop="8dp" app:recyclerPaddingBottom="8dp" app:recyclerPaddingLeft="8dp" app:recyclerPaddingRight="8dp" app:scrollbarStyle="insideOverlay"//insideOverlay or insideInset or
outsideOverlay or outsideInset app:scrollbars="none"//none or vertical or horizontal />

程式碼中設定

void setEmptyView(View emptyView)
void setProgressView(View progressView)
void setErrorView(View errorView)

展示檢視

void showEmpty()
void showProgress()  
void showError()  
void showRecycler()

Demo:上拉下拉重新整理,載入時進度提示,錯誤提示

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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="com.junx.easyrecyclerview.MainActivity">

    <com.jude.easyrecyclerview.EasyRecyclerView
        android:id="@+id/rv"
        android:layout_width="368dp"
        android:layout_height="495dp"
        app:scrollbarStyle="insideOverlay"
        app:layout_error="@layout/view_error"

        app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent" android:layout_marginLeft="8dp"
        android:layout_marginBottom="8dp" android:layout_marginTop="8dp"/>
</android.support.constraint.ConstraintLayout>
//傳入資料的model為泛型
public class DemoViewHolder extends BaseViewHolder<Person> {
    private TextView mTv_name;
    private ImageView mImg_face;
    private TextView mTv_sign;

    public DemoViewHolder(ViewGroup parent) {
        super(parent,R.layout.item_person);
        mTv_name = $(R.id.person_name);
        mTv_sign = $(R.id.person_sign);
        mImg_face = $(R.id.person_face);
    }

    @Override
    public void setData(Person person) {
        mTv_name.setText(person.getName());
        mTv_sign.setText(person.getSign());

        Picasso.with(getContext())
                .load(person.getFace())
                .into(mImg_face);
    }
}
public class MainActivity extends AppCompatActivity{

    EasyRecyclerView rv;
    private RecyclerArrayAdapter<Person> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rv= (EasyRecyclerView) findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        //設定分割線
        DividerDecoration decoration = new DividerDecoration(Color.RED, 2, 20, 20);
        decoration.setDrawLastItem(false);
        rv.addItemDecoration(decoration);

        rv.setAdapterWithProgress(adapter=new RecyclerArrayAdapter<Person>(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                //adapter與viewholder解耦,通過viewholder
                return new DemoViewHolder(parent);
            }
        });

        //下拉重新整理
        rv.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                adapter.addAll(DataProvider.getPersonList(1));
                            }
                        });
                    }
                },2000);
            }
        });

        //上拉載入更多
        adapter.setMore(R.layout.view_more, new RecyclerArrayAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //當載入資料的size=0時,展示資料到底的檢視,並且不能在上拉重新整理
                                adapter.addAll(new ArrayList<Person>());
                            }
                        });
                    }
                },2000);
            }
        });
        //設定資料到底時的檢視
        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                Log.d("meee",getClass()+":\n"+"onErrorShow:");
//                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                Log.d("meee",getClass()+":\n"+"onErrorClick:");
//                adapter.resumeMore();
            }
        });
        //通過該方法展示error檢視
        rv.showError();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
              runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      adapter.pauseMore();
                      Log.d("meee",getClass()+":\n"+"暫停");
                  }
              });
            }
        },2000);

    }
}

插入 更新 移除資料

        //新增item出現的動畫
        rv.setItemAnimator(new DefaultItemAnimator());
        //插入資料 移除資料 更新資料
                adapter.insert(data,position);
                adapter.remove(position);
                adapter.update(data,position);

新增頭佈局和腳佈局

        adapter.addHeader(new RecyclerArrayAdapter.ItemView() {
            @Override
            public View onCreateView(ViewGroup parent) {
                TextView tv = new TextView(MainActivity.this);
                tv.setText("你好啊");
                return tv;
            }

            @Override
            public void onBindView(View headerView) {
            }
        });
        adapter.addFooter(new RecyclerArrayAdapter.ItemView() {
            @Override
            public View onCreateView(ViewGroup parent) {
                TextView tv = new TextView(MainActivity.this);
                tv.setText("你好啊");
                return tv;
            }

            @Override
            public void onBindView(View headerView) {

            }
        });

新增粘性頭佈局

public class StickyHeaderAdapter implements StickyHeaderDecoration.IStickyHeaderAdapter<StickyHeaderAdapter.HeaderHolder> {

    private LayoutInflater mInflater;

    public StickyHeaderAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public long getHeaderId(int position) {
        return position / 3;
    }

    @Override
    public HeaderHolder onCreateHeaderViewHolder(ViewGroup parent) {
        final View view = mInflater.inflate(R.layout.header_item, parent, false);
        return new HeaderHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(HeaderHolder viewholder, int position) {
        viewholder.header.setText("第"+getHeaderId(position)+"組");
    }

    class HeaderHolder extends RecyclerView.ViewHolder {
        public TextView header;

        public HeaderHolder(View itemView) {
            super(itemView);
            header = (TextView) itemView;
        }
    }
}
//R.layout.header_item
<?xml version="1.0" encoding="utf-8"?>

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#e6e6e6"
    android:paddingLeft="16dp"
    android:gravity="center_vertical"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000">

</TextView>
        StickyHeaderDecoration stickyHeaderDecoration = new StickyHeaderDecoration(new StickyHeaderAdapter(this));
        stickyHeaderDecoration.setIncludeHeader(false);
        rv.addItemDecoration(stickyHeaderDecoration);