创建ListView
并选择模式为CHOICE_MODE_MULTIPLE_MODAL
时,我遇到了问题。我在列表项布局中添加了android:background="?android:attr/activatedBackgroundIndicator"
。但我的问题是列表项没有在选择时突出显示。但是动作模式会显示所选项目的数量。
这是我的代码段。如果还不够请评论。
group_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="?android:attr/activatedBackgroundIndicator">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:textColor="@color/black"
android:textSize="20sp"
android:padding="10dp"
android:id="@+id/tv_item_head"/>
</LinearLayout>
ListViewFragment.java
public class ListViewFragment extends Fragment {
private ArrayList<String> listItems;
ListViewAdapter adapter;
private Activity mActivity;
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_listview, container, false);
initVariables();
if(mActivity == null) {
mActivity = getActivity();
}
listView = (ListView) root.findViewById(R.id.list);
listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new callbacks());
adapter = new ListViewAdapter(listItems, getActivity(), getContext());
listView.setAdapter(adapter);
return root;
}
private void initVariables() {
listItems = new ArrayList<>(Arrays.asList("Item 1", "Item 2", "Item 3", "Item 4", "Item 5","Item 6","Item 7"));
}
private class callbacks implements ListView.MultiChoiceModeListener {
@Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
final int checkedCount = listView.getCheckedItemCount();
mode.setSubtitle("" + checkedCount + " items selected");
adapter.checkedItems.put(position, checked);
adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater inflater = mActivity.getMenuInflater();
inflater.inflate(R.menu.list_select_menu, menu);
actionMode.setTitle("Select Items");
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return true;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.share:
Toast.makeText(mActivity, "Shared " + listView.getCheckedItemCount() +
" items", Toast.LENGTH_SHORT).show();
actionMode.finish();
break;
default:
Toast.makeText(mActivity, "Clicked " + menuItem.getTitle(),
Toast.LENGTH_SHORT).show();
break;
}
return true;
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
adapter.checkedItems.clear();
adapter.notifyDataSetChanged();
}
}
}
ListViewAdapter.java
public class ListViewAdapter extends BaseAdapter {
ArrayList<String> listItems;
Context mContext;
Activity mActivity;
SparseBooleanArray checkedItems;
public ListViewAdapter(ArrayList<String> listItems, FragmentActivity activity, Context context) {
this.listItems = listItems;
this.mActivity = activity;
this.mContext = context;
this.checkedItems = new SparseBooleanArray();
}
@Override
public int getCount() {
return listItems.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View listItem;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
listItem = inflater.inflate(R.layout.group_layout, null);
} else {
listItem = convertView;
}
TextView tv = (TextView) listItem.findViewById(R.id.tv_item_head);
tv.setText(listItems.get(position));
listItem.setId(position);
listItem.setActivated(checkedItems.get(position));
return listItem;
}
}
这里有什么遗漏。任何帮助将不胜感激。 请指出我的代码中没有遵守android约定或规则的任何内容,如果有的话。