在GridView中选择多个项目

时间:2016-03-10 21:52:13

标签: android gridview

我正在开发类似画廊的活动。一切运行良好但有一个关键功能缺失,我找不到一个体面的答案或解释。我需要的是让用户能够通过长按来选择多个项目。

这是期望的结果:

enter image description here

您可以在ActionBar中清楚地看到所选的照片和选项。



我的设置是:

1.我的XML中有 GridView

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/gridview"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:columnWidth="90dp"
          android:numColumns="auto_fit"
          android:verticalSpacing="5dp"
          android:horizontalSpacing="5dp"
          android:stretchMode="columnWidth"
          android:gravity="center"/>

2.它附加到一个类,它扩展 BaseAdapter 并使用Picasso加载图像:

public class GalleryAdapter extends BaseAdapter {
    Context mContext;
    List<String> mDataset;

    public GalleryAdapter(Context context, List<String> dataset) {
        mContext = context;
        mDataset = dataset;
    }

    @Override
    public int getCount() {
        return mDataset.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataset.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if(convertView == null){
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(200,200));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8,8,8,8);
        } else {
            imageView = (ImageView) convertView;
        }

        //TODO: REMOVE INTEGER.VALUEOFF. IT'S MADE FOR MOCK
        Picasso.with(mContext).load(Integer.valueOf(mDataset.get(position))).fit().into(imageView);

        return imageView;
    }


}

3.它附加到活动:

//Get images paths
List<String> data = getImagesPath(this);
    List<String> sortedData = new ArrayList<>();
    for(String file : data){
        sortedData.add(0, file);
    }

    GalleryAdapter adapter = new GalleryAdapter(this, sortedData);
    mGallery.setAdapter(adapter);


从现在开始斗争开始了:

  • 我尝试在GridView中制作我的项目来实现 Checkable

    public class CheckableItem extends LinearLayout implements Checkable{
    private boolean mIsChecked = false;
    private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };
    
    public CheckableItem(Context context) {
        super(context);
    }
    
    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked())
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        return drawableState;
    }
    
    @Override
    public void setChecked(boolean checked) {
        mIsChecked = checked;
        refreshDrawableState();
    }
    
    @Override
    public boolean isChecked() {
        return mIsChecked;
    }
    
    @Override
    public void toggle() {
        setChecked(!isChecked());
        refreshDrawableState();
    }
    

然后我将其加载到我的XML中,并将.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE)添加到Activity中的 GridView 。什么都没发生。

  • 添加了 MultiChoiceModeListener

    class MultiChoiceListener implements GridView.MultiChoiceModeListener{ @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { int selectCount = mGallery.getCheckedItemCount(); switch (selectCount) { case 1: mode.setSubtitle("One item selected"); break; default: mode.setSubtitle("" + selectCount + " items selected"); break; } }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.setTitle("Select Items");
        mode.setSubtitle("One item selected");
        return true;
    }
    
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }
    
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        return false;
    }
    
    @Override
    public void onDestroyActionMode(ActionMode mode) {
    
    }
    

    @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { mode.setTitle("Select Items"); mode.setSubtitle("One item selected"); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; } @Override public void onDestroyActionMode(ActionMode mode) { }

然后将其添加到我的gridView },但仍然没有结果。

有人可以提出一种在 GridView 中实现此项目选择行为的方法吗?我想使用原生Android API,没有第三方库。

1 个答案:

答案 0 :(得分:6)

我认为Checkable和我在你的代码中看到的其余内容有点过分......当我想实现类似的东西时我只是在GridView /中显示的对象的类中添加另一个字段ListView,用于存储对象的已检查状态。例如:

class Image {
     Bitmap bm;
     boolean isChecked=false;

     public Image(Bitmap bm){
        this.bm=bm;
     }

     public boolean isChecked(){
         return isChecked;
     }
     public void toggleChecked(){
         isChecked = !isChecked;
     }
}

并将ArrayList提供给适配器。在适配器的getView方法中我做了这样的事情;

@Override
public View getView(int position, View convertView, ViewGroup parent) { 

     if(images.get(position).isChecked()){
        //show the overlay view that suggests the item is selected
     }
     else{
        //hide the overlay view 
     } 
}

最后在ListView的onItemClickListener

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        images.get(position).toggleChecked();
        listView.getAdapter().notifyDataSetChanged();
    }
});