下拉加載更多的聊天數(shù)據(jù)更新

聊天的時(shí)候會(huì)遇到下拉加載更多消息,并且定位到加載的數(shù)據(jù)最后一條,這里用recyclerview來(lái)寫一個(gè)簡(jiǎn)單的demo,完成下拉加載更多數(shù)據(jù),并且定位到加載的數(shù)據(jù)的最后一條

github代碼鏈接 (https://github.com/yukunkun/TextDemo/tree/master/app/src/main/java/com/yukun/textapplication/chat)

布局文件很簡(jiǎn)單

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/sr_refresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_chat"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout>

下面是activity里的代碼

public class ChatListActivity extends AppCompatActivity {

    @BindView(R.id.rv_chat)
    RecyclerView mRvChat;
    @BindView(R.id.sr_refresh)
    SwipeRefreshLayout mSrRefresh;
    private List<String> mList=new ArrayList<>();
    private RecyclerItemAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_list);
        ButterKnife.bind(this);
        init();
        getaddInfo();
        setListener();
    }
    private void init() {
        mSrRefresh.setRefreshing(true);
        LinearLayoutManager mLayoutManager=new LinearLayoutManager(this);
        mRvChat.setLayoutManager(mLayoutManager);
        mAdapter = new RecyclerItemAdapter(mList,getApplicationContext());
        mRvChat.setAdapter(mAdapter);
    }

    private void setListener() {

        mSrRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getaddInfo();
            }
        });
    }

    private void getaddInfo() {
        //新的數(shù)據(jù)
        List<String> mListNew=new ArrayList<>();
        for (int j = 1; j <=20;j++) {
            mListNew.add("第"+j+"個(gè)!!");
        }
        mListNew.add("加載更多的底部");
        //從頭插入數(shù)據(jù),原來(lái)的保留
        mList.addAll(0,mListNew);

        mSrRefresh.setRefreshing(false);
        mAdapter.addMesasge(mList);
        mAdapter.notifyDataSetChanged();
        mRvChat.smoothScrollToPosition(mListNew.size()-1);
    }
}

東西其實(shí)很少,就是幾個(gè)常用的方法

主要是下面的這幾句完成加載更多的數(shù)據(jù),并且定位

 //新的數(shù)據(jù)
            List<String> mListNew=new ArrayList<>();
            for (int j = 1; j <=20;j++) {
                mListNew.add("第"+j+"個(gè)!!");
            }
            mListNew.add("加載更多的底部");
            //從頭插入數(shù)據(jù),原來(lái)的保留
            mList.addAll(0,mListNew);
    
            mSrRefresh.setRefreshing(false);
            mAdapter.addMesasge(mList);
            mAdapter.notifyDataSetChanged();
            //定位到加載的最后一條數(shù)據(jù)
            mRvChat.smoothScrollToPosition(mListNew.size()-1);

adapter和常用的沒(méi)啥區(qū)別,代碼貼出來(lái),為了完整性

public class RecyclerItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    List<String> mList;
    Context context;

    public RecyclerItemAdapter(List<String> list, Context context) {
        mList = list;
        this.context = context;
    }
    public void addMesasge(List<String> mList){
        this.mList=mList;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.recycler_item,null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((MyHolder)holder).mTextView.setText(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
    class MyHolder extends RecyclerView.ViewHolder {
        TextView mTextView;
        public MyHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.tv_content);
        }
    }
}

adapter的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:text=""
        android:textSize="20dp"
        android:textColor="#70f57b"
        android:gravity="center"
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="30dp"/>
    <View
        android:background="#ff8888"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"/>

</LinearLayout>

這里就OK了,雖然簡(jiǎn)單,下次遇到了才能更好的完成.留個(gè)筆記.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容