Android:有关菜单的学习
上下文菜单
上下文菜单就是手机中对某一项进行点击一定时间后弹出的针对该项处理的菜单。
context_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_rotate"
android:title="Rotate"/>
<item
android:id="@+id/action_resize"
android:title="Resize"/>
</menu>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pop up the Context Menu"
android:id="@+id/button1"/>
</LinearLayout>
MainActivity:
package com.example.contextmenudemo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button1;//使用该按键来触发上下文菜单
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initUI();
}
private void initUI(){
button1 = (Button)findViewById(R.id.button1);
registerForContextMenu(button1);//对按键进行上下文菜单触发的注册
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();//需要用到MenuInflater
inflater.inflate(R.menu.context_menu,menu);//来获取Menu中的Item
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_rotate){//用来判断你选择了menu中的哪个Item
showAlertDialog("Rotate","you selected Rotate");
return true;
}else if (item.getItemId()==R.id.action_resize){
showAlertDialog("Resize","You selected Resize");
return true;
}
return super.onContextItemSelected(item);
}
private void showAlertDialog(String title,String message){
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.show();
}
}
效果:
Q:乍一看好像很简单,但一般这类菜单是用来删除ListView中的item用的,那又如何实现对ListView的删除呢?
A:得到对ListView的处理重在获得item的位置
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="26dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="item: X"
android:id="@+id/list_item_text"/>
</LinearLayout>
MainActivity.java
package com.example.listviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends Activity {
private ListView list;
private List<String>Data;
private int item_id;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
initUI();
}
public void initUI(){
list = (ListView)findViewById(R.id.list);
//往List中添加内容,让ListView显示Data中的内容
Data = new ArrayList<>();
for(int i=0;i<10;i++){
Data.add("item"+i);
}
adapter = new MyAdapter(MainActivity.this,Data);
list.setAdapter(adapter);
registerForContextMenu(list);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo)menuInfo;
item_id = info.position;
//以上的代码是获得所点击的ListView位置
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main,menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_delete){
Data.remove(item_id);
//下面这条代码是为了刷新ListView
adapter.notifyDataSetChanged();
}
return super.onContextItemSelected(item);
}
}
Myadapter.java
package com.example.listviewdemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by Funny_One on 2017/8/28.
*/
public class MyAdapter extends BaseAdapter {
private Context context;
private List<String>Data;
private View MyView;
public MyAdapter(Context context,List<String>Data){
this.context = context;
this.Data = Data;
}
@Override
public int getCount() {
return Data.size();
}
@Override
public Object getItem(int i) {
return 0;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
MyView = layoutInflater.inflate(R.layout.list_item,null);
TextView text =(TextView)MyView.findViewById(R.id.list_item_text);
text.setText(Data.get(i));
return MyView;
}
}
效果: