您好,登錄后才能下訂單哦!
這篇文章給大家介紹Android中怎么利用GridView實現多選效果,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
ActionBar:
Action Bar是活動中的一種控件,用以代替傳統的品目頂端的標題欄,它提供了多便利性。有關其詳細內容會在以后研究,現在主要考慮上述需求的實現。
先上效果圖
首先是關于ActionBar的布局文件:主要是用于實現全選與全不選的功能的。
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_select" android:title="@string/select_all"/> <item android:id="@+id/menu_unselect" android:title="@string/unselect_all"/> </menu>
其次是其樣式文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 設置ActionBar的樣式 --> <style name="DiyTheme" parent="@android:style/Theme.Holo"> <item name="android:actionModeBackground">@drawable/tab_bg</item> <item name="android:actionModeCloseDrawable">@drawable/back</item> <item name="android:actionBarStyle">@style/DiyActionBarStyle</item> </style> <style name="DiyActionBarStyle" parent="@android:style/Widget.Holo.ActionBar"> <item name="android:background">@drawable/tab_bg</item> </style> </resources>
字符資源文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">GridView</string> <string name="selection">選中%s個</string> <string name="select_all">全選</string> <string name="unselect_all">全不選</string> </resources>
grid_item.xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/img_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY" /> <ImageView android:id="@+id/select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@drawable/icon_choice" android:visibility="gone" /> <!-- 這里是當該圖片被選上的時候打上小勾的,因此起初是隱藏不顯示的 --> </RelativeLayout>
main.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="fill_parent" > <GridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:columnWidth="75dip" android:gravity="center" android:horizontalSpacing="2dip" android:numColumns="4" android:verticalSpacing="2dip" android:layout_marginTop="20dip" /> </LinearLayout>
actionBar_layout.xml文件:
<?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" > <TextView android:id="@+id/action_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="@android:color/white" android:layout_gravity="center_vertical" /> </LinearLayout>
GridItem.java文件:
package com.kiritor; /** * @author Kiritor * 實現自己的View繼承Checable接口*/ import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Checkable; import android.widget.ImageView; import android.widget.RelativeLayout; public class GridItem extends RelativeLayout implements Checkable { private Context mContext; private boolean mChecked;//判斷該選項是否被選上的標志量 private ImageView mImgView = null; private ImageView mSecletView = null; public GridItem(Context context) { this(context, null, 0); } public GridItem(Context context, AttributeSet attrs) { this(context, attrs, 0); } public GridItem(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub mContext = context; LayoutInflater.from(mContext).inflate(R.layout.grid_item, this); mImgView = (ImageView) findViewById(R.id.img_view); mSecletView = (ImageView) findViewById(R.id.select); } @Override public void setChecked(boolean checked) { // TODO Auto-generated method stub mChecked = checked; setBackgroundDrawable(checked ? getResources().getDrawable( R.drawable.background) : null); mSecletView.setVisibility(checked ? View.VISIBLE : View.GONE);//選上了則顯示小勾圖片 } @Override public boolean isChecked() { // TODO Auto-generated method stub return mChecked; } @Override public void toggle() { setChecked(!mChecked); } public void setImgResId(int resId) { if (mImgView != null) { mImgView.setBackgroundResource(resId);//設置背景 } } }
主activity文件:
package com.kiritor; import java.util.HashMap; import java.util.Map; import java.util.Set; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.ActionMode; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView.LayoutParams; import android.widget.AbsListView.MultiChoiceModeListener; import android.widget.BaseAdapter; import android.widget.Checkable; import android.widget.FrameLayout; import android.widget.GridView; import android.widget.ImageView; import android.widget.ListAdapter; import android.widget.TextView; public class HomeActivity extends Activity implements MultiChoiceModeListener { private GridView mGridView; private GridAdapter mGridAdapter; private TextView mActionText; private static final int MENU_SELECT_ALL = 0; private static final int MENU_UNSELECT_ALL = MENU_SELECT_ALL + 1; private Map<Integer, Boolean> mSelectMap = new HashMap<Integer, Boolean>(); private int[] mImgIds = new int[] { R.drawable.img_1, R.drawable.img_2, R.drawable.img_3, R.drawable.img_4, R.drawable.img_5, R.drawable.img_6, R.drawable.img_7, R.drawable.img_8, R.drawable.img_9, R.drawable.img_1, R.drawable.img_2, R.drawable.img_3, R.drawable.img_4, R.drawable.img_5, R.drawable.img_6, R.drawable.img_7, R.drawable.img_3, R.drawable.img_4, R.drawable.img_5, R.drawable.img_5 }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mGridView = (GridView) findViewById(R.id.gridview); mGridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);// 設置為多選模式 mGridAdapter = new GridAdapter(this); mGridView.setAdapter(mGridAdapter);// 數據適配 mGridView.setMultiChoiceModeListener(this);// 設置多選模式監聽器 } /** Override MultiChoiceModeListener start **/ @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { // TODO Auto-generated method stub // 得到布局文件的View View v = LayoutInflater.from(this).inflate(R.layout.actionbar_layout, null); mActionText = (TextView) v.findViewById(R.id.action_text); // 設置顯示內容為GridView選中的項目個數 mActionText.setText(formatString(mGridView.getCheckedItemCount())); // 設置動作條的視圖 mode.setCustomView(v); // 得到菜單 getMenuInflater().inflate(R.menu.action_menu, menu); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { // TODO Auto-generated method stub /* 初始狀態下,如果選中的項數不等于總共的項數,設置"全選"的狀態為True */ menu.getItem(MENU_SELECT_ALL).setEnabled( mGridView.getCheckedItemCount() != mGridView.getCount()); return true; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { // TODO Auto-generated method stub /* * 當點擊全選的時候,全選 點擊全不選的時候全不選 */ switch (item.getItemId()) { case R.id.menu_select: for (int i = 0; i < mGridView.getCount(); i++) { mGridView.setItemChecked(i, true); mSelectMap.put(i, true); } break; case R.id.menu_unselect: for (int i = 0; i < mGridView.getCount(); i++) { mGridView.setItemChecked(i, false); mSelectMap.clear(); } break; } return true; } @Override public void onDestroyActionMode(ActionMode mode) { // TODO Auto-generated method stub mGridAdapter.notifyDataSetChanged(); } @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { // TODO Auto-generated method stub // 當每個項狀態改變的時候的操作 mActionText.setText(formatString(mGridView.getCheckedItemCount())); mSelectMap.put(position, checked);/* 放入選中的集合中 */ mode.invalidate(); } /** Override MultiChoiceModeListener end **/ private String formatString(int count) { return String.format(getString(R.string.selection), count); } private class GridAdapter extends BaseAdapter { private Context mContext; public GridAdapter(Context ctx) { mContext = ctx; } @Override public int getCount() { // TODO Auto-generated method stub return mImgIds.length; } @Override public Integer getItem(int position) { // TODO Auto-generated method stub return Integer.valueOf(mImgIds[position]); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @SuppressWarnings("deprecation") @Override /* 得到View */ public View getView(int position, View convertView, ViewGroup parent) { GridItem item; if (convertView == null) { item = new GridItem(mContext); item.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); } else { item = (GridItem) convertView; } item.setImgResId(getItem(position)); item.setChecked(mSelectMap.get(position) == null ? false : mSelectMap.get(position)); return item; } } }
關于Android中怎么利用GridView實現多選效果就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。