两种布局的ListVIew Adapter。例如微信对话界面
这个界面 实现的不是微信对话界面。实现的是,focus的状态下,变为放大的另一种布局
重点:
一、定义类型个数
private final int TYPE_COUNT = 2;
private final int FIRST_TYPE = 0;
private final int OTHERS_TYPE = 1;
二、重写两个函数
@Override
public int getViewTypeCount() {
return TYPE_COUNT;
}
@Override
public int getItemViewType(int position) {
if (position == mSelectedPosition) {
return FIRST_TYPE;
} else {
return OTHERS_TYPE;
}
}
public class CallContactListViewAdapter extends BaseAdapter {
public static final String TAG = CallContactListViewAdapter.class.getName();
private final int TYPE_COUNT = 2;
private final int BIG_TYPE = 0;
private final int SMALL_TYPE = 1;
private int currentType;
private List<ContactIndex> mContactList;
private Context mContext;
private LayoutInflater layoutInflater;
private int mSelectedPosition = -1;
private Bitmap mBitmap = null;
// private Matrix mMatrix;
public CallContactListViewAdapter(Context context,
List<ContactIndex> contactList) {
mContext = context;
mContactList = contactList;
this.layoutInflater = LayoutInflater.from(context);
// mMatrix = new Matrix();
// mMatrix.postScale(0.6f,0.6f);
}
public void setSelectItem(int position) {
mSelectedPosition = position;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getCount()
*/
@Override
public int getCount() {
// Log.d(TAG, "contact list size:" + mContactList.size());
return mContactList.size();
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItem(int)
*/
@Override
public Object getItem(int position) {
if (position >= 0 && position < mContactList.size()) {
return mContactList.get(position);
// return mList.get(position % mList.siez());
}
return null;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public int getViewTypeCount() {
return TYPE_COUNT;
}
@Override
public int getItemViewType(int position) {
if (position == mSelectedPosition) {
return BIG_TYPE;
} else {
return SMALL_TYPE;
}
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
Log.d(TAG, "position:" + position);
ViewHolder viewHolder = null;
currentType = getItemViewType(position);
System.out.println("type=" + currentType);
if (currentType == BIG_TYPE) {
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.call_item, null);
viewHolder.friendPic = (ImageView) convertView
.findViewById(R.id.callFriendPic);
viewHolder.friendName = (TextView) convertView
.findViewById(R.id.callFriendName);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
} else {
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.call_item_small,
null);
viewHolder.friendPic = (ImageView) convertView
.findViewById(R.id.callFriendPic);
viewHolder.friendName = (TextView) convertView
.findViewById(R.id.callFriendName);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
}
// if(mSelectedPosition == position){
// viewHolder.friendPicBorder.setVisibility(View.VISIBLE);
// viewHolder.friendName.setTextSize(mContext.getResources().getDimension(R.dimen.callListNameTextSizeBig));
// }
// else{
// mBitmap = BitmapFactory.decodeResource(mContext.getResources(),
// mContactList.get(position).getPicId());
// Bitmap resizeBmp =
// Bitmap.createBitmap(mBitmap,0,0,mBitmap.getWidth(),mBitmap.getHeight(),mMatrix,true);
// viewHolder.friendPic.setImageBitmap(resizeBmp);
// }
viewHolder.friendPic.setImageResource(mContactList.get(position)
.getPicId());
viewHolder.friendName.setText(mContactList.get(position).getUserName());
return convertView;
}
private static class ViewHolder {
ImageView friendPic;
TextView friendName;
}
}
三、TV 版本中,ListView 首尾循环相接的方法:
即,到了最后一个,再按向下键的时候,跳到第一个
到了第一个,再按向上键的时候,跳到最后一个
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_DOWN:
if (mOldPosition == mContactListAdapter.getCount() - 1) {
mCallContactListContainer.setSelection(0);
}
break;
case KeyEvent.KEYCODE_DPAD_UP:
if (mOldPosition == 0) {
mCallContactListContainer.setSelection(mContactListAdapter
.getCount() - 1);
}
break;
}
return super.onKeyDown(keyCode, event);
}
如果是触屏,涉及到滚动,就比较麻烦
四、 Adapter中自定义的方法 用来在Activity中,一个处于focus状态的item的position
public void setSelectItem(int position) {
mSelectedPosition = position;
}
Activity中,给ListView设置选中的Listener
mListView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView,
View view, int position, long arg3) {
mMyAdapter.setSelectItem(position);
mMyAdapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});