当前位置:操作系统 > 安卓/Android >>

android GridView实现图库预览图,多选模式下右上角打勾

    看到有初学者有这样的需求: GridView实现图库预览图,多选模式下右上角打勾。(4.0图库的预览图多选时,多了个蓝色边框,其实是蓝色背景)
 
       GridView在自己实际开发中也没用到过,就想试着实现下,写个demo供初学者参考,高手略过。
 
       先来个效果图吧:
 
       实现起来不复杂,就2个文件。 首先看看GridView继承关系:
 
       
 
java.lang.Object
   ↳ android.view.View
    ↳ android.view.ViewGroup
    ↳ android.widget.AdapterView<T extends android.widget.Adapter>
    ↳ android.widget.AbsListView
    ↳ android.widget.GridView
     我们知道ListView有单选模式和多选模式[AbsListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE/AbsListView.CHOICE_MODE_MULTIPLE_MODAL)]
 
     那么GridView也支持多选模式。
 
     废话不多说,相信你也知道!贴代码吧
 
主布局文件:main.xml
 
[html]  
<?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" />  
  
</LinearLayout>  
主Activity:HomeActivity
[java]  
package com.xyz.gridview;  
  
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;  
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 };  
  
    /** 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 v = LayoutInflater.from(this).inflate(R.layout.actionbar_layout,  
                null);   www.zzzyk.com
        mActionText = (TextView) v.findViewById(R.id.action_text);  
        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  
        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
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,