package com.example.mhyuam.actionmode;
import android.content.Context;
import android.support.v7.widget.PopupMenu;
import android.util.AttributeSet;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by mhyuam on 2016/7/20.
*/
/**
* 想要将popmenu添加到每一项中,首先要自定义一个Itemview,将popMenu关联到我们的ItemView中来
*/
public class ContextListItem extends LinearLayout implements PopupMenu.OnMenuItemClickListener, View.OnClickListener {
private PopupMenu popupMenu;
private TextView textView;
public ContextListItem(Context context) {
super(context);
}
public ContextListItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ContextListItem(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onClick(View view) {
popupMenu.show();
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
String itemText = textView.getText().toString();
switch (menuItem.getItemId()){
case R.id.menu_delete:
Toast.makeText(getContext(),"delete",Toast.LENGTH_SHORT).show();
break;
case R.id.menu_edit:
Toast.makeText(getContext(),"edit",Toast.LENGTH_SHORT).show();
break;
}
return true;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
textView = (TextView) findViewById(R.id.text);
View contextButton = findViewById(R.id.context);
//为view添加点击事件,在view的onclick的事件中显示popupmenu
contextButton.setOnClickListener(this);
popupMenu = new PopupMenu(getContext(),contextButton);
popupMenu.setOnMenuItemClickListener(this);
popupMenu.inflate(R.menu.contextmenu);
}
}
package com.example.mhyuam.actionmode;
import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
/**
* 想要使用ActionMode,需要实现很多的回调方法,直接implements ActionMode.Callback即可
* 我们这里使用的是Listview的MultiChoiceModeListener接口,它是ActionMode.Callback的
* 一个子接口
*
*/
public class ActionModeActivity extends Activity implements AbsListView.MultiChoiceModeListener{
private static final String[] ITEMS = {"one","two","three","four","five","six","seven","eight","nie",
"ten","eleven","twelve","thirtheen","fourteenth","fifteenth"};
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_action_mode);
listView = new ListView(this);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.list_item,R.id.text,ITEMS);
listView.setAdapter(arrayAdapter);
/**
* 想要使用一个多选择项的ActionMode,需要将choiceMode属性设置为CHOICE_MODE_MULTIPLE_MODAL
*/
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(this);
setContentView(listView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
int count = listView.getCheckedItemCount();
actionMode.setTitle(String.format("%d selected",count));
}
/**
* 这个方法其实和是创建点击后叠层的菜单选项,这个不需要icon,系统会自动为我们加上
* 点击一个菜单选项时会调用onActionItemClicked方法,来完成相应的操作
* 其中间显示的是已选中的项数,每选中或者取消一项都会调用onItemCheckedStateChanged,将其显示到叠层上来
* 选中的条目由getCheckedItemPositions()进行统计
*
* @param actionMode
* @param menu
* @return
*/
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
actionMode.getMenuInflater().inflate(R.menu.contextmenu,menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
//
return true;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
SparseBooleanArray itmes = listView.getCheckedItemPositions();
switch (menuItem.getItemId()){
case R.id.menu_delete:
Toast.makeText(ActionModeActivity.this,"item_delete",Toast.LENGTH_SHORT).show();
break;
case R.id.menu_edit:
Toast.makeText(ActionModeActivity.this,"item_edit",Toast.LENGTH_SHORT).show();
break;
default:
return false;
}
return true;
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
//ActionMode退出时会调用该方法
}
}
这个是item的布局文件
<com.example.mhyuam.actionmode.ContextListItem
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:background="?android:attr/activatedBackgroundIndicator">
"@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:layout_gravity="center_vertical"/>
"@+id/context"
android:layout_width="?android:attr/listPreferredItemHeightSmall"
android:layout_height="match_parent"
style="@style/Widget.AppCompat.ActionButton.Overflow"
android:clickable="true"
android:focusable="false"/>
com.example.mhyuam.actionmode.ContextListItem>
END