ListView的三種適配器 ArrayAdapter 支持list集合,只能展現一行文字 SimpleAdapter 具有擴展性,可以自定義多種效果 BaseAdapter 抽象類,繼承這個類並且重寫相關方法 常用屬性 android:divider 設置分割線的顏色或圖片 android:div ...
ListView的三種適配器
ArrayAdapter 支持list集合,只能展現一行文字
SimpleAdapter 具有擴展性,可以自定義多種效果
BaseAdapter 抽象類,繼承這個類並且重寫相關方法
常用屬性
android:divider 設置分割線的顏色或圖片
android:divider 分割線
android:scrollbars="none" 不顯示滾動條
android:fadingEdge="none" 去掉上邊和下邊黑色的陰影
android:fastScrollEnable="true" 設置快速滾動
android:drawSelectorOnTop="true" 設置長按試背景色高亮
android:stackFromBottom="true" 顯示列表在最下麵
android:cacheColorHint=“#000000” 設置背景時消除拖動的黑色陰影
android:headerDividersEnabled=“false” 不會在頁眉畫分割線
android:footerDividersEnabled=“false” 不會在頁腳畫分割線
android:descendantFocusability="blocksDescendants" 給子view設置,取消子控制項的焦點
ArrayAdapter
1.系統自帶佈局
simple_list_item_1 : 單獨一行的文本框,常用
simple_list_item_2 : 兩個文本框組成
simple_list_item_checked : 每項都是由一個已選中的列表項 慎用
imple_list_item_multiple_choice : 都帶有一個覆選框 慎用
simple_list_item_single_choice : 都帶有一個單選鈕 慎用
setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL) 設置多選狀態 慎用
2.實例
listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,new String[]{"元旦","清明","勞動節","端午節","中秋節"}));
SimpleAdapter
List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>(); for (int i = 0; i < idCard.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", idCard[i]); map.put("sex", "sex"+i); map.put("number","number"+i); list.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, mList, R.layout.item_listview, new String[] { "name", "sex","number" },
newint[] {R.id.listview_item_name,R.id.listview_item_sex,R.id.listview_item_number });
自定義BaseAdapter模板
class MyAdapter extends BaseAdapter { private Context context = null; public MyAdapter(Context context) { this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder mHolder; if (convertView == null) { mHolder = new ViewHolder(); LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.item_listview, null, true); mHolder.text_item_listview_username = (TextView) convertView.findViewById(R.id.listview_item_name); mHolder.text_item_listview_email = (TextView) convertView.findViewById(R.id.listview_item_sex); mHolder.imageView_item_listview_headpic = (ImageView) convertView.findViewById(R.id.listview_item_number); convertView.setTag(mHolder); } else { mHolder = (ViewHolder) convertView.getTag(); } String name = list.get(position).get("name").toString(); String sex = list.get(position).get("sex").toString(); String number = list.get(position).get("number").toString();
mHolder.listview_item_name.setText(name); mHolder.listview_item_sex.setText(sex); mHolder.listview_item_number.setText(number); return convertView; } class ViewHolder { private TextView listview_item_name; private TextView listview_item_sex; private TextView listview_item_number; } }
實現上拉刷新功能的ListView
1.自定義Adapter
public class MyListView extends ListView implements OnScrollListener{ private FootLayout flayout; private Scroller scroller; private ListViewListener lvListener; private float mlastY; private int itemConut=-1; private int PULL_LOAD_MORE_DELTA=150; private int SCROLL_DURATION=400; public MyListView(Context context) { super(context); initView(context); // TODO Auto-generated constructor stub } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); // TODO Auto-generated constructor stub } public MyListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); // TODO Auto-generated constructor stub } public void initView(Context context){ super.setOnScrollListener(this); scroller=new Scroller(context, new DecelerateInterpolator()); flayout= new FootLayout(context); flayout.setState(FootLayout.STATE_NO); flayout.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub statrView(); } }); } @Override public void setAdapter(ListAdapter adapter) { // TODO Auto-generated method stub addFooterView(flayout); super.setAdapter(adapter); } public void statrView(){ flayout.setState(FootLayout.STATE_LOADING); if(lvListener!=null){ lvListener.onLoadMore(); } } public void stopView(){ flayout.setState(FootLayout.STATE_NO); } @Override public boolean onTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mlastY=ev.getRawY(); break; case MotionEvent.ACTION_MOVE: final float deltaY=ev.getRawY()-mlastY; mlastY=ev.getRawY(); if(getLastVisiblePosition()==itemConut-1){ updateFooterHeight(-deltaY); } break; default: if (getLastVisiblePosition() == itemConut - 1) { // 調用載入更多 if (flayout.getBottomMargin() > PULL_LOAD_MORE_DELTA) { statrView();// 如果底部視圖高度大於可以載入高度,那麼就開始載入 } resetFooterHeight();// 重置載入更多視圖高度 } break; } return super.onTouchEvent(ev); } private void updateFooterHeight(float arg0){ int height=flayout.getBottomMargin()+(int)arg0; if (height > PULL_LOAD_MORE_DELTA) { flayout.setState(FootLayout.STATE_READY); } else { flayout.setState(FootLayout.STATE_NO); } flayout.setBottomMargin(height); } private void resetFooterHeight() { int bottomMargin = flayout.getBottomMargin(); if (bottomMargin > 0) { scroller.startScroll(0, bottomMargin, 0, -bottomMargin, SCROLL_DURATION); invalidate(); } } @Override public void computeScroll() { if (scroller.computeScrollOffset()) { flayout.setBottomMargin(scroller.getCurrY()); postInvalidate(); } super.computeScroll(); } public interface ListViewListener { public void onLoadMore(); } public void setListViewListener(ListViewListener l) { lvListener=l; } @Override public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub itemConut=arg3; } @Override public void onScrollStateChanged(AbsListView arg0, int arg1) { // TODO Auto-generated method stub } }
2.底部FootLayout
public class FootLayout extends LinearLayout{ static int STATE_READY=0,STATE_LOADING=1,STATE_NO=2; private RelativeLayout rlayout; private TextView tv; private ProgressBar pb; public FootLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub initView(context); } public FootLayout(Context context) { super(context); // TODO Auto-generated constructor stub initView(context); } public void initView(Context context){ LinearLayout layout=(LinearLayout) LayoutInflater.from(context).inflate(R.layout.foot_view, null); addView(layout); layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); rlayout=(RelativeLayout) layout.findViewById(R.id.foot_rlayout); tv=(TextView) layout.findViewById(R.id.foot_text); pb=(ProgressBar) layout.findViewById(R.id.foot_progressbar); } public void hide(){ rlayout.setVisibility(View.VISIBLE); } public void show(){ rlayout.setVisibility(View.INVISIBLE); } public void setState(int state){ tv.setVisibility(View.INVISIBLE); pb.setVisibility(View.INVISIBLE); if(state==STATE_READY){ tv.setVisibility(View.VISIBLE); tv.setText("鬆開顯示更多"); }else if(state==STATE_LOADING){ pb.setVisibility(View.VISIBLE); }else{ tv.setVisibility(View.VISIBLE); tv.setText("載入更多"); } } public void setBottomMargin(int height) { if (height < 0) return; LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rlayout .getLayoutParams(); lp.bottomMargin = height; rlayout.setLayoutParams(lp); } public int getBottomMargin() { LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rlayout .getLayoutParams(); return lp.bottomMargin; } }
3.底部FootLayout的佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:id="@+id/foot_rlayout" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/foot_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="載入更多" android:layout_centerInParent="true" /> <ProgressBar android:id="@+id/foot_progressbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:verticalScrollbarPosition="defaultPosition" android:visibility="invisible" /> </RelativeLayout> </LinearLayout>
4.使用MyListView
public class MainActivity extends Activity implements IXListViewListener{ private ListViews mylists; private SimpleAdapter mAdapter; private List<Map<String,Object>> mlist; private Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mylists =(ListViews) findViewById(R.id.myListView); mlist=new ArrayList<Map<String,Object>>(); mAdapter = new SimpleAdapter(MainActivity.this, getData(), R.layout.list_item, new String[] { "name", "img", "content" }, new int[] { R.id.title, R.id.mImage, R.id.content }); mylists.setAdapter(mAdapter); mylists.setPullLoadEnable(true); mylists.setXListViewListener(this); mHandler = new Handler(); } String data[] = new String[] { "勇士", "火箭", "湖人", "凱爾特人", "雷霆" }; String data1[] = new String[] { "湯普森", "哈登", "詹姆斯", "塔圖姆", "維斯布魯克" }; private List<Map<String, Object>> getData() { for (int i = 0; i < data.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", data[i]); map.put("content", data1[i]); map.put("img", R.drawable.ic_launcher); mlist.add(map); } return mlist; } // 載入更多 @Override public void onLoadMore() { mHandler.postDelayed(new Runnable() { @Override public void run() { getData(); mAdapter.notifyDataSetChanged(); onLoad(); } }, 1500); } private void onLoad() { mylists.stopLoadMore(); } }