Fragment中的ListView ContextMenu

时间:2014-03-16 16:47:18

标签: android listview fragment contextmenu

在Fragment onCreateView中,我有以下代码

consumerlist=(ListView)view.findViewById(R.id.consumerlist);
    consumerlist.setAdapter(new consumerListAdapter(getActivity(), dataList));
    consumerlist.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listview, View view, int position,
                long id) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity(), "ID"+id, 1);

        }
    });
    Button btn=(Button)view.findViewById(R.id.button1);
    registerForContextMenu(consumerlist);
    return view;

如果我写registerForContextMenu(btn)然后它工作。但列表视图行长按没有做任何事情。即使我的listView onItemClick监听器不工作。如何修复它?任何人都非常感谢我。谢谢。提前谢谢。

2 个答案:

答案 0 :(得分:0)

试试这个:

consumerlist.setOnItemLongClickListener(new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
            int position, long id) {
        // TODO Auto-generated method stub

          registerForContextMenu( view );
          openContextMenu( view );        

        return false;
    }
});

this教程也应该对你有所帮助......

<强> //修改

移动

registerForContextMenu(consumerlist);

public void onActivityCreated(Bundle savedState) {

答案 1 :(得分:0)

删除布局中的scrollview或将其定义为不可聚焦使代码正常工作。