如何在ListView项目中长按单击上下文菜单?

时间:2014-02-20 17:29:44

标签: java android listview android-fragments

HomeFragment

进口;

public class HomeFragment extends Fragment {

// Declare Variables
    ListView list;
    TextView text;
    ListViewAdapter adapter;
    EditText editsearch;
    String[] title;
    String[] date;
    String[] status;
    ArrayList<ListCourse> arraylist = new ArrayList<ListCourse>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        date = new String[] { "11/01/2011", "10/05/2006", "12/07/2002", "05/04/2011", "01/08/2012",
                          "09/12/2017", "22/06/2024", "31/01/2000", "10/10/2156", "10/02/2006" };

        title = new String[] { "Programação", "Matemática", "Logística",
            "Mobile", "Sistemas Operativos", "iOS", "Android", "Windows",
            "Hardware", "Formação" };

        status = new String[] { " ongoing ", " ongoing ",
            " ongoing ", " standby ", " ongoing ", " ongoing ",
            " ongoing ", " ongoing ", " finished ", " ongoing " };

        // Locate the ListView in listview_main.xml

        list = (ListView) rootView.findViewById(R.id.listview);

        for (int i = 0; i < title.length; i++) 
        {
            ListCourse wp = new ListCourse(date[i], title[i],
                status[i]);
            // Binds all strings into an array
            arraylist.add(wp);
        }

        // Pass results to ListViewAdapter Class
        adapter = new ListViewAdapter(getActivity(), arraylist);

        // Binds the Adapter to the ListView
        list.setAdapter(adapter);

       return rootView;
    }
}

我在上下文菜单中只需要3个项目:喜欢,评论和收藏。我尝试实现各种教程无济于事,我的项目包含一个MainActivity,它有一个slidermenu来打开一些像这样的片段,列表就在这里,我想放置上下文菜单。

这是我的适配器:

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context mContext;
    LayoutInflater inflater;
    private List<ListCourse> coursepopulatelist = null;
    private ArrayList<ListCourse> arraylist;

    public ListViewAdapter(Context context, List<ListCourse> coursepopulatelist) {
        mContext = context;
        this.coursepopulatelist = coursepopulatelist;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<ListCourse>();
        this.arraylist.addAll(coursepopulatelist);
    }

    public class ViewHolder {
        TextView title;
        TextView date;
        TextView status;
    }

    @Override
    public int getCount() {
        return coursepopulatelist.size();
    }

    @Override
    public ListCourse getItem(int position) {
        return coursepopulatelist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.listview_item, null);
            // Locate the TextViews in listview_item.xml
            holder.title = (TextView) view.findViewById(R.id.title);
            holder.date = (TextView) view.findViewById(R.id.date);
            holder.status = (TextView) view.findViewById(R.id.status);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Set the results into TextViews
        holder.title.setText(coursepopulatelist.get(position).getTitle());
        holder.date.setText(coursepopulatelist.get(position).getDate());
        holder.status.setText(coursepopulatelist.get(position).getStatus());

        // Listen for ListView Item Click
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Send single item click data to SingleItemView Class
                Intent intent = new Intent(mContext, SingleItemView.class);
                // Pass all data rank
                intent.putExtra("title",(coursepopulatelist.get(position).getTitle()));
                // Pass all data country
                intent.putExtra("date",(coursepopulatelist.get(position).getDate()));
                // Pass all data population
                intent.putExtra("status",(coursepopulatelist.get(position).getStatus()));
                // Pass all data flag
                // Start SingleItemView Class
                mContext.startActivity(intent);
            }
        });

        return view;
    }

    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        coursepopulatelist.clear();
        if (charText.length() == 0) {
            coursepopulatelist.addAll(arraylist);
        } 
        else 
        {
            for (ListCourse wp : arraylist) 
            {
                if (wp.getDate().toLowerCase(Locale.getDefault()).contains(charText)) 
                {
                    coursepopulatelist.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }

}

更改是应该放在适配器还是片段中?我尝试了几次OnCreateContextMenu和ContextMenuSelectedItem无法让它在片段上工作,也尝试了OnLongItemClick。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

您必须在ListView中设置setOnItemLongClickListener():

lv.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int pos, long id) {

                // Do stuff here

                return true;
            }
        }); 

此外,您需要将其设置在不在适配器内的列表中(如果这是您的疑问)

编辑:您需要在onCreate()方法中执行此操作。

答案 1 :(得分:0)

首先将您的listvieW设置为跟随myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        myListView.setAdapter(adapter);

然后注册MultiChoiceModeListener并根据自己的喜好覆盖他的方法:)

        myListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
               // here you will inflate your CAB
                return true;
            }

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

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {

            }
        });

    }

Here is the link

我希望它有所帮助

修改:添加可以帮助您的链接

答案 2 :(得分:0)

我设法让一个上下文菜单工作,就在这里:

       @Override
       public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
        adapter = new ListViewAdapter(getActivity(), arraylist);
        Object item = adapter.getItem(info.position);

        menu.setHeaderTitle("Opções");
        menu.add(0, v.getId(), 0, "Like");
        menu.add(1, v.getId(), 0, "Comment");
        menu.add(2, v.getId(), 0, "Favorite");

        }


        @Override   
        public boolean onContextItemSelected(MenuItem item) {
        if (item.getTitle() == "Like") {
            addLike(item.getItemId());
        } else if (item.getTitle() == "Comment") {

        } else if (item.getTitle() == "Favorite") {
            // code
        } else {
            return false;
        }
        return true;

        }

        public void addLike(int id){

        }

在HomeFragment中返回rootView之后。还必须在listview.xml上添加android:longClickable =“true”,否则它将无法工作(我将其放入listviewitem.xml中以防万一)。