如何避免在 SlidingDrawer 中按两次相同的列表视图项?

How to avoid pressing same listview item twice in SlidingDrawer?

我的应用程序中有一个 Sliding Drawer,并且有像 HomeFragmentFriendFragment etc.Now 这样的片段 HomeFragment 有一个 map。因此,当我处于 HomeFragment 时,即它被按下,然后如果我再次单击同一个按钮,它 crashes.I 在我的 onCreateView()

中有以下代码
 View v = inflater.inflate(R.layout.home_map_activity, null, false);

logcat中的错误是:

 Caused by: java.lang.IllegalArgumentException: Binary XML file line #46: Duplicate id 0x7f0a008a, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
            at android.app.Activity.onCreateView(Activity.java:4722)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at com.example.MAPit.MAPit.HomeFragment.onCreateView(HomeFragment.java:79)
            at android.app.Fragment.performCreateView(Fragment.java:1695)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
            at android.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
            at android.app.FragmentManagerImpl.run(FragmentManager.java:441)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)



所以我检查了 HomeFragmentonCreateView

if (v != null) {
            ViewGroup parent = (ViewGroup) v.getParent();
            if (parent != null){
               parent.removeView(v);
                }
        }
        try {
             v = inflater.inflate(R.layout.home_map_activity, null, false);
        } catch (InflateException e) {
            //I have a toast check here and it's printing with vacant map and not crashing
        }

但是进行此检查时,碰巧应用程序没有崩溃,但地图没有出现在 HomeFragment 中。我调试并看到它没有膨胀布局,即它正在抛出 InflateException .然后我尝试在我的 HomeFragment 中添加这部分但又崩溃了。

 public void onDestroyView() {
        super.onDestroyView();
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
        FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (map != null) {
            map = null;
        }
    }

我试图禁用我的列表视图选项,但它没有 worked.what 我试过的是

if(listview.getChildAt(selectedPosition).isEnabled())
{
    listview.getChildAt(selectedPosition).setEnabled(false);
}

logcat中的错误:

 Caused by: java.lang.NullPointerException
            at com.example.MAPit.MAPit.SlidingDrawerActivity.displayView(SlidingDrawerActivity.java:201)
            at com.example.MAPit.MAPit.SlidingDrawerActivity.onCreate(SlidingDrawerActivity.java:138)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access0(ActivityThread.java:141)

我的listview onclick实现代码:

private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            // display view for selected nav drawer item
            displayView(position);
        }
    }

    /**
     * Displaying fragment view for selected nav drawer list item
     */
    private void displayView(int position) {

        switch (position) {
            case 0:
                fragment = new HomeFragment();
                getFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
                break;
            case 1:
                fragment = new Friend_Search_Fragment();
                getFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
                break;
            case 2:
                fragment = new Groups_Fragment();
                getFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
                break;
            case 3:
                fragment = new Friend_Request_Fragment();
                getFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
                Bundle frienddata = new Bundle();
                frienddata.putString(Commands.Notification_job.getCommand(), Commands.Friends_Request.getCommand());
                fragment.setArguments(frienddata);
                break;
            case 4:
                fragment = new Friend_Request_Fragment();
                getFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
                Bundle groupdata = new Bundle();
                groupdata.putString(Commands.Notification_job.getCommand(), Commands.Group_Join_Group.getCommand());
                fragment.setArguments(groupdata);
                break;
            case 5:
                fragment = new MyWallFragment();
                getFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
                break;
            case 6:
                Intent intent = new Intent(SlidingDrawerActivity.this,SignIn.class);
                startActivity(intent);
                break;

            default:
                break;
        }
        startFragment(fragment, position);
    }

    private void startFragment(Fragment fragment, int position) {
        mDrawerLayout.closeDrawer(mDrawerLinear);
        if (fragment != null) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.frame_container,fragment);
            transaction.addToBackStack(null);
            transaction.commit();


            // update selected item and title, then close the drawer
            if (position != -1) {
                mDrawerList.setItemChecked(position, true);
                mDrawerList.setSelection(position);

            }
            /*if(position == 0){
                if(mDrawerList.getChildAt(1)!=null)
                mDrawerList.getChildAt(1).setEnabled(false);
            }*/
            //setTitle(navMenuTitles[position]);
            //mDrawerLayout.closeDrawer(mDrawerLinear);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }

我不知道如何防止在 slidingdrawer.Can 的列表视图中两次单击同一项目 谁能告诉我任何路径?提前致谢。

FragmentManager fm= getFragmentManager();
int count = fm.getBackStackEntryCount();
for (int i = 0; i < count; ++i) {
    fm.popBackStackImmediate();
}

你可以尝试在这之前添加上面的代码 FragmentTransaction 事务 = getFragmentManager().beginTransaction() 行