Это мой класс ListAdapter, в котором я использую флажок для проверки пользовательских элементов списка. Я хочу вызвать CAB при выборе флажка, например, в приложении Gmail. Пожалуйста, помогите мне в этом. Заранее спасибо.
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 расширяет фрагмент списка, его можно использовать как фрагмент в другом действии. В этом упражнении я вызываю MultiChoiceModeListener в своем списке внутри ListFragment. Я хочу вызвать CAB без использования ABS.
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)
{
}
});
}
}