在片段中的recyclerview中处理长按

时间:2019-03-14 17:04:26

标签: android android-recyclerview

我想处理来自片段的recyclerView项目中的longclick。 我写了一些代码来处理contxtMenu。我已经在string.xml中创建了菜单数组。我不想在主要活动中处理这个问题,在该活动中,我从navigatiodrawer调用了此片段。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
    View rootview=inflater.inflate(R.layout.activity_classes,null);

    classes_recyclerView = rootview.findViewById(R.id.classes_recyclerView);
    classes_recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    final ClassesAdapter mAdapter = new ClassesAdapter(rootview.getContext(), getClasses());

    ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
            classes.remove(viewHolder.getAdapterPosition());
            mAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
        }
    };

    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
    itemTouchHelper.attachToRecyclerView(classes_recyclerView);

    classes_recyclerView.setAdapter(mAdapter);
    registerForContextMenu(classes_recyclerView);

    return rootview;
}

在同一片段中,我已经覆盖了功能

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if(v.getId() == R.id.classes_recyclerView){
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        selectedClassName = classes.get(info.position).getClassName();
        menu.setHeaderTitle(selectedClassName);
        String[] popupMenuItems = getResources().getStringArray(R.array.modify_popup_menu);
        for(int i=0; i < popupMenuItems.length; i++){
            menu.add(Menu.NONE, i , i , popupMenuItems[i]);
        }
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    int menuItemIndex = item.getItemId();
    String[] menuItems = getResources().getStringArray(R.array.modify_popup_menu);
    String menuItemName = menuItems[menuItemIndex];
    switch (menuItemName){
        case "Edit":
            changeClassInfoDialog = new Dialog(context);
            changeClassInfoDialog.setContentView(R.layout.modify_class_info_layout);

            EditText className = changeClassInfoDialog.findViewById(R.id.className_txtV);
            classIcon = changeClassInfoDialog.findViewById(R.id.class_icon);
            FloatingActionButton changeClassIcon_FB = changeClassInfoDialog.findViewById(R.id.changeClassIcon_floatingBtn);
            changeClassIcon_FB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_IMAGE );
                }
            });


            break;
        case "Delete":
            break;
    }

    return true;
}

0 个答案:

没有答案