1. 二級列表(ExpandableListView)
①創(chuàng)建布局并找控件
②獲取數據:網絡數據脆贵、死數據
③創(chuàng)建適配器:10個方法必須掌握
④設置適配器
⑤父項凛膏、子項的點擊事件
※setOnChildClickListener
//單擊子選項
※setOnGroupClickListener
//單擊組選項
⑥ExpandableListView的基本屬性
※divider
這個屬性用于設置父選項之間的分割線樣式
※childDivider
自選項之間分割樣式
※dividerHeight
用于設置分割線的高度
※groupIndicator
這個屬性是用于控制父項前面的小箭頭用的
ExpandableListView適配器的10個方法
//父項目的個數
@Override
public int getGroupCount() {
return list.size();
}
//某個父項的子項的個數
@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getChildren().size();
}
//獲得某個父項
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
//獲得某個子項
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.get(groupPosition).getChildren().get(childPosition);
}
//父項的id
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//子項的id
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
//獲得父項的view
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
convertView= LayoutInflater.from(context).inflate(R.layout.layout_group,null);
TextView tv_group = convertView.findViewById(R.id.tv_group);
tv_group.setText(list.get(groupPosition).getName());
return convertView;
}
//獲得子項的view
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
convertView= LayoutInflater.from(context).inflate(R.layout.layout_child,null);
TextView tv_child = convertView.findViewById(R.id.tv_child);
tv_child.setText(list.get(groupPosition).getChildren().get(childPosition).getName());
return convertView;
}
//子項是否可選中,如果要設置子項的點擊事件,需要返回true
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
2. ListView手動宗侦、自動加載更多
⑴ListView
①創(chuàng)建布局并找控件
②獲取網絡數據
③創(chuàng)建適配器:兩個優(yōu)化(必會)
④設置適配器
ListView優(yōu)化:
⑴對convertView
進行復用
⑵使用ViewHolder
減少findViewById
的查找
⑵ListView手動加載更多
①創(chuàng)建布局并找控件
②獲取網絡數據
③創(chuàng)建適配器:兩個優(yōu)化(必會)
④設置適配器
⑤給ListView
添加一個Footer
⑥點擊footer
中的button
加載更多:page++
香罐,獲取數據
View footer = LayoutInflater.from(this).inflate(R.layout.button, null);
Button btn_add = footer.findViewById(R.id.btn_add);
lv.addFooterView(footer);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
page++;
initData();
adapter.notifyDataSetChanged();
}
});
⑶ListView自動加載更多
①創(chuàng)建布局并找控件
②獲取網絡數據
③創(chuàng)建適配器:兩個優(yōu)化(必會)
④設置適配器
⑤定義一個變量isBottom
表示是否滑到底部
⑥ListView
設置滑動監(jiān)聽
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (isBottom){
page++;
initData();
isBottom=false;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (totalItemCount>0&&totalItemCount==firstVisibleItem+visibleItemCount){
isBottom=true;
}else {
isBottom=false;
}
}
});