我真的需要你的帮助。首先,我创建了一个从 SherlockListFragment 扩展的 Customfragment。我实现了自己的 Listviewrow 设计,因此我创建了自己的 BaseAdapter。列表视图行包含一个复选框和一些文本视图。我的目标是构建一个类似于 Gmail 应用程序的外观和交互方式的列表视图。这意味着如果用户单击复选框,则将激活操作上下文菜单。
所以这些是我的课程:
public class PurchasesOpenFragment extends SherlockListFragment implements
OnItemClickListener, ListViewContextualMenuListener {
private ActionMode actionMode;
ArrayList<String> purchases = new ArrayList<String>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
purchases.add("Temp 1");
purchases.add("Temp 5");
purchases.add("Temp 2");
purchases.add("Temp 3");
purchases.add("Temp 4");
purchases.add("Temp 6");
purchases.add("Temp 7");
PurchaseOpenAdapter purchaseAdapter = new PurchaseOpenAdapter(
getActivity().getBaseContext(), purchases);
setListAdapter(purchaseAdapter);
purchaseAdapter.setOnListViewContextualMenuListener(this);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast toast = Toast.makeText(getActivity(),
"Show details of element with id: " +arg3,Toast.LENGTH_SHORT);
toast.show();
}
private final class ModeCallback implements ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Create the menu from the xml file
MenuInflater inflater = getSherlockActivity()
.getSupportMenuInflater();
inflater.inflate(R.menu.context_test, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here, you can checked selected items to adapt available actions
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Destroying action mode, let's unselect all items
for (int i = 0; i < getListView().getAdapter().getCount(); i++)
getListView().setItemChecked(i, false);
if (mode == actionMode) {
actionMode = null;
}
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
long[] selected = getListView().getCheckedItemIds();
Toast toast = Toast.makeText(getActivity(), "elements selected: "
+ selected.length, Toast.LENGTH_SHORT);
toast.show();
if (selected.length > 0) {
for (long id : selected) {
toast = Toast.makeText(getActivity(), "element selected with id: " + id,
Toast.LENGTH_SHORT);
toast.show();
}
}
mode.finish();
return true;
}
};
public void closeContextualMenu() {
if (actionMode != null) {
actionMode.finish();
actionMode = null;
}
}
/**
*
*/
@Override
public void onItemCheckBoxChanged(int position, boolean checkState) {
Toast toast = Toast.makeText(getActivity(), "pos: " + position
+ " check: " + checkState, Toast.LENGTH_SHORT);
toast.show();
getListView().setItemChecked(position, checkState);
int amountOfCheckedElements = getListView().getCheckedItemIds().length;
if (amountOfCheckedElements > 0) {
if (actionMode == null) {
actionMode = getSherlockActivity().startActionMode(
new ModeCallback());
}
actionMode.setTitle(amountOfCheckedElements + " selected");
return;
} else {
if (actionMode != null) {
actionMode.finish();
}
}
}
}
第二类:
公共类 PurchaseOpenAdapter 扩展 BaseAdapter 实现 OnClickListener {
private Context context;
private ArrayList<String> purchases;
private static LayoutInflater inflator;
private ListViewContextualMenuListener contextualListener;
public PurchaseOpenAdapter(Context context, ArrayList<String> purchases) {
this.context = context;
this.purchases = purchases;
inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return purchases.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
boolean checkBoxChanged = false;
if (convertView == null) {
convertView = inflator
.inflate(R.layout.listview_row, parent, false);
holder = new ViewHolder();
holder.tvDate = (TextView) convertView
.findViewById(R.id.listview_purchase_date);
holder.tvTitle = (TextView) convertView
.findViewById(R.id.listview_purchase_title);
holder.tvSupermarket = (TextView) convertView
.findViewById(R.id.listview_supermarket);
holder.tvArticleAmount = (TextView) convertView
.findViewById(R.id.listview_article_amount);
holder.checkBox = (CheckBox) convertView
.findViewById(R.id.listview_checkbox);
holder.checkBox.setTag(position);
holder.checkBox.setOnClickListener(this);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// TODO only temp data
String temp = purchases.get(position);
holder.tvDate.setText("01.01.2001");
holder.tvTitle.setText(temp);
holder.tvSupermarket.setText("Spar");
holder.tvArticleAmount.setText("20 Articles");
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
public void setOnListViewContextualMenuListener(
ListViewContextualMenuListener l) {
contextualListener = l;
}
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Toast toast = Toast
.makeText(context, "Checkboxstate of element with positon: "
+ v.getTag() + " check state: " + cb.isChecked(),
Toast.LENGTH_SHORT);
toast.show();
contextualListener.onItemCheckBoxChanged(Integer
.parseInt(cb.getTag().toString()),
cb.isChecked());
}
static class ViewHolder {
TextView tvDate;
TextView tvTitle;
TextView tvSupermarket;
TextView tvArticleAmount;
CheckBox checkBox;
}
}
正如您在此处看到的,我使用为 PurchaseOpenAdapter 中的复选框实现了 OnClickMethod。为了通知 PurchaseOpenFragment 复选框的状态发生了变化,我创建了自己的监听器(onItemCheckBoxChanged)。
所以现在我来描述我的问题:我单击复选框并出现上下文菜单但未选中复选框但如果我检查复选框的状态一切正常(id 和 isChecked() 是正确的)。
如果我关闭上下文菜单,则会选中复选框。
如果我再次单击同一个复选框,则 id 完全错误。如果我删除适配器类中的侦听器执行,复选框将正常工作。
希望有人可以帮助我。也许还有另一种方式来通知 listfragment 类。先感谢您!
由于我是新用户,我不允许发布图片,但如有必要,我可以通过邮件发送。