- 浏览: 84037 次
- 性别:
- 来自: 成都
最新评论
-
284772894:
好文章,谢谢
android硬件传感器 -
康妮西:
求这个View的解释望博主联系,我的联系方式:kangnixi ...
让scrollview在滚动的过程中自动定位页的边边 -
muyu114:
你这个是啥意思啊,有什么效果没有
让scrollview在滚动的过程中自动定位页的边边
1.如果你有一个ListView,listView的 adapter 是自定义的,并且item中包含一个CheckBox 跟其他的一些组件,
如果你想通过额外一的一个CheckBox来实现select All 跟dis select all 以及 手动 选择的话,可参照下面的实现方式。
merge_list_item.xml
MergeListActivity.java
MergeListAdapter.java
实现效果:
全选
全不选
全选后再手动去除几个
如果你想通过额外一的一个CheckBox来实现select All 跟dis select all 以及 手动 选择的话,可参照下面的实现方式。
merge_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/item" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="4dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:layout_alignParentLeft="true"> <RelativeLayout android:id="@+id/avatarLayout_1" android:layout_width="60dip" android:layout_height="60dip" android:background="@drawable/frame"> <ImageView android:id="@+id/avatar_1" android:layout_width="60dip" android:layout_height="60dip" android:layout_alignParentLeft="true" android:src="@drawable/default_avatar" /> <ImageView android:id="@+id/sns_type_icon_1" android:layout_width="25dip" android:layout_height="25dip" android:paddingRight="5dip" android:paddingLeft="3dip" android:layout_alignBottom="@id/avatar_1" android:layout_alignRight="@id/avatar_1" /> </RelativeLayout> <TextView android:layout_below="@+id/item" android:layout_toRightOf="@+id/avatarLayout_1" android:layout_centerVertical="true" android:paddingLeft="1dip" android:id="@+id/name_1" android:layout_width="60dip" android:layout_height="wrap_content" android:textSize="18dip" android:layout_weight="1" android:textColor="#ffffff" android:text="Contact1" /> </LinearLayout> <CheckBox android:id="@+id/link" android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:layout_alignParentRight="true"> <TextView android:layout_below="@+id/item" android:layout_centerVertical="true" android:paddingRight="1dip" android:id="@+id/name_2" android:layout_width="60dp" android:layout_height="wrap_content" android:textSize="18dip" android:layout_weight="1" android:textColor="#ffffff" android:text="Contact2" /> <RelativeLayout android:id="@+id/avatarLayout_2" android:layout_width="60dip" android:layout_height="60dip" android:background="@drawable/frame"> <ImageView android:id="@+id/avatar_2" android:layout_width="60dip" android:layout_height="60dip" android:layout_alignParentLeft="true" android:src="@drawable/default_avatar" /> <ImageView android:id="@+id/sns_type_icon_2" android:layout_width="25dip" android:layout_height="25dip" android:paddingRight="5dip" android:paddingLeft="3dip" android:layout_alignBottom="@id/avatar_2" android:layout_alignRight="@id/avatar_2" /> </RelativeLayout> </LinearLayout> </RelativeLayout>
MergeListActivity.java
public class MergeListActivity extends Activity{ public MergeListAdapter mAdapter; ListView mergeListView; /* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.merge_list); initMergeList(); super.onCreate(savedInstanceState); } public void initMergeList(){ MyLog.println("MergeListActivity.initMergeList"); // get the list view final ListView mergeListView = ((ListView) findViewById(R.id.merge_list)); mAdapter = new MergeListAdapter(this); //add header if (mergeListView.getHeaderViewsCount() == 0){ final RelativeLayout header = (RelativeLayout) LayoutInflater.from(MergeListActivity.this) .inflate(R.layout.merge_list_header, null); mergeListView.addHeaderView(header); final CheckBox selectAll = (CheckBox)header.findViewById(R.id.selectedAll); selectAll.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //mergeListView.setSelected(true); mAdapter.setAllChecked(isChecked); } }); final Button cancel = (Button)findViewById(R.id.btn_cancel); cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); final Button ok = (Button)findViewById(R.id.btn_ok); ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mAdapter.autoMergeSelectItems(); finish(); } }); } mergeListView.setAdapter(mAdapter); } }
MergeListAdapter.java
public class MergeListAdapter extends BaseAdapter { private Context mContext; public static ArrayList<SimpleContact> similar_1; public static ArrayList<SimpleContact> similar_2; public static LayoutInflater inflater = null; private ArrayList<Boolean> checkBoxStateList = new ArrayList<Boolean>(); private ArrayList<CheckBox> ctv = new ArrayList<CheckBox>(); public MergeListAdapter(Context context) { mContext = context; inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); for(int i = 0; i < similar_1.size(); i++){ checkBoxStateList.add(i,false); } } public int getCount() { return similar_1.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } private void onClickItem(int position){ MyLog.println("MergeListActivity.onClickItem"); final int contactID1 = Integer.parseInt(similar_1.get(position).contactID); final int contactID2 = Integer.parseInt(similar_2.get(position).contactID); Intent itent = new Intent(mContext,MergeDetailActivity.class); itent.putExtra(IntentExtraName.EXTRA_PRIMARY_CONTACT_ID_NAME, contactID1); itent.putExtra(IntentExtraName.EXTRA_SECONDARY_CONTACT_ID_NAME, contactID2); mContext.startActivity(itent); } public static class ViewHolder{ public ImageView avatar_1; public ImageView avatar_2; public TextView name_1; public TextView name_2; public ImageView snsTypeIcon_1; public ImageView snsTypeIcon_2; public CheckBox checkBox; } public View getView( final int position, View convertView, ViewGroup parent) { View row = convertView; ViewHolder holder; if(convertView==null) { row = inflater.inflate(R.layout.merge_list_item, parent, false); holder = new ViewHolder(); holder.avatar_1 = (ImageView)row.findViewById(R.id.avatar_1); holder.name_1 = (TextView)row.findViewById(R.id.name_1); holder.snsTypeIcon_1 = (ImageView)row.findViewById(R.id.sns_type_icon_1); holder.checkBox = (CheckBox)row.findViewById(R.id.link); holder.avatar_2 = (ImageView)row.findViewById(R.id.avatar_2); holder.name_2 = (TextView)row.findViewById(R.id.name_2); holder.snsTypeIcon_2 = (ImageView)row.findViewById(R.id.sns_type_icon_2); holder.checkBox.setTag(position); row.setTag(holder); }else{ holder = (ViewHolder)row.getTag(); } final SimpleContact contact1 = similar_1.get(position); final SimpleContact contact2 = similar_2.get(position); final ContentResolver cr = mContext.getContentResolver(); final Bitmap avater_1 = DataOperations.getAvatar(Long.parseLong(contact1.contactID), cr); final String name_1 = contact1.displayName; final Bitmap avater_2 = DataOperations.getAvatar(Long.parseLong(contact2.contactID),cr ); final String name_2 = contact2.displayName; //avatar if (avater_1 != null){ holder.avatar_1.setImageBitmap(avater_1); }else{ holder.avatar_1.setBackgroundResource(R.drawable.default_avatar); } if (avater_2 != null){ holder.avatar_2.setImageBitmap(avater_2); }else{ holder.avatar_2.setBackgroundResource(R.drawable.default_avatar); } //name holder.name_1.setText(name_1); holder.name_2.setText(name_2); //icon of snsType if (RichContactConfig.CONTACT_TYPE_FACEBOOK == Integer.parseInt(contact1.contactType)){ holder.snsTypeIcon_1.setImageResource(R.drawable.facebook_s); }else if (RichContactConfig.CONTACT_TYPE_SINA == Integer.parseInt(contact1.contactType)){ holder.snsTypeIcon_1.setImageResource(R.drawable.sina_s); } if (RichContactConfig.CONTACT_TYPE_FACEBOOK == Integer.parseInt(contact2.contactType)){ holder.snsTypeIcon_2.setImageResource(R.drawable.facebook_s); }else if (RichContactConfig.CONTACT_TYPE_SINA == Integer.parseInt(contact2.contactType)){ holder.snsTypeIcon_2.setImageResource(R.drawable.sina_s); } ctv.add(holder.checkBox); holder.checkBox.setChecked(checkBoxStateList.get(position)); holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ checkBoxStateList.set(position, true); } else { checkBoxStateList.set(position,false); } } }); final int index = position; row.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { onClickItem(index); } }); row.setId(position); return row; } public boolean isChecked(int position) { return checkBoxStateList.get(position); } public void setAllChecked(boolean isChecked) { if(isChecked) { for(int i = 0; i < ctv.size(); i++){ ctv.get(i).setChecked(true); } }else { for(int i = 0; i < ctv.size(); i++) { ctv.get(i).setChecked(false); } } } public ArrayList<Boolean> getCheckBoxStateList() { return checkBoxStateList; } public void autoMergeSelectItems(){ MyLog.println("MergeListActivity.autoMergeSelectItems"); } }
实现效果:
全选
全不选
全选后再手动去除几个
发表评论
-
Android_SDK_NDK_JNI
2014-09-10 12:26 722Android SDK、NDK、JNI的简 ... -
如何检查apk是否签名
2012-02-22 16:46 2487Use this command : (Jarsigner i ... -
切割bitmap
2011-11-15 16:09 973Bitmap resource = BitmapFactory ... -
TextView控件属性列表
2011-10-08 15:58 1189纯属转载: http://www.cnbl ... -
元数据Meta Data
2011-09-30 16:30 1142<meta-data andr ... -
Android UI Operation in Thread
2011-09-21 17:16 1140Android UI Operation in Thread ... -
系统自带样式Android:theme
2011-06-23 15:58 2257Android应用开发——系统自带样式Android:them ... -
EditText自动获取焦点并弹出输入法的问题
2011-06-16 16:25 50431.每次启动新的Activity的时候,输入法总是弹出来,太烦 ... -
Service Broadcast BroadcastReceiver AlarmManager,Notification利用service定时/周期性发送广播
2011-05-31 16:35 38741.如题 主要实现 1.启动service,关 ... -
android硬件传感器
2011-05-30 16:07 13376纯属转载:http://dev.10086 ... -
权限permission
2011-05-27 18:03 1279纯属于转载: http://blog.csdn.net/ ... -
读写系统设置——日期与时间使用网络提供的值(时间与网络同步)
2011-05-27 17:19 2533//读_1.带默认参数的 Settings.Syste ... -
读写系统设置
2011-05-27 17:13 1810Android的很多设置想关的东西都存储在数据库中 andro ... -
Display_获取屏幕宽高
2011-05-06 12:19 975public static void intScreen(A ... -
seletctAll___利用ScrollView+RadioGroup+RadioButton实现相似互斥
2011-05-03 17:58 2091假如你有多个匹配list,在这些匹配list中存在这样的一些匹 ... -
Layout___模块重用之include布局整合
2011-04-06 14:04 1358在Android的开发中,我们知道布局文件可以让我们很 ... -
value-string
2011-04-06 13:36 2091我们知道在android的value文件夹下,string.x ... -
利用PorterDuff.Mode做橡皮擦效果
2011-04-01 18:03 10760http://stackoverflow.com/questi ... -
UI___tab view 的实现
2011-03-24 15:19 1985一.通过xml文件实现 1.创建一个tab iew,所需的x ... -
常用的html特殊符号转义符
2011-03-24 14:40 2221HTML特殊转义字符列表,最常用的字符实体 Character ...
相关推荐
Button selectAllBtn = findViewById(R.id.button_select_all); selectAllBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 全选/全不选的逻辑 for (int i = 0...
void selectAll() { for (int i = 0; i (); i++) { selectedItems[i] = true; } adapter.notifyDataSetChanged(); } // 反选 void reverseSelect() { for (int i = 0; i (); i++) { selectedItems[i] = !...
Button selectAll = findViewById(R.id.select_all); selectAll.setOnClickListener(v -> { for (Item item : items) { item.setSelected(true); } adapter.notifyDataSetChanged(); // 通知适配器数据已更新 })...
在适配器中,我们可以添加两个方法:`selectAll()`和`deselectAll()`,分别用于设置所有CheckBox为选中和未选中状态。同时,我们还需要监听CheckBox的状态变化,当用户点击CheckBox时更新数据源的状态。 为了实现...
在`selectAll()`方法中,我们可以检查当前项的CheckBox状态,如果已选中则将其设为未选中,反之亦然。 3. **删除所选**:删除所选项需要先获取选中项的列表。在ListView中,我们可以监听CheckBox的...
Button selectAllBtn = findViewById(R.id.select_all); selectAllBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i (); i++) { adapter....
findViewById(R.id.selectAll).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (MyData item : data) { item.setSelected(true); } notifyDataSetChanged(); ...
Button selectAllBtn = findViewById(R.id.select_all); selectAllBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i (); i++) { MyListAdapter...
本教程将详细讲解如何利用ListView、CheckBox和Menu来实现全选、反选以及删除选中项的功能。 **1. ListView的基本使用** ListView的使用通常涉及到以下步骤: - 在布局文件中添加ListView控件,设置其ID。 - 创建一...
selectAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i (); i++) { selectedItems[i] = !selectedItems[i]; } notifyDataSetChanged(); } ...
Button selectAllBtn = findViewById(R.id.select_all); selectAllBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i ; i++) { checkedItems[i]...
selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { for (int i = 0; i (); i++)...
selectAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i (); i++) { checkBoxStates[i] = true; // 更新ListView或RecyclerView中的Checkbox...
bt_selectall = (Button) findViewById(R.id.bt_selectall); // bt_cancel = (Button) findViewById(R.id.bt_cancelselectall); // bt_deselectall = (Button) findViewById(R.id.bt_deselectall); tv_show...
selectAll.setOnClickListener(v -> adapter.setAllChecked(true)); Button invert = findViewById(R.id.btn_invert_selection); invert.setOnClickListener(v -> adapter.invertSelection()); ``` 这样,我们就...
Button selectAllBtn = findViewById(R.id.select_all); selectAllBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isAllSelected = !isAllSelected; for (int i...
selectAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i ; i++) { checkedItems[i] = true; } adapter.notifyDataSetChanged(); // 更新视图...
void selectAll() { isAllChecked = true; for (int i = 0; i (); i++) { getItem(i).setChecked(isAllChecked); } notifyDataSetChanged(); } // 反选 void unselectAll() { isAllChecked = false; for ...
public void selectAll() { for (MyItem item : dataList) { item.setSelected(true); } notifyDataSetChanged(); } public void deselectAll() { for (MyItem item : dataList) { item.setSelected(false); ...
public void selectAll() { for (int i = 0; i (); i++) { getItem(i).setSelected(true); notifyDataSetInvalidated(); } } // 反选 public void invertSelection() { for (int i = 0; i (); i++) { getItem...