2014-06-09

【Android】context menu 浮動功能表

context menu 可以應用在任何 viewview 的子類別,但通常使用在 ListViewGridView,當使用者在 ListView 上一直按著某個項目不放(長按),就會跳出浮動功能表。

開發工具:ADT v22.6.2-1085508
target SDK:v4.1.2 JellyBean(API 16)
min SDK:v2.3.3 Gingerbread(API 10)

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.contextmenutest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.contextmenutest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



activity_main.xml
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.contextmenudemo.MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:longClickable="true" >

    </ListView>
    
</LinearLayout>


main_list_item.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/ListItem"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" 
 android:orientation="vertical">

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="text1"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    
</LinearLayout>


MainActivity.java
package com.example.contextmenudemo;

import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity 
{
    ListView listview;
    String[] values = new String[] { "item1", "item2", "item3", "item4", "item5" };
    int pos = -1;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        listview = (ListView) findViewById(R.id.listView1);
  
        final ArrayList<String> list = new ArrayList<String>();
        for(int i=0; i<values.length; ++i) 
        {
            list.add(values[i]);
        }

        MainListAdapter mAdapter = new MainListAdapter (this, list);
        listview.setAdapter(mAdapter);
  
        // 註冊浮動功能表給 view, 在本例為 listview
        registerForContextMenu(listview);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    // 點選功能表選項的回應碼
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        int id = item.getItemId();
   
        switch(id)
        {
            case R.id.action_settings:
                Toast.makeText(this, "list "+pos+" menu 設定", Toast.LENGTH_LONG).show();
                pos = -1;
                return true;
            case R.id.action_new:
                Toast.makeText(this, "list "+pos+" menu 新增", Toast.LENGTH_LONG).show();
                pos = -1;
                return true;
            case R.id.action_edit:
         Toast.makeText(this, "list "+pos+" menu 修改", Toast.LENGTH_LONG).show();
                pos = -1;
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
 
 
    // 建立浮動功能表
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
    {
        // 功能表 和 浮動功能表是可以不同的
        // 選項不同, 各自點擊選項時的反應也可不同
        // 不過在本例, 偷懶採用同一個功能表
        super.onCreateContextMenu(menu, v, menuInfo);
     
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        pos = info.position + 1;
  
        // 設定浮動功能表的標題欄
        menu.setHeaderTitle("選取 "+values[info.position]);
     
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
    }
 
    // 點選浮動功能表選項的回應碼
    @Override
    public boolean onContextItemSelected(MenuItem item) 
    {
        // 因為在本例, 浮動功能表 和 功能表 是同一個
        // 所以直接將參數傳給功能表的 onOptionsItemSelected()
        // 讓功能表去處理
        return onOptionsItemSelected(item);
    }

}


MainListAdapter.java
package com.example.contextmenudemo;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MainListAdapter extends BaseAdapter
{
    private Activity activity;
    private List<String> mTextView;
    private static LayoutInflater inflater = null;
    
    public MainListAdapter(Activity a, List<String> str)
    {
        activity = a;
        mTextView = str;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount()
    {
        return mTextView.size();
    }

    public Object getItem(int position)
    {
        return position;
    }

    public long getItemId(int position)
    {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View vi = convertView;
        if(convertView==null)
        {
            vi = inflater.inflate(R.layout.main_list_item, null);
        }
        
        TextView TextView1 = (TextView) vi.findViewById(R.id.TextView1);
        
        TextView1.setText(mTextView.get(position).toString());
        return vi;
    }
}


沒有留言:

張貼留言