1. 程式人生 > >RecyclerView重新整理與載入更多

RecyclerView重新整理與載入更多

   序言:上一篇述說了android架構層,本篇主要學習RecyclerView重新整理與載入,原理就是重新整理使用的是SwipeRefireshLayout,而載入更多是採用RecyclerView的滾動監聽(addOnScrollListener),

      1.第一步當然是先寫佈局啦

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/srf_mains" android:layout_width="wrap_content" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_ab" android
:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_blue_bright"/> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>

其中SwipeRefireshLayout是谷歌原生的重新整理控制元件,所以現在好多軟體都採用這種效果來重新整理資料,RecyclerView是

ListView和GridView的更新迭代產品,雖然程式碼相對複雜點,沒有setonImtemOnclickListener,只能在itemview設定或者設定回撥,但卻相對靈活,也很強大,能實現橫向,縱向,瀑布流等效果。

2.業務邏輯

public class Main2Activity extends AppCompatActivity {
    private List<String> list = new ArrayList<>();
MyViewAdapter myViewAdapter;
SwipeRefreshLayout srf_mains;
RecyclerView rv_ab;
@TargetApi(Build.VERSION_CODES.M)
    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//初始化檢視
initView();
//業務邏輯
initData();
}



    private void initView() {
        srf_mains = (SwipeRefreshLayout) findViewById(R.id.srf_mains);
rv_ab = (RecyclerView) findViewById(R.id.rv_ab);
}
    private void initData() {
        //給集合建立假資料
for (int i=0;i<20;i++){
            list.add(""+i+"條資料");
}
        //設定recyclerview的樣式,這裡採用縱向線性樣式
rv_ab.setLayoutManager(new LinearLayoutManager(this));
//建立介面卡
myViewAdapter = new MyViewAdapter();
//設定設配器
rv_ab.setAdapter(myViewAdapter);
//SwipeRefreshLayout設定重新整理監聽,這裡模擬集合資料清空,重新新增
srf_mains.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
public void onRefresh() {
                if (list.size()>0){
                    list.clear();
                    for (int i=0;i<20;i++){
                        list.add(""+i+"條資料");
}
                }
                myViewAdapter.notifyDataSetChanged();
srf_mains.setRefreshing(false);
ToastUtils.show(Main2Activity.this,"重新整理成功");
}
        });
//recyclerview設定滾動監聽,當到達最底部時分頁載入資料,重新整理介面卡
rv_ab.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState == RecyclerView.SCROLL_STATE_IDLE){
                    list.add(list.size()-1,"底部");
myViewAdapter.notifyDataSetChanged();
}
            }

        });
}
3.最後粘上adapter的程式碼
private class MyViewAdapter extends RecyclerView.Adapter<MyViewAdapter.MyViewHolder> {
    @Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType==0){
            return new MyViewHolder(LayoutInflater.from(Main2Activity.this).inflate(R.layout.item_view, parent, false));
}
        return new MyViewHolder(LayoutInflater.from(Main2Activity.this).inflate(R.layout.item_views, parent, false));
}

    @Override
public int getItemViewType(int position) {
        if (position==list.size()-1){
            return 1;
}
        return 0;
}

    @Override
public void onBindViewHolder(MyViewHolder holder, int position) {
        if (position!=list.size()-1){
            holder.tv_iteme.setText(list.get(position));
}


    }

    @Override
public int getItemCount() {
        return list.size();
}

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_iteme;
        public MyViewHolder(View itemView) {
            super(itemView);
tv_iteme = (TextView) itemView.findViewById(R.id.tv_iteme);
}
    }
}
總結:我採用的是分型別的adapter,一部分是資料顯示部分,底部是載入更多顯示,通過對recyclerview的滾動監聽,當滑動到底部來載入更多,也就是分頁顯示,重新整理部分採用的是官方的控制元件。