码迷,mamicode.com
首页 > 编程语言 > 详细

MenuAcitivity.java

时间:2016-03-18 09:26:17      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

package com.example.droid;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MenuActivity extends Activity {
    private Button btn;
    private ListView listView;
    private List list;
    private BaseAdapter adapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("onCreate", "布局创建 kaishi");
        setContentView(R.layout.activity_menu);
        
        btn = (Button)findViewById(R.id.btn);
//        registerForContextMenu(btn);
        
        listView = (ListView) findViewById(R.id.listView);
        list = newList(10);
        adapter = new MenuAdapter(this, list);
        listView.setAdapter(adapter);
        registerForContextMenu(listView);
        Log.d("onCreate", "布局创建");
    }
    
    private List<String> newList (int count) {
        ArrayList<String> list = new ArrayList<String>();
        int i = 0;
        for (i = 0; i < count; ++i) {
            list.add("Item " + i);
        }
        return list;
    }
        
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        String text = null;
        switch (item.getItemId()) {
            case R.id.action_settings:     text = "选项"; break;
            case R.id.create_new:         text = "新建"; break;
            case R.id.create_open:         text = "打开"; break;
            default: text = "NOTHING"; break;
        }
        Toast.makeText(MenuActivity.this, text, Toast.LENGTH_SHORT).show();
        Log.d("onOptionsItemSelected", text);
        return true;
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(ContextMenu.NONE, 1, 1, "打开");
        menu.add(ContextMenu.NONE, 2, 2, "删除");
        Log.d("onCreateContextMenu", "构建上下文菜单");
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        super.onContextItemSelected(item);
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        int position = info.position;
        switch (item.getItemId()) {
            case 1:
                Toast.makeText(this, "打开", Toast.LENGTH_SHORT);
                break;
            case 2:
                list.remove(position);
                adapter.notifyDataSetChanged();
                break;
        }
        Log.d("onContextItemSelected", "Item被选择");
        return true;
    }
    
    class MenuAdapter extends BaseAdapter {
        Context context;
        List<String> list;
        LayoutInflater inflater;
        
        public MenuAdapter (Context context, List<String> list) {
            this.context = context;
            this.list = list;
            inflater = LayoutInflater.from(context);
        }
        
        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }
    
        @Override
        public Object getItem(int index) {
            return null;
        }
    
        @Override
        public long getItemId(int index) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            Log.d("getView", "渲染MenuItem");
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_explorer, null);
                holder = new ViewHolder();
                holder.textView = (TextView) convertView.findViewById(R.id.textView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.textView.setText(list.get(position));
            return convertView;
        }

    }
    
    class ViewHolder {
        TextView textView;
        Button button;
    }
}

 

MenuAcitivity.java

标签:

原文地址:http://www.cnblogs.com/thens/p/5290722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!