一、长按item时的响应
在长按item时,我希望能让CheckBox显示出来,并且呼出全选、反选、取消菜单,以及下载、删除、移动、复制操作菜单。
我在具体实现的时候发现处理item布局是一个很大的问题,因为不知道怎么在Fragment里面去改变每一个item的CheckBox状态。经过考虑,我打算把这些方法都写在ListViewAdapter里,并且在这个类里设置一个Map对象,记录每一个position对应的isCheck状态。当我点击每一个CheckBox或者从Fragment里面点击全选/反选时,改变这个Map的值,然后通过重写adapter里的getView方法来控制哪些CheckBox显示为已选,哪些为未选。
而另外的两个菜单,我在HomeFragment布局里面都把他们写了出来,然后初始化为不可见。当长按item触发OnItemLongClickedListener的时候将他们设置为可见,并且把他们覆盖了的组件设置为不可见(避免不可见但仍能响应Click事件的问题)
另外还有一个变量isSelecting我认为非常重要,它用来判断当前Fragment是否是处于选择阶段。
二、FileListAdapter
package com.example.vcloud_3_25.utils; import java.util.HashMap; import java.util.List; import java.util.Map; import android.annotation.SuppressLint; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.SimpleAdapter; import com.example.vcloud_3_25.R; public class FileListAdapter extends SimpleAdapter { private Map<Integer, Boolean> isSelected; private boolean isSelecting; private List<Map<String, Object>> mResList; @SuppressWarnings("unchecked") public FileListAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); this.mResList = (List<Map<String, Object>>)data; init(); } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); CheckBox cb = (CheckBox) view.findViewById(R.id.file_checkbox); if (!isSelecting) { cb.setVisibility(View.INVISIBLE); } else { cb.setVisibility(View.VISIBLE); cb.setChecked(isSelected.get(position)); } cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton checkBox, boolean flag) { isSelected.put(position, flag); } }); return view; } @SuppressLint("UseSparseArrays") public void init() { isSelected = new HashMap<Integer,Boolean>(); for (int i = 0; i < mResList.size(); i++) { isSelected.put(i, false); } isSelecting = false; } public void setIsSelecting(boolean flag){ this.isSelecting = flag; } public void setSelected(int position,boolean flag){ isSelected.put(position, flag); } public Map<Integer,Boolean> getSelected(){ return isSelected; } public void selectAll(){ for (int i = 0; i < mResList.size(); i++) { isSelected.put(i, true); } } public void selectReverse(){ for (int i = 0; i < mResList.size(); i++) { isSelected.put(i, !isSelected.get(i)); } } }
三、HomeFragment里面的一些核心函数
mListView.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View item, int position, long id) { if (!isSelecting) { mFolderIndicator.setVisibility(View.INVISIBLE); mSelectBar.setVisibility(view.VISIBLE); mSelectActionBar.setVisibility(view.VISIBLE); isSelecting = true; adapter.init(); adapter.setIsSelecting(isSelecting); adapter.setSelected(position, true); refreshListView(); } return true; } }); mSelectCancelButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { backToNormal(); } }); mSelectAllButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { adapter.selectAll(); refreshListView(); } }); mSelectReverseButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { adapter.selectReverse(); refreshListView(); } }); } private void refreshListView() { mResList.clear(); mResList.addAll(getResList(recentPid)); adapter.notifyDataSetChanged(); } private void backToNormal() { adapter.init(); isSelecting = false; refreshListView(); mFolderIndicator.setVisibility(View.VISIBLE); mSelectBar.setVisibility(View.INVISIBLE); mSelectActionBar.setVisibility(View.INVISIBLE); }