带适配器的上下文操作栏

时间:2013-03-17 18:07:56

标签: android contextual-action-bar

我第一次尝试使用上下文操作栏,我遇到了这个设计问题: 在我的主要活动中,我已将适配器设置到我的列表中:

    // Create an adapter
    Adapter = new ToDoItemAdapter(this, 
                                  R.layout.list_date_item, 
                                  toDoListCursor, 
                                  from, 
                                  to,
                                  toDoDBAdapter);
    List.setAdapter(Adapter);

现在我需要setOnLongClickListener每个列表项。我在哪里可以做到这一点,如果我在我的活动中写它我不能像我在适配器中那样访问每个'视图'但是如果我在Adapter类中写它我没有访问ActionMode.Callback或更重要的是 - 我的逻辑方法,如 - RemoveItem,ShareItem等。

我想知道这样做的最佳做法是什么。

2 个答案:

答案 0 :(得分:2)

我已在项目点击ListView中完成此操作。

注意:我使用过actionbarsherlock库。

enter image description here

check this code:

package com.example.testapp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class MainActivity extends SherlockListActivity {

    private static final String[] COUNTRIES = new String[] { "Belgium",
            "France", "Italy", "Belgium", "France", "Italy", "Belgium",
            "France", "Italy" };

    private static final Integer[] img = new Integer[] {
            R.drawable.ic_launcher, R.drawable.dhaval1, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.dhaval1, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.dhaval1, R.drawable.ic_launcher };

    private static final String[] COUNTRIES_Detail = new String[] {
            "Belgium , officially the Kingdom of Belgium, is a federal state in Western Europe.",
            "France (English , officially the French Republic (French: République française ",
            "Italy i/ˈɪtəli/ (Italian: Italia [iˈtaːlja]), officially the Italian Republic ",
            "Belgium , officially the Kingdom of Belgium, is a federal state in Western Europe.",
            "France (English , officially the French Republic (French: République française ",
            "Italy i/ˈɪtəli/ (Italian: Italia [iˈtaːlja]), officially the Italian Republic ",
            "Belgium , officially the Kingdom of Belgium, is a federal state in Western Europe.",
            "France (English , officially the French Republic (French: République française ",
            "Italy i/ˈɪtəli/ (Italian: Italia [iˈtaːlja]), officially the Italian Republic " };
    private MyArrayAdapter adapter;
    Bundle savedInstanceState;

    private ActionMode mMode;

    private static ListView list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        list = getListView();
        adapter = new MyArrayAdapter(MainActivity.this, COUNTRIES,
                getListView());
        getListView().setAdapter(adapter);
        if (COUNTRIES.length <= 0) {
            getListView().setEmptyView(findViewById(android.R.id.empty));
        }

    }

    protected void onRestart() {
        // adapter.notifyDataSetChanged();
        onCreate(savedInstanceState);
    };

    class MyArrayAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        private Context mcon;
        private String[] COUNTRIES_;
        private ListView listView_;

        public MyArrayAdapter(Activity con, String[] countries,
                ListView listView) {
            // TODO Auto-generated constructor stub
            mcon = con;
            COUNTRIES_ = countries;
            listView_ = listView;
            mInflater = LayoutInflater.from(con);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return COUNTRIES_.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final ListContent holder;
            View v = convertView;
            if (v == null) {
                v = mInflater.inflate(R.layout.my_spinner_style, null);
                holder = new ListContent();
                holder.line = (LinearLayout) v.findViewById(R.id.line_);
                holder.name = (TextView) v.findViewById(R.id.textView1);
                holder.name1 = (TextView) v.findViewById(R.id.textView2);
                holder.name2 = (ImageView) v.findViewById(R.id.imageView1);

                v.setTag(holder);
            } else {

                holder = (ListContent) v.getTag();
            }

            holder.name.setText("" + Html.fromHtml("" + COUNTRIES_[position]));
            holder.name1.setText("" + Html.fromHtml("" + COUNTRIES_[position]));
            // holder.name2.setImageBitmap();

            holder.name.setOnClickListener(mOnTitleClickListener3);

            return v;
        }

    }

    class ListContent {

        TextView name;
        TextView name1;
        ImageView name2;
        LinearLayout line;

    }

    public OnClickListener mOnTitleClickListener3 = new OnClickListener() {
        public void onClick(View v) {
            final int position = list.getPositionForView((View) v.getParent());

            mMode = startActionMode(new AnActionModeOfEpicProportions());
            Log.d("you are click on Ratings", "you are click on Ratings");

        }
    };

    public class AnActionModeOfEpicProportions implements ActionMode.Callback {
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Used to put dark icons on light action bar

            menu.add("Save").setIcon(

            R.drawable.ic_launcher)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

            menu.add("Search").setIcon(R.drawable.ic_launcher)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

            menu.add("Search").setIcon(R.drawable.ic_launcher)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

            menu.add("Refresh").setIcon(R.drawable.ic_launcher)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

            return true;
        }

        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            Toast.makeText(MainActivity.this, "Got click: " + item,
                    Toast.LENGTH_SHORT).show();
            mode.finish();
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
        }

    }

}

activity_main.xml中

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

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

listview行文件my_spinner_style.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:id="@+id/line_"
    android:orientation="horizontal"
    android:weightSum="3" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

我使用OnClickListener的方式与OnLongClickListener

相同
public OnLongClickListener longclick = new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            mMode = startActionMode(new AnActionModeOfEpicProportions());
            Log.d("you are click on Ratings", "you are click on Ratings");
            return true;
        }
    };

编辑:没有actionbarsherlock

使用AppCompart支持Lib使用

startSupportActionMode

没有AppCompart

startSupportActionMode

答案 1 :(得分:-1)

我认为在你的情况下最好的方法是这样的

    List.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override

            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, lon id) {
//              TextView childItem = (TextView) view.findViewById(android.R.id.text1);
                return true;
            }
        });