我需要在自定义列表视图上实现上下文操作栏以删除列表所选项目,我尝试了很多教程但没有得到结果
1) contextual_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/item_delete"
android:icon="@drawable/delete"
app:showAsAction="ifRoom|withText"
android:title="Delete"
android:titleCondensed="Delete">
</item>
</menu>
2) main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/titlelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/btnbackicon"
android:layout_width="40dp"
android:layout_height="60dp"
android:paddingLeft="-15dp"
android:paddingStart="-15dp"
android:paddingRight="-4dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:onClick="onBackIconClick"
android:background="@drawable/back_arrow" />
<ImageView
android:id="@+id/gmmf_logo"
android:layout_width="90dp"
android:paddingLeft="-10dp"
android:layout_height="90dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/btnbackicon"
android:onClick="onImageBackClick"
android:src="@drawable/gmmf" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/gmmf_logo"
android:layout_toRightOf="@+id/gmmf_logo"
android:background="@drawable/text_bg"
android:gravity="center"
android:padding="20dip"
android:text="@string/app_name"
android:textColor="@color/whiteColor"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="@+id/txtActivityTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/titlelayout"
android:text="Favorite Restaurants"
android:textSize="55sp"
android:background="@color/lightblack"
android:gravity="center"
android:textColor="@color/Orange"
android:textStyle="bold"
android:typeface="serif"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/titlelayout"
android:layout_alignEnd="@+id/titlelayout" />
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:choiceMode="multipleChoice"
android:layout_marginTop="10dp"
android:background="?android:attr/activatedBackgroundIndicator"
android:layout_below="@+id/txtActivityTitle"></ListView>
</RelativeLayout>
3) list_row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/txtRestroName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Darkorange"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtRastroType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtRestroName"
android:textColor="@color/whiteColor"
android:textSize="18sp"
/>
<Button
android:id="@+id/viewdetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/txtRestroRating"
android:background="@drawable/detail_button"
android:text="View Details"
android:textColor="@color/whiteColor"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtRestroRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="@color/whiteColor"
android:textSize="18sp" />
<View
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@+id/viewdetails"
android:layout_marginTop="5dp"
android:background="@color/whiteColor" />
</RelativeLayout>
4) CustomFavRestroAdapter.java
public class CustomFavRestroAdapter extends ArrayAdapter<FavEntityList> {
Context context;
LayoutInflater inflater;
List<FavEntityList> favRestroList;
private SparseBooleanArray mSelectedItemsIds;
public CustomFavRestroAdapter(Context context, int resourceId,
List<FavEntityList> favRestroList) {
super(context, resourceId, favRestroList);
mSelectedItemsIds = new SparseBooleanArray();
this.context = context;
this.favRestroList = favRestroList;
inflater = LayoutInflater.from(context);
}
private class ViewHolder {
TextView restroname;
TextView restrotype;
TextView restroratings;
}
public View getView(int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.list_row_layout, null);
// Locate the TextViews in listview_item.xml
holder.restroname = (TextView) view.findViewById(R.id.txtRestroName);
holder.restrotype = (TextView) view.findViewById(R.id.txtRastroType);
holder.restroratings = (TextView) view.findViewById(R.id.txtRestroRating);
// Locate the ImageView in listview_item.xml
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Capture position and set to the TextViews
holder.restroname.setText(favRestroList.get(position).getRestaurantName());
holder.restrotype.setText(favRestroList.get(position).getRestaurantType());
holder.restroratings.setText(favRestroList.get(position).getRestaurantRating());
return view;
}
@Override
public void remove(FavEntityList object) {
favRestroList.remove(object);
notifyDataSetChanged();
}
public List<FavEntityList> getWorldPopulation() {
return favRestroList;
}
public void toggleSelection(int position) {
selectView(position, !mSelectedItemsIds.get(position));
}
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
notifyDataSetChanged();
}
public void selectView(int position, boolean value) {
if (value)
mSelectedItemsIds.put(position, value);
else
mSelectedItemsIds.delete(position);
notifyDataSetChanged();
}
public int getSelectedCount() {
return mSelectedItemsIds.size();
}
public SparseBooleanArray getSelectedIds() {
return mSelectedItemsIds;
}
}
5) MainActivity.java
public class MainActivity extends Activity {
ListView list1;
CustomFavRestroAdapter listviewadapter;
List<FavEntityList> favrestro = new ArrayList<FavEntityList>();
String[] Restaurant_Names={"Paasha-JW Mariott Pune","RainForest","SubWay","KFC","Domino's","Pride Hotel","Blue Star"};
String[] Restaurant_Type={"Restro Bar","Restaurant (Vegetarian)","Restaurant (Non-Vegetarian)","Restaurant (Non-Vegetarian)","Restaurant (Vegetarian)","Restaurant (Vegetarian)","Restro Bar"};
String[] Restaurant_Rating={"Ratings 5.0","Ratings 4.8","Ratings 4.6","Ratings 4.0","Ratings 3.7","Ratings 3.5","Ratings 3.4"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < Restaurant_Names.length; i++) {
FavEntityList favrestros = new FavEntityList(Restaurant_Names[i], Restaurant_Type[i], Restaurant_Rating[i]);
favrestro.add(favrestros);
}
list1 = (ListView) findViewById(R.id.list_view);
listviewadapter = new CustomFavRestroAdapter(this, R.layout.list_row_layout, favrestro);
list1.setAdapter(listviewadapter);
list1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
list1.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
final int checkedCount = list1.getCheckedItemCount();
// Set the CAB title according to total checked items
mode.setTitle(checkedCount + " Selected");
// Calls toggleSelection method from ListViewAdapter Class
listviewadapter.toggleSelection(position);
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.item_delete:
// Calls getSelectedIds method from ListViewAdapter Class
SparseBooleanArray selected = listviewadapter
.getSelectedIds();
// Captures all selected ids with a loop
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
FavEntityList selecteditem = listviewadapter
.getItem(selected.keyAt(i));
// Remove selected items following the ids
listviewadapter.remove(selecteditem);
}
}
// Close CAB
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
listviewadapter.removeSelection();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我尝试了很多答案1)selected item on custom listview with contextual action bar
感谢提前继任
答案 0 :(得分:0)
在list_row_layout.xml中添加以下行
android:descendantFocusability="blocksDescendants"
由于你的行中有一个按钮,LongClick不会触发,因此需要添加上面一行。
同时添加
list1.setOnItemLongClickListener(this).
ActionBar侦听器
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contextual_menu.xml, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
答案 1 :(得分:0)
终于解决了问题
由于Button,OnItemLongClick未执行,因此
我刚刚删除按钮并放置了View of View Detail。
感谢@Lochana Ragupathy:)