我真的需要你的帮助。首先,我创建了一个从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;
}
}
正如您在此处所见,我使用OnClickMethod实现了PurchaseOpenAdapter中的复选框。 要通知PurchaseOpenFragment复选框的状态已更改,我创建了自己的监听器(onItemCheckBoxChanged)。
所以现在我来描述我的问题: 我点击复选框,出现上下文菜单,但没有选中复选框,但如果我检查复选框的状态,一切正常(id和isChecked()是正确的。)
如果我关闭上下文菜单,则会选中该复选框。
如果我再次点击同一个复选框,则ID完全错误。 如果我在适配器类中删除了侦听器执行,则复选框可以正常工作。
希望有人可以帮助我。也许还有另一种方法来通知listfragment类。 提前谢谢!
由于我是新用户,我不允许发布图片,但如果有必要,我可以通过邮件发送。