我有一个带有MultiChoice listView的ListFragment。我想将选择的数量限制为3.为此,我在onViewCreated中实现了MultiChoiceModeListener。出于某种原因,听众不工作?
然而; onListItemClick有效吗?
(作为替代方法:有没有办法在onListItemClick中跟踪已检查和未检查的限制为3?)
public class PinnedSectionListFrag extends ListFragment {...
...
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setListAdapter(new SimpleAdapter(getActivity(), android.R.layout.simple_list_item_multiple_choice, android.R.id.text1));
listView = getListView();
// listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
Log.e("PinnedSectionsList...","......"+position);
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Log.e("PinnedSectionsList...","...item........"+item);
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
});
}
...
@Override // Working..
public void onListItemClick(ListView l, View v, int position, final long id) {
Item item = (Item) getListView().getAdapter().getItem(position);
if (item != null) {
Toast.makeText(getActivity(), "Item " + position + ": " + item.text, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Item " + position, Toast.LENGTH_SHORT).show();
}
}...
- 感谢任何帮助/线索。
答案 0 :(得分:0)
这可能对某些人有所帮助......我选择了替代方案并使用onListItemClick跟踪自己。
@Override
public void onListItemClick(ListView l, View v, int position, final long id) {
if (selectedItems.size() < 2){
selectedItems.add(item.text);
if (selectedItems.size() == 1) {
MyAppClass.getInstance().saveSharedSetting(getActivity(), "Titles", selectedItems.get(0));
}else {
MyAppClass.getInstance().saveSharedSetting(getActivity(), "Titles", TextUtils.join(", ", selectedItems));
}
Log.e("onListItemClick()","Read Titles..."+MyAppClass.getInstance().readSharedSetting(getActivity(), "Titles","TitlesRead"));
//
}else {
for (int i =0; i < selectedItems.size(); i++ ){
if (selectedItems.get(i) == item.text){
selectedItems.remove(i);
}
}
// sparseBooleanArray.put(position, false);//.get(position) = false;
getListView().setItemChecked(position, false);
Toast.makeText(getActivity(), "Max 2 Items Reached ! - ", Toast.LENGTH_SHORT).show();
}