無論是iOS開發(fā)還是Android開發(fā),列表都是很常用的功能,幾乎沒有哪一個(gè)app是沒有列表的,那么今天我就將我這兩天學(xué)習(xí)的列表知識(shí)記錄下來,與君共勉!
我是iOS中途過來開發(fā)Android的,對比UITableView來學(xué)習(xí)會(huì)簡單很多
RecyclerView配合SpringView實(shí)現(xiàn)列表加下拉上拉
RecyclerView和SpringView布局代碼
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".message.fragment.MessageFragment">
<!--消息模塊列表的上拉和下拉-->
<com.liaoinstan.springview.widget.SpringView
android:id="@+id/springView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/messageListView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</com.liaoinstan.springview.widget.SpringView>
</FrameLayout>
有內(nèi)容的item布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!--標(biāo)題-->
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333333"
android:textSize="16sp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="100dp"
android:lines="1"
android:text="新資源提醒"
/>
<TextView
android:id="@+id/timeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:textColor="#b3b3b3"
android:textSize="13sp"
android:gravity="right"
android:text="12月23日"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"/>
<TextView
android:id="@+id/contentTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/titleTextView"
android:layout_marginTop="10dp"
android:textColor="#b3b3b3"
android:textSize="13sp"
android:text=""
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
/>
<!--底部分割線-->
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_below="@+id/contentTextView"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dp"
android:background="#e6e6e6"/>
</RelativeLayout>
無數(shù)據(jù)item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_centerInParent="true"
android:textSize="20dp"
android:textColor="#333333"
android:id="@+id/emptyText"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</TextView>
</RelativeLayout>
我們在fragment中獲取RecyclerView對象
recyclerView = contentView.findViewById(R.id.messageListView);
給recyclerView賦值(有數(shù)據(jù))
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> map01 = new HashMap<>();
map01.put("title","title-01");
map01.put("content","內(nèi)容");
list.add(map01);
Map<String,Object> map02 = new HashMap<>();
map02.put("title","title-02");
map02.put("content","內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容");
list.add(map02);
Map<String,Object> map03 = new HashMap<>();
map03.put("title","title-03");
map03.put("content","內(nèi)容");
list.add(map03);
Map<String,Object> map04 = new HashMap<>();
map04.put("title","title-04");
map04.put("content","內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容");
list.add(map04);
Map<String,Object> map05 = new HashMap<>();
map05.put("title","title-05");
map05.put("content","內(nèi)容");
list.add(map05);
Map<String,Object> map06 = new HashMap<>();
map06.put("title","title-06");
map06.put("content","內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容");
list.add(map06);
Map<String,Object> map07 = new HashMap<>();
map07.put("title","title-07");
map07.put("content","內(nèi)容");
list.add(map07);
Map<String,Object> map08 = new HashMap<>();
map08.put("title","title-08");
map08.put("content","內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容");
list.add(map08);
Map<String,Object> map09 = new HashMap<>();
map09.put("title","title-09");
map09.put("content","內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容");
list.add(map09);
messageAdapter = new MessageAdapter(list,getContext());
recyclerView.setAdapter(messageAdapter);
//線性
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);

haveData.jpg
給recyclerView賦值(無數(shù)據(jù))
messageAdapter = new MessageAdapter(new ArrayList<Map<String,Object>>(),getContext());
recyclerView.setAdapter(messageAdapter);
//線性
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);

noData.jpg
adapter
package com.example.jizhigang.crm_android_j.message.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.jizhigang.crm_android_j.R;
import java.util.List;
import java.util.Map;
public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private int EMPTY_ITEM = 0; //無數(shù)據(jù)
private int DEFAULT_ITEM = 1; //有數(shù)據(jù)
private List<Map<String,Object>> messages;
private Context context;
public MessageAdapter(List<Map<String,Object>> messages, Context context){
this.messages = messages;
this.context = context;
}
/*給item一個(gè)唯一標(biāo)識(shí)*/
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
/*如果是多種item,那么在這里判斷是哪一種item*/
@Override
public int getItemViewType(int position) {
if(messages.size() == 0){//展示無數(shù)據(jù)的item
return EMPTY_ITEM;
}else if(messages.size() > 0) {
return DEFAULT_ITEM;
}else {
return super.getItemViewType(position);
}
}
/*根據(jù)item類型去獲取對應(yīng)種類的item對象*/
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if(i==EMPTY_ITEM){//無數(shù)據(jù)item
View emptyView = LayoutInflater.from(context).inflate(R.layout.empty_recycler,viewGroup,false);
return new EmptyHolder(emptyView);
}else {//有數(shù)據(jù)item
//獲取cell對象
View itemView = LayoutInflater.from(context).inflate(R.layout.message_view_item,viewGroup,false);
return new MessageHolder(itemView);
}
}
/*給對應(yīng)種類的item賦值*/
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolder instanceof EmptyHolder){ //顯示空item
EmptyHolder emptyHolder = (EmptyHolder) viewHolder;
emptyHolder.textView.setText("暫無數(shù)據(jù)");
}else if (viewHolder instanceof MessageHolder){//顯示正常item
MessageHolder messageHolder = (MessageHolder) viewHolder;
//獲取model對象
Map<String,Object> map = messages.get(i);
String title = map.get("title").toString();
String content = map.get("content").toString();
//根據(jù)model對象給cell的組件賦值
messageHolder.titleTextView.setText(title);
messageHolder.contentTextView.setText(content);
}
}
/*獲取item數(shù)量*/
@Override
public int getItemCount() {
if (messages.size()>0){
//獲得cell的數(shù)量
return messages.size();
}else { //顯示無數(shù)據(jù)item
return 1;
}
}
/*有數(shù)據(jù)的holder*/
public class MessageHolder extends RecyclerView.ViewHolder{
private TextView titleTextView;
private TextView contentTextView;
//itemView實(shí)際上就是cell對象
public MessageHolder(@NonNull View itemView) {
super(itemView);
//獲得cell對象上的組件
titleTextView = (TextView) itemView.findViewById(R.id.titleTextView);
contentTextView = (TextView) itemView.findViewById(R.id.contentTextView);
}
}
/*無數(shù)據(jù)的空item*/
public class EmptyHolder extends RecyclerView.ViewHolder{
private TextView textView;
public EmptyHolder(@NonNull View itemView) {
super(itemView);
textView = (TextView)itemView.findViewById(R.id.emptyText);
}
}
}
需要注意的是getItemCount方法,沒有數(shù)據(jù)要返回1
/*獲取item數(shù)量*/
@Override
public int getItemCount() {
if (messages.size()>0){
//獲得cell的數(shù)量
return messages.size();
}else { //顯示無數(shù)據(jù)item
return 1;
}
}

pull.jpg

push.jpg