XRecyclerView和萬能baseeAdapter 實現上拉下拉重新整理列表

寒江雪語發表於2016-11-14


XRecyclerView : https://github.com/jianghejie/XRecyclerView

萬能baseAdaper: https://github.com/hongyangAndroid/baseAdapter


新增依賴:

compile 'com.zhy:base-rvadapter:3.0.3'
compile 'com.zhy:base-adapter:3.0.3'
compile 'com.jcodecraeer:xrecyclerview:1.2.7'
activity_recycler1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:id="@+id/recyclerview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

item_comment.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/shape_white">

    <ImageView
        android:id="@+id/user_logo"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:scaleType="centerInside"
        android:src="@mipmap/girl" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/user_logo"
        android:layout_alignTop="@id/user_logo"
        android:layout_toRightOf="@id/user_logo">

        <TextView
            android:id="@+id/user_name"
            style="@style/TextTheme"
            android:layout_marginLeft="8dip"
            android:text="歲月撫傷" />

        <TextView
            android:id="@+id/item_public_time"
            style="@style/TextTheme"
            android:layout_below="@id/user_name"
            android:layout_marginLeft="8dip"
            android:text="12點"
            android:textColor="#888888"
            android:textSize="15dp" />
    </RelativeLayout>

    <TextView
        android:id="@+id/item_index_comment"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@id/user_logo"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/user_logo"
        android:scaleType="centerInside"
        android:text="1樓" />

    <TextView
        android:id="@+id/content_text"
        style="@style/TextTheme"
        android:layout_below="@id/user_logo"
        android:layout_margin="4dp"
        android:text="十年之前,我不認識你 你不屬於我,我們還是一樣,陪在一個陌生人左右,走過漸漸熟悉的街頭;十年之後,我們是朋友, 還可以問候,只是那種溫柔,再也找不到擁抱的理由,情人最後難免淪為朋友。" />

    <TextView
        android:id="@+id/item_action_comment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/content_text"
        android:layout_marginBottom="4dip"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
        android:layout_marginTop="4dip"
        android:drawableLeft="@mipmap/ic_action_edit"
        android:gravity="center"
        android:text="回覆" />

    <TextView
        android:id="@+id/item_action_love"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/content_text"
        android:layout_marginBottom="4dip"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
        android:layout_marginTop="4dip"
        android:layout_toLeftOf="@id/item_action_comment"
        android:drawableLeft="@mipmap/ic_action_love"
        android:gravity="center"
        android:text="985贊"
        android:textColor="#888888" />
</RelativeLayout>

Comment.java:

package com.example.superrecyclerviewtest;

/**
 * Created by Keen on 11/14/2016.
 */

public class Comment {

    private String userName;
    private String contentText;
    private String indexComment;
    private String loveCount;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getContentText() {
        return contentText;
    }

    public void setContentText(String contentText) {
        this.contentText = contentText;
    }

    public String getIndexComment() {
        return indexComment;
    }

    public void setIndexComment(String indexComment) {
        this.indexComment = indexComment;
    }

    public String getLoveCount() {
        return loveCount;
    }

    public void setLoveCount(String loveCount) {
        this.loveCount = loveCount;
    }
}

testrecycleradapter.java:

package adapter;

import android.content.Context;

import com.example.superrecyclerviewtest.Comment;
import com.example.superrecyclerviewtest.R;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

/**
 * Created by Keen on 11/13/2016.
 */

public class TestRecyclerAdapter extends CommonAdapter<Comment> {


    public TestRecyclerAdapter(Context context, int layoutId, List<Comment> datas) {
        super(context, layoutId, datas);
    }

    @Override
    protected void convert(ViewHolder holder, Comment s, int position) {

        holder.setText(R.id.content_text, s.getContentText());
        holder.setText(R.id.item_index_comment, s.getIndexComment());
        holder.setText(R.id.item_action_love, s.getLoveCount());
        holder.setText(R.id.user_name, s.getUserName());
    }
}

MainActivity.java:

package com.example.superrecyclerviewtest;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhy.adapter.recyclerview.CommonAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import adapter.DividerItemDecoration;
import adapter.TestRecyclerAdapter;

public class MainActivity extends AppCompatActivity {

    private XRecyclerView mRecyclerView;
    private TestRecyclerAdapter adapter;
    private List<Comment> mDatas;
    private int refreshTime = 0;
    private int times = 0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview1);
        mRecyclerView = (XRecyclerView)findViewById(R.id.recyclerview1);

        initDatas();

        LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        adapter = new TestRecyclerAdapter(MainActivity.this, R.layout.item_coment, mDatas);
        mRecyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener()
        {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position)
            {
                Toast.makeText(MainActivity.this, "pos = " + position, Toast.LENGTH_SHORT).show();
                //adapter.notifyItemRemoved(position);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position)
            {
                return false;
            }
        });

        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //refresh data here
                refreshTime ++;
                times = 0;
                new Handler().postDelayed(new Runnable(){
                    public void run() {


                        mDatas.clear();
                        for(int i = 0; i < 15 ;i++){
                            Comment comment = new Comment();
                            comment.setContentText("這是重新整理內容的測試文字");
                            comment.setUserName("重新整理測試");
                            comment.setLoveCount(Integer.toString(i*10) + "贊");
                            comment.setIndexComment(Integer.toString(i) + "樓");
                            Log.w("COMMENT", comment.getIndexComment());
                            mDatas.add(comment);
                        }
                        adapter.notifyDataSetChanged();
                        mRecyclerView.refreshComplete();
                    }

                }, 1000);            //refresh data here
            }

            @Override
            public void onLoadMore() {
                // load more data here
                new Handler().postDelayed(new Runnable(){
                    public void run() {

                        int size = mDatas.size();
                        for(int i = 0; i < 15 ;i++){
                            Comment comment = new Comment();
                            comment.setContentText("這是自動載入的內容測試文字");
                            comment.setUserName("自動載入");
                            comment.setLoveCount(Integer.toString(size + i + 1) + "贊");
                            comment.setIndexComment(Integer.toString(size + i + 1) + "樓");
                            Log.w("COMMENT", comment.getIndexComment());
                            mDatas.add(comment);
                        }
                        mRecyclerView.loadMoreComplete();
                        adapter.notifyDataSetChanged();
                    }
                }, 1000);
            }
        });


    }


    private void initDatas(){

        mDatas = new ArrayList<Comment>();

        for (int i = 1; i <= 10; i++){
            Comment comment = new Comment();
            comment.setContentText("十年之前,我不認識你 你不屬於我,我們還是一樣,陪在一個陌生人左右,走過漸漸熟悉的街頭;十年之後,我們是朋友, 還可以問候,只是那種溫柔,再也找不到擁抱的理由,情人最後難免淪為朋友。");
            comment.setUserName("十年之戀");
            comment.setIndexComment(Integer.toString(i) + "樓");
            Log.w("COMMENT", comment.getIndexComment());
            comment.setLoveCount(Integer.toString(i*10) + "贊");
            mDatas.add(comment);
        }
    }
}

效果如下:


相關文章