聊天的時(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>