編輯:關於Android編程
無論是android還是iOS,列表視圖應該是最復雜的控件了,android中的listview從命名可以看出是個一維數組,而iOS中的tableview則是二維數組,但其實需要注意的地方是差不多的,都是重用機制,這是考量你對listview能否掌握的最好的方法。
常見的listview的初始化以及設置適配器的代碼如下:
ListView listView; MyAdapter listAdapter; ArrayListlistString; listView = (ListView)this.findViewById(R.id.listview); listString = new ArrayList (); for(int i = 0 ; i < 100 ; i++) { listString.add(Integer.toString(i)); } listAdapter = new MyAdapter(this); listView.setAdapter(listAdapter); } class MyAdapter extends BaseAdapter{ Context mContext; LinearLayout linearLayout = null; LayoutInflater inflater; public MyAdapter(Context context) { // TODO Auto-generated constructor stub mContext = context; inflater = LayoutInflater.from(mContext); } @Override public int getCount() { // TODO Auto-generated method stub return listString.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return listString.get(arg0); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } public final class ViewHolder{ public ImageView img; public TextView title; public TextView info; public Button viewBtn; } public class MyAdapter extends BaseAdapter{ private LayoutInflater mInflater; public MyAdapter(Context context){ this.mInflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return mData.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder=new ViewHolder(); convertView = mInflater.inflate(R.layout.vlist2, null); holder.img = (ImageView)convertView.findViewById(R.id.img); holder.title = (TextView)convertView.findViewById(R.id.title); holder.info = (TextView)convertView.findViewById(R.id.info); holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn); convertView.setTag(holder); }else { holder = (ViewHolder)convertView.getTag(); } holder.img.setBackgroundResource((Integer)mData.get(position).get("img")); holder.title.setText((String)mData.get(position).get("title")); holder.info.setText((String)mData.get(position).get("info")); holder.viewBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showInfo(); } }); return convertView; } }
@Override public void setAdapter(ListAdapter adapter) { if (null != mAdapter) { mAdapter.unregisterDataSetObserver(mDataSetObserver); } resetList(); mRecycler.clear(); if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0) { mAdapter = new HeaderViewListAdapter(mHeaderViewInfos, mFooterViewInfos, adapter); } else { mAdapter = adapter; } mOldSelectedPosition = INVALID_POSITION; mOldSelectedRowId = INVALID_ROW_ID; if (mAdapter != null) { mAreAllItemsSelectable = mAdapter.areAllItemsEnabled(); mOldItemCount = mItemCount; mItemCount = mAdapter.getCount(); checkFocus(); mDataSetObserver = new AdapterDataSetObserver(); mAdapter.registerDataSetObserver(mDataSetObserver); mRecycler.setViewTypeCount(mAdapter.getViewTypeCount()); int position; if (mStackFromBottom) { position = lookForSelectablePosition(mItemCount - 1, false); } else { position = lookForSelectablePosition(0, true); } setSelectedPositionInt(position); setNextSelectedPositionInt(position); if (mItemCount == 0) { // Nothing selected checkSelectionChanged(); } } else { mAreAllItemsSelectable = true; checkFocus(); // Nothing selected checkSelectionChanged(); } if (mCheckStates != null) { mCheckStates.clear(); } requestLayout(); }
AdapterDataSetObserver
是用來存儲數據的,我們看一下裡面的代碼,有個成員變量
mDataSetObserver
就是這個對象申明在listview的父類AbsListView(源碼在此)中
那麼這個AdapterDataSetObserver究竟是個什麼東西呢,我們還是到AbsListView的父類AdapterView(源碼在此)中一探究竟吧。
class AdapterDataSetObserver extends DataSetObserver { private Parcelable mInstanceState = null; @Override public void onChanged() { mDataChanged = true; mOldItemCount = mItemCount; mItemCount = getAdapter().getCount(); // Detect the case where a cursor that was previously invalidated has // been repopulated with new data. if (AdapterView.this.getAdapter().hasStableIds() && mInstanceState != null && mOldItemCount == 0 && mItemCount > 0) { AdapterView.this.onRestoreInstanceState(mInstanceState); mInstanceState = null; } else { rememberSyncState(); } checkFocus(); requestLayout(); } @Override public void onInvalidated() { mDataChanged = true; if (AdapterView.this.getAdapter().hasStableIds()) { // Remember the current state for the case where our hosting activity is being // stopped and later restarted mInstanceState = AdapterView.this.onSaveInstanceState(); } // Data is invalid so we should reset our state mOldItemCount = mItemCount; mItemCount = 0; mSelectedPosition = INVALID_POSITION; mSelectedRowId = INVALID_ROW_ID; mNextSelectedPosition = INVALID_POSITION; mNextSelectedRowId = INVALID_ROW_ID; mNeedSync = false; checkSelectionChanged(); checkFocus(); requestLayout(); } public void clearSavedState() { mInstanceState = null; } }
這是一個抽象類,不多說了。
未完待續。。。
基於Android 6.0的源碼剖析, 分析Binder IPC通信的權限控制方法clearCallingIdentity和restoreCallingId
1、開發Android硬件抽象層代碼 ~/android-2.3_r1/hardware/libhardware ----include
給庫添加Cocoapods支持, 使這個工具使用起來更加方便, 更好的使用Cocoapods, 助力iOS程序開發, 下面進入正題, 想要實現這個過程, 絕對不虛此讀.首
前言:Vibrator簡介: 下面我們就來寫個簡單的例子,來熟悉下這個Vibrator的用法!1.獲得Vibrator實例:Vibrator vb = (Vib