【问题标题】:Android: After long pressing a textview I need a Pop up Context MenuAndroid:长按文本视图后,我需要弹出上下文菜单
【发布时间】:2017-05-08 19:49:58
【问题描述】:

我使用 Recyclerview 扩展了 Fragment 类。长按 textView 后我需要一个弹出菜单。我已经显示了下面的代码。没有错误,所以我不知道再看哪里了。

public class RecyclerViewFragment  extends Fragment implements View.OnCreateContextMenuListener {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        RecyclerView recyclerView = (RecyclerView) inflater.inflate(
                R.layout.recycler_view, container, false);
        ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return recyclerView;
    }



    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
        public ImageView avator;
        public TextView name;
        public TextView description;
        public TextView num;
        public View divider;
        public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.listing_view, parent, false));
            avator = (ImageView) itemView.findViewById(R.id.listing_avatar);
            name = (TextView) itemView.findViewById(R.id.listing_title);
            description = (TextView) itemView.findViewById(R.id.listing_desc);
            num = (TextView) itemView.findViewById(R.id.listing_num) ;
            divider= itemView.findViewById(R.id.listing_div);
            itemView.setOnCreateContextMenuListener(this);
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            if (v.getId()==R.id.list) {
                //AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
                menu.setHeaderTitle("Action");
                String[] menuItems = getResources().getStringArray(R.array.arr_menu);
                for (int i = 0; i<menuItems.length; i++) {
                    menu.add(Menu.NONE, i, i, menuItems[i]);
                }
            }
        }
    }

【问题讨论】:

  • 回收器视图如何扩展片段类?

标签: android android-fragments android-recyclerview contextmenu long-press


【解决方案1】:

tv.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        return false;
    }

});

使用 OnLongClickListener 代替 OnClickListner

https://developer.android.com/guide/topics/ui/menus.html

【讨论】:

    【解决方案2】:

    希望对你有帮助。

    onCreate()
    

    registerForContextMenu(textView1);

    @Override   
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)  
        {  
                super.onCreateContextMenu(menu, v, menuInfo);  
                menu.setHeaderTitle("Select The Action");    
                menu.add(0, v.getId(), 0, "Call");//groupId, itemId, order, title   
                menu.add(0, v.getId(), 0, "SMS");   
        }   
        @Override    
        public boolean onContextItemSelected(MenuItem item){    
                if(item.getTitle()=="Call"){  
                    Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();  
                }    
                else if(item.getTitle()=="SMS"){  
                    Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();  
                }else{  
                   return false;  
                }    
              return true;    
          }    
    

    【讨论】:

      猜你喜欢
      • 2014-05-04
      • 2017-11-07
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      相关资源
      最近更新 更多