android-fragments 如何在ListView项中长时间单击时调用上下文菜单?

hzbexzde  于 2022-11-13  发布在  Android
关注(0)|答案(2)|浏览(109)

家庭片段
进口品;

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,它有一个滑块菜单来打开一些片段,比如这个,列表在哪里,我想把上下文菜单放在哪里。
这里是我的适配器:

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。如有任何帮助,将不胜感激。

g6ll5ycj

g6ll5ycj1#

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

@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中以防万一)。

0qx6xfy6

0qx6xfy62#

首先按如下所示设置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

相关问题