ListView是android里面用到很多的一个控件,这使用起来比较简单,又很实用。今天要讲的就是在Activity进而 重写Adapter的Button事件,废话不多说,就直接开始了 1、创建一个适配器FindAdapter.java public class FindAdapter extends CursorAdapter implements OnClickListener{ private Context context;//this context private OnItemFindClick onItemFindClick; public FindAdapter(Cursor cursor){ super( IM.im, cursor, FLAG_REGISTER_CONTENT_OBSERVER ); } public View getView(int pos, View convertView, ViewGroup parent) { return renderUser(pos,convertView,parent); } // 将分割线放在上面,利于判断 public View renderUser(int pos,View view,ViewGroup parent){ FindHolder findHolder = null; if(view == null){ view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.tt_item_find, parent,false); findHolder = new FindHolder(view); findHolder.btnRefuse.setOnClickListener(this); findHolder.btnRefuse.setTag(pos);//这里很容易忘记 findHolder.btnAccept.setOnClickListener(this); findHolder.btnAccept.setTag(pos); view.setTag(findHolder); }else{ //ant findHolder = (FindHolder)view.getTag(); } Cursor cursor = (Cursor)getItem(pos); String accountStr = cursor.getString(cursor.getColumnIndex(ContactProvider.ContactColumns.ACCOUNT)); String nameStr = cursor.getString(cursor.getColumnIndex(ContactProvider.ContactColumns.NAME)); findHolder.tvAccount.setText(accountStr+""); findHolder.tvName.setText(nameStr); findHolder.ivcAvatar.setImageDrawable(IM.getAvatar(accountStr)); return view; } public void setOnItemFindClick(OnItemFindClick onItemFindClick){ this.onItemFindClick = onItemFindClick; } class FindHolder{ TextView tvAccount; TextView tvName; ImageViewCircle ivcAvatar; Button btnRefuse; Button btnAccept; public FindHolder(View v){ tvAccount = (TextView)v.findViewById(R.id.tt_item_find_account); tvName = (TextView)v.findViewById(R.id.tt_item_find_name); ivcAvatar = (ImageViewCircle)v.findViewById(R.id.tt_item_find_icon); btnRefuse = (Button)v.findViewById(R.id.tt_item_find_refuse); btnAccept = (Button)v.findViewById(R.id.tt_item_find_accept); } } public interface OnItemFindClick{ public void onAcceptClick(View v); public void onRefuseClick(View v); } public void onClick(View v) { switch(v.getId()){ case R.id.tt_item_find_accept://答应请求 onItemFindClick.onAcceptClick(v); break; case R.id.tt_item_find_refuse://拒绝请求 onItemFindClick.onRefuseClick(v); break; } } public void bindView(View arg0, Context arg1, Cursor arg2) {} public View newView(Context arg0, Cursor arg1, ViewGroup arg2) { return null; } } 2.当然需要一个布局哦!这里是我的布局tt_item_find.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" > <com.chat.ui.widget.ImageViewCircle android:id="@+id/tt_item_find_icon" android:layout_width="45dp" android:layout_height="45dp" android:layout_marginLeft="10dp" android:scaleType="centerCrop" android:src="@drawable/tt_login_logo" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginLeft="10dp" > <TextView android:id="@+id/tt_item_find_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/contact_realname_color" android:textSize="17sp" /> <TextView android:id="@+id/tt_item_find_account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/contact_name_color" android:textSize="12sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="right" android:orientation="horizontal" android:layout_marginRight="10dp"> <Button android:id="@+id/tt_item_find_accept" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:background="@drawable/tt_show_head_toast_bg" android:text="@string/tt_item_find_accept" android:textColor="@color/contact_realname_color" android:paddingLeft="5dp" android:paddingRight="5dp"/> <Button android:id="@+id/tt_item_find_refuse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/tt_show_head_toast_bg" android:text="@string/tt_item_find_refuse" android:textColor="@color/contact_realname_color" android:paddingLeft="5dp" android:paddingRight="5dp"/> </LinearLayout> </LinearLayout> 3.现在item开始了,就开始写Activity了 public class FindActivity extends Activity implements OnItemFindClick{ private ListView list; private FindAdapter findAdapter; private OnItemFindClick onItemFindClick; private Cursor cursor; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initPatent(); init(); initData(); } //cursor的数据自己去加载吧 private void initData(){} private void init(){ context = this; list = (ListView)contentView.findViewById(R.id.tt_activity_find_list); onItemFindClick = this; findAdapter = new FindAdapter(cursor); findAdapter.setOnItemFindClick(onItemFindClick); list.setAdapter(findAdapter); } //接受好友请求 public void onAcceptClick(View v) { Log.e("findActivity-->","accept"); Log.e("find---》",""+ v.getTag()); final Cursor cursor = (Cursor)findAdapter.getItem((Integer) v.getTag()); } //拒绝好友请求 public void onRefuseClick(View v) { Log.e("findActivity-->","refuse"); } } 4.activitty的布局自己去写吧!好了就完事了。
时间: 2024-10-08 10:13:59