Android UI (一) : ListView、RecyclerView
阿新 • • 發佈:2020-12-31
技術標籤:Android
大家好,我是OB!
這是Android
的UI系列之ListView
、RecyclerView
一:ListView
列表佈局
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
initData();
ListView listView = findViewById(R.id.my_listview) ;
listView.setAdapter(new MyAdapter(dataArray,this));
}
設定Adapter
class MyAdapter extends BaseAdapter {
ArrayList arrayList = null;
Context context;
MyAdapter(ArrayList arrayList, Context context) {
this.arrayList = arrayList;
this.context = context;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType (int position) {
if (position % 2 == 0) {
return 0;
} else {
return 1;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("TAG", "getCount: "+arrayList.size());
MyHolderOne holderOne;
MyHolderTwo holderTwo;
if (convertView == null) {
if (getItemViewType(position) == 1) {
convertView = LayoutInflater.from(context).inflate(R.layout.cell, parent, false);
holderOne = new MyHolderOne();
holderOne.textView = (TextView)convertView.findViewById(R.id.title_name);
convertView.setTag(R.id.title_name,holderOne);
holderOne.textView.setText((String)arrayList.get(position));
} else {
convertView = LayoutInflater.from(context).inflate(R.layout.cell_header, parent, false);
holderTwo = new MyHolderTwo();
holderTwo.textView = (TextView)convertView.findViewById(R.id.title_header);
convertView.setTag(R.id.title_header,holderTwo);
holderTwo.textView.setText((String)arrayList.get(position));
}
} else {
if (getItemViewType(position) == 1) {
holderOne = (MyHolderOne) convertView.getTag(R.id.title_name);
holderOne.textView.setText((String)arrayList.get(position));
} else {
holderTwo = (MyHolderTwo) convertView.getTag(R.id.title_header);
holderTwo.textView.setText((String)arrayList.get(position));
}
}
return convertView;
}
class MyHolderOne {
TextView textView;
}
class MyHolderTwo {
TextView textView;
}
}
getItemViewType
的返回值必須從 0 開始,不然會陣列越界,當返回多個型別的item時,getItemViewType()
和getViewTypeCount()
必須實現
二:RecyclerView
RecyclerView常用的佈局分三種
// 線性佈局 --類似於ListView
recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL,false));
// 網格佈局 --瀑布流佈局
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
// 錯落網格佈局 --瀑布流佈局
recyclerView.setLayoutManager(new
StaggeredGridLayoutManager(3,RecyclerView.VERTICAL));
這也是StaggeredGridLayoutManager
public class OBRecycleViewActivity extends AppCompatActivity {
ArrayList dataArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_o_b_recycle_view);
initData();
RecyclerView recyclerView = findViewById(R.id.ob_recyclerveiw);
// 線性佈局 --類似於ListView
recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL,false));
// 網格佈局 --瀑布流佈局
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
// 錯落網格佈局 --瀑布流佈局
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,RecyclerView.VERTICAL));
recyclerView.setAdapter(new OBRecyclerAdapter(dataArray, this));
// 水平方向分割線
recyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.HORIZONTAL));
// 豎直方向分割線
recyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.VERTICAL));
}
private void initData () {
dataArray = new ArrayList<String>();
dataArray.add("ListView");
dataArray.add("RecycleView");
dataArray.add("ViewPager");
// ....
}
private class OBRecyclerAdapter extends RecyclerView.Adapter {
ArrayList datas;
Context context;
OBRecyclerAdapter(ArrayList datas, Context context) {
this.datas = datas;
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == 0) {
View v = LayoutInflater.from(context).inflate(R.layout.rv_cell_header, parent, false);
RVHolderHeader rvHolder = new RVHolderHeader(v);
rvHolder.textView = v.findViewById(R.id.header_tv);
return rvHolder;
} else {
View v = LayoutInflater.from(context).inflate(R.layout.recycler_view_cell, parent, false);
RVHolder rvHolder = new RVHolder(v);
rvHolder.textView = v.findViewById(R.id.content_tv);
return rvHolder;
}
}
@Override
public int getItemViewType(int position) {
if (position % 2 == 0){
return 0;
} else {
return 1;
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder.getClass() == RVHolderHeader.class) {
RVHolderHeader header = (RVHolderHeader) holder;
header.textView.setText((String) datas.get(position));
} else {
RVHolder rvHolder = (RVHolder) holder;
rvHolder.textView.setText((String) datas.get(position));
}
}
@Override
public int getItemCount() {
return datas==null?0:datas.size();
}
}
private class RVHolderHeader extends RecyclerView.ViewHolder {
TextView textView;
public RVHolderHeader(@NonNull View itemView) {
super(itemView);
}
}
private class RVHolder extends RecyclerView.ViewHolder {
TextView textView;
public RVHolder(@NonNull View itemView) {
super(itemView);
}
}
}
其中adapter.notifyDataSetChanged();
可以用來更新資料