I have made simple menu.
Now how can I 开发者_运维百科make a context menu?
just register for context menu in onCreate method
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)this.findViewById(R.id.textViewId);
registerForContextMenu(this.getTextView());
}
create ContextMenu here by adding item
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
menu.setHeaderTitle("Sample Context Menu");
menu.add(200, 200, 200, "item1");
}
respond here for selected item
@Override
public boolean onContextItemSelected(MenuItem item)
{
if (item.itemId() = some-menu-item-id)
{
//handle this menu item
return true;
}
… other exception processing
}
just run and get your context menu :)
try this one..
registerForContextMenu(Listview);
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(Menu.NONE, 0, Menu.NONE, "Edit");
menu.add(Menu.NONE, 1, Menu.NONE,"Delete");
menu.add(Menu.NONE, 2, Menu.NONE,"Merge");
//menu.add(Menu.NONE, 3, Menu.NONE,"Delete duplicate");
}
edit: sorry, but ist's similar, you have to register the view for the contextmenü and then you have to ovverride the create and selected stuff
registerForContextMenu([your view])
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
switch (item.getItemId()){
case CONTEXT_ASSIS:
switch (item.getGroupId()){
case CONTEXT_ASSIS:
break;
}
case R.id.navi:
break;
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case CONTEXT_ASSIS:
switch (item.getGroupId()){
case R.id.navi:
break;
case R.id.assis:
break;
case R.id.save_pos:
break;
case R.id.docs:
break;
}
return true;
}
//ende edit
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menutest, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
boolean ret =super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.rotate:
break;
case R.id.settings:
break;
case R.id.talk:
break;
case R.id.calendar:
break;
case R.id.speechsearch:
break;
}
return ret;
}
Code for context Menu:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout tv = (LinearLayout)findViewById(R.id.linear);
registerForContextMenu(tv);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
menu.setHeaderTitle("Sample Context Menu");
menu.add("item1");
menu.add("item2");
}
public boolean onContextItemSelected(MenuItem item)
{
if (item.getTitle().toString().equals("item1"))
{
//handle this menu item
Toast.makeText(this, "Hey, you selected item1", Toast.LENGTH_SHORT).show();
}
return true;
}
}
I can explain steps to make context menu easily :-
First clone demo project from this Github Repo and follow below steps.
You can find, CircleDrag class is used for dragging circle implementation. This class contains logic for interaction with menu items and return callback to MainActivity using OnViewCrossed interface to redraw menu items.
Check MainActivity.java First we initialize CircleDrag class.
private void initCircleDrag(){
final int radius = (int) getResources().getDimension(R.dimen._80sdp);
final CircleDrag circleDrag = new CircleDrag(this);
circleDrag.init(viewCircle, this, radius, indicatorViewList);}
Below method is used to create dynamic menu items. Just pass menu item name and icon.
public void createMenuItem(String text, @DrawableRes int drawableId) {
textViewList.add(createTextView(relativeParent, text));
menuItemViewList.add(createMenuItem(rlCenterView, drawableId));
indicatorViewList.add(createMenuItem(rlCenterView, R.drawable.trans_circle));
DOTS_COUNT = textViewList.size();
}
Below method is used to move menu layout to touched position.
public void setMenuItemsPosition(View parentView, View menuLayoutView, float x, float y) {
//p for parent
float pl, pr, pb, pt;
//c for child
float cl, cr, cb, ct;
int[] v1_coords = new int[2];
parentView.getLocationOnScreen(v1_coords);
pl = 0;
pr = parentView.getWidth();
pt = 0;
pb = parentView.getHeight();
cl = menuLayoutView.getX();
ct = menuLayoutView.getY();
cr = menuLayoutView.getWidth() + cl;
cb = menuLayoutView.getHeight() + ct;
if (cl < pl) {
if (ct < pt && cl < pl) {
setMenusPosition(Angle.LEFT_TOP);
} else if (cb > pb && cl < pl) {
setMenusPosition(Angle.LEFT_BOTTOM);
} else {
setMenusPosition(Angle.LEFT);
}
} else if (cr > pr) {
if (ct < pt && cr > pr) {
setMenusPosition(Angle.RIGHT_TOP);
} else if (cb > pb && cr > pr) {
setMenusPosition(Angle.RIGHT_BOTTOM);
} else {
setMenusPosition(Angle.RIGHT);
}
} else if (ct < pt) {
setMenusPosition(Angle.TOP);
} else if (cb > pb) {
setMenusPosition(Angle.BOTTOM);
} else {
setMenusPosition(Angle.CENTER);
}
}
Below method is used to draw menu items circle based on the angle. If user touch in center menu items draws in round shape, if user touch on sides then menu items draw in half of the circle and if user touch on any angle of the screen then menu items draw in fourth of the circle.
private void setMenusPosition(Angle angle);
Below listener is used to show contextual menu view when long press is performed.
relativeParent.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return false;
}
});
final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
Log.e("GestureDetector", "Longpress detected");
if (setVisible) {
rlMenuView.setVisibility(View.VISIBLE);
rlTouchLayout.setBackgroundResource(R.color.menu_background);
isLongPressDetected = true;
}
}
});
Below listener is used to detect touch and draw menu items around touched point.
relativeParent.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getX() >= getResources().getDimension(R.dimen._10sdp) && event.getX() <= relativeParent.getWidth() - getResources().getDimension(R.dimen._10sdp) &&
event.getY() >= getResources().getDimension(R.dimen._10sdp) && event.getY() <= relativeParent.getHeight() - getResources().getDimension(R.dimen._10sdp)) {
setVisible = true;
//pass touch event
viewCircle.dispatchTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastSelectedPosition = -1;
float centerX = event.getX() - rlMenuView.getWidth() / 2;
float centerY = event.getY() - rlMenuView.getHeight() / 2;
rlMenuView.setX(centerX);
rlMenuView.setY(centerY);
setMenuItemsPosition(relativeParent, rlMenuView, centerX, centerY);
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
if (lastSelectedPosition != -1) {
onMenuSelected(lastSelectedPosition, textViewList.get(lastSelectedPosition).getText().toString());
} else {
onNoMenuSelected();
}
setVisible = false;
setMenusPosition(Angle.CENTER);
rlMenuView.setVisibility(View.INVISIBLE);
rlTouchLayout.setBackgroundResource(R.color.tranparent);
hideTextViews();
break;
}
} else {
if (event.getAction() == MotionEvent.ACTION_UP) {
onNoMenuSelected();
setVisible = false;
setMenusPosition(Angle.CENTER);
rlMenuView.setVisibility(View.INVISIBLE);
rlTouchLayout.setBackgroundResource(R.color.tranparent);
hideTextViews();
}
}
return false;
}
});
Below method is used to redraw menu items and menu text when item is selected or not selected.
@Override
public void OnViewTouched(int flag) {
Log.d(TAG, "onSelect: ");
lastSelectedPosition = -1;
for (int i = 0; i < DOTS_COUNT; i++) {
if (flag == indicatorViewList.get(i).getId()) {
lastSelectedPosition = i;
if ((int) pos_center_view[0] > (int) indicatorViewList.get(i).getX()
&& (pos_center_view[0] - indicatorViewList.get(i).getX() > 4)) {
menuItemViewList.get(i).setX(indicatorViewList.get(i).getX() - getResources().getDimension(R.dimen._6sdp));
} else if ((int) pos_center_view[0] < (int) indicatorViewList.get(i).getX()
&& (indicatorViewList.get(i).getX() - pos_center_view[0] > 4)) {
menuItemViewList.get(i).setX(indicatorViewList.get(i).getX() + getResources().getDimension(R.dimen._6sdp));
} else {
menuItemViewList.get(i).setX(indicatorViewList.get(i).getX());
}
if ((int) pos_center_view[1] > (int) indicatorViewList.get(i).getY()
&& (pos_center_view[1] - indicatorViewList.get(i).getY() > 4)) {
menuItemViewList.get(i).setY(indicatorViewList.get(i).getY() - getResources().getDimension(R.dimen._6sdp));
} else if ((int) pos_center_view[1] < (int) indicatorViewList.get(i).getY()
&& (indicatorViewList.get(i).getY() - pos_center_view[1] > 4)) {
menuItemViewList.get(i).setY(indicatorViewList.get(i).getY() + getResources().getDimension(R.dimen._6sdp));
} else {
menuItemViewList.get(i).setY(indicatorViewList.get(i).getY());
}
textViewList.get(i).setVisibility(View.VISIBLE);
} else {
menuItemViewList.get(i).setX(indicatorViewList.get(i).getX());
menuItemViewList.get(i).setY(indicatorViewList.get(i).getY());
textViewList.get(i).setVisibility(View.INVISIBLE);
}
}
}
Below methods are called when menu item is selected or nothing is selected.
@Override
public void onMenuSelected(int position, String selectedText) {
tvResult.setText(String.format("Selected Item : %s", selectedText));
}
@Override
public void onNoMenuSelected() {
// TODO when no menu selected.....
tvResult.setText(String.format("Selected Item : %s", "None"));
}
Here is GitHub Link for this complete demo.
精彩评论