这是我的ListAdapter类,我使用复选框来检查自定义列表视图项。我想在Gmail应用程序的复选框选择上调用CAB。请帮帮我。提前致谢。
public class ListAdapter extends ArrayAdapter<ListGettersSetters>
{
ArrayList<ListGettersSetters> arrayListGettersSetters;
LayoutInflater layoutInflater;
Context context;
int Resource, i = 0, j = 0, checkedItemsCount = 0;
Animation animation1;
Animation animation2;
CheckBox flipCheckBox;
viewHolder holder;
static class viewHolder
{
public CheckBox imageView;
public TextView textViewName;
public TextView textViewData;
public TextView textViewDetails;
}
public ListAdapter(Context context, int resource, ArrayList<ListGettersSetters> arrayListGettersSetters)
{
super(context, resource, arrayListGettersSetters);
this.arrayListGettersSetters = arrayListGettersSetters;
Resource = resource;
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
animation1 = AnimationUtils.loadAnimation(context, R.anim.to_middle);
animation2 = AnimationUtils.loadAnimation(context, R.anim.from_middle);
}
@Override
public int getCount()
{
return arrayListGettersSetters.size();
}
@Override
public ListGettersSettersgetItem(int position)
{
return arrayListGettersSetters.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
holder = new viewHolder();
if(convertView == null)
{
convertView = layoutInflater.inflate(Resource, null);
holder.imageView = (CheckBox) convertView.findViewById(R.id.id_for_checkBox);
holder.textViewName = (TextView) convertView.findViewById(R.id.id_for_name_textView);
holder.textViewData = (TextView) convertView.findViewById(R.id.id_for_data_textView);
holder.textViewDetails = (TextView) convertView.findViewById(R.id.id_for_details_textView);
convertView.setTag(holder);
}
else
{
holder = (viewHolder) convertView.getTag();
}
holder.textViewName.setText(getItem(position).getName());
holder.textViewData.setText(getItem(position).getData());
holder.textViewDetails.setText(getItem(position).getDetails());
holder.imageView.setTag(position);
holder.imageView.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
flipCheckBox = (CheckBox) view;
flipCheckBox.clearAnimation();
flipCheckBox.setAnimation(animation1);
flipCheckBox.startAnimation(animation1);
setAnimListners(arrayListGettersSetters.get(Integer.parseInt(view.getTag().toString())));
}
});
return convertView;
}
private void setAnimListners(final ListGettersSetters listGettersSetters)
{
AnimationListener animationListener = new AnimationListener()
{
@Override
public void onAnimationStart(Animation animation)
{
if (animation == animation1)
{
flipCheckBox.clearAnimation();
flipCheckBox.setAnimation(animation2);
flipCheckBox.startAnimation(animation2);
}
else
{
listGettersSetters.setIsChecked(!listGettersSetters.isChecked());
setCount();
}
}
public void setCount()
{
if (listGettersSetters.isChecked())
{
checkedItemsCount++;
}
else
{
if (checkedItemsCount != 0)
{
checkedItemsCount--;
}
}
Log.v("Checked items count", checkedItemsCount + "");
}
@Override
public void onAnimationRepeat(Animation animation)
{
}
@Override
public void onAnimationEnd(Animation animation)
{
}
};
animation1.setAnimationListener(animationListener);
animation2.setAnimationListener(animationListener);
}
}
我的MainActivity.java类扩展了一个列表片段,它可以用作另一个活动中的片段。在这个活动中,我在ListFragment里面的listview上调用MultiChoiceModeListener。我想在不使用ABS的情况下调用CAB。
public class ActivityFragment extends ListFragment
{
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState)
{
View view = layoutInflater.inflate(R.layout.folders_fragment_listview, null, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
objectsList = new ArrayList<FoldersFragmentGettersSetters>();
fragmentListView = getListView();
fragmentListView .setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long offset)
{
}
});
fragmentListView .setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
fragmentListView .setMultiChoiceModeListener(new MultiChoiceModeListener()
{
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
{
return false;
}
@Override
public void onDestroyActionMode(ActionMode actionMode)
{
}
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
{
MenuInflater menuInflater = actionMode.getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
{
switch (menuItem.getItemId())
{
case R.id.menu_delete:
actionMode.finish();
return true;
default:
return false;
}
}
@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int position, long id, boolean checked)
{
}
});
}
}
答案 0 :(得分:0)
在数组
中添加所选项目ArrayList<ListGettersSetters> selected_items = new ArrayList<ListGettersSetters>();
public void setCount(position)
{
if (listGettersSetters.isChecked())
{
// add the selected item in array
selected_items.add(arrayListGettersSetters .get(position));
checkedItemsCount++;
Log.v("Checked items count", checkedItemsCount + "");
}
else
{
// Remove the item which is unselcted
selected_items.remove(position);
if (checkedItemsCount != 0)
{
checkedItemsCount--;
Log.v("Checked items count", checkedItemsCount + "");
}
}
}
所以你需要在setAnimListners
中传递位置
setAnimListners(arrayListGettersSetters.get(Integer.parseInt(view.getTag().toString())), position );
调用方法仅从setCount更新UI:
public void setCount(position)
{
// after if else
// your all other code will go here
// lastly call this method
((YourActivity)context).updateView(selected_items.size());
}
活动:
public void updateView(Integer length) {
if (length > 0) {
// show
} else {
// hide
}
}
希望它有助于ツ