在滑动菜单中切换片段?

时间:2013-08-01 20:46:35

标签: android android-fragments slidingmenu

有时候有人包含一些代码而没有解释。嗯,这次是这种情况,因为代码并不难。 我正试图在滑动抽屉中切换碎片。 代码:

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

这是DrawerItemClickListener类:

private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    Fragment frag = null;
    switch (position) {
    case 0:
        ft.replace(R.id.content_frame, Algeria);
        break;
    case 1:
        ft.replace(R.id.content_frame, Bahrain);

        break;
    case 2:
        ft.replace(R.id.content_frame, Comoros);
        break;
    case 3:
        ft.replace(R.id.content_frame, Djibouti);
        break;
    case 4:
        ft.replace(R.id.content_frame, Egypt);
        break;
    case 5:
        ft.replace(R.id.content_frame, Iraq);
        break;
    case 6:
        ft.replace(R.id.content_frame, Jordan);
        break;
    case 7:
        ft.replace(R.id.content_frame, Kuwait);
        break;
    case 8:
        ft.replace(R.id.content_frame, Lebanon);
        break;
    case 9:
        ft.replace(R.id.content_frame, Libya);
        break;
    case 10:
        ft.replace(R.id.content_frame, Mauritania);
        break;
    case 11:
        ft.replace(R.id.content_frame, Morocco);
        break;
    case 12:
        ft.replace(R.id.content_frame, Oman);
        break;
    case 13:
        ft.replace(R.id.content_frame, Pelastine);
        break;
    case 14:
        ft.replace(R.id.content_frame, Qatar);
        break;
    case 15:
        ft.replace(R.id.content_frame, Saudi_Arabia);
        break;
    case 16:
        ft.replace(R.id.content_frame, Somalia);
        break;
    case 17:
        ft.replace(R.id.content_frame, Sudan);
        break;
    case 18:
        ft.replace(R.id.content_frame, Syria);
        break;
    case 19:
        ft.replace(R.id.content_frame, Tunisia);
        break;
    case 20:
        ft.replace(R.id.content_frame, Emirates);
        break;
    case 21:
        ft.replace(R.id.content_frame, Yemen);
        break;
    }
    ft.commit();

}

但它不起作用 没有错误,只是屏幕上没有显示!我知道片段100%工作但它没有在屏幕上显示。那么为什么!? 谢谢。 :)

更新FUL代码

public class Drawer extends SherlockFragmentActivity {
    MenuListAdapter mMenuAdapter;
    String[] title;
    String[] subtitle;
    int[] icon;
    SharedPreferences prefs;
    public static String prefName = "SecretFile";
    Fragment Algeria = new Fragment1();
    Fragment Bahrain = new Fragment2();
    Fragment Comoros = new Fragment3();
    Fragment Djibouti = new Fragment4();
    Fragment Egypt = new Fragment5();
    Fragment Iraq = new Fragment6();
    Fragment Jordan = new Jordan();
    Fragment Kuwait = new Fragment8();
    Fragment Lebanon = new Lebanon();
    Fragment Libya = new Fragment10();
    Fragment Mauritania = new Fragment11();
    Fragment Morocco = new Fragment12();
    Fragment Oman = new Fragment13();
    Fragment Pelastine = new Fragment14();
    Fragment Qatar = new Qatar();
    Fragment Saudi_Arabia = new Fragment16();
    Fragment Somalia = new Fragment17();
    Fragment Sudan = new Fragment18();
    Fragment Syria = new Fragment19();
    Fragment Tunisia = new Fragment20();
    Fragment Emirates = new Fragment21();
    Fragment Yemen = new Fragment22();
    ConnectivityManager connMgr;
    NetworkInfo networkInfo;
    ListView mDrawerList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SlidingMenu sm = new SlidingMenu(this);
        title = getResources().getStringArray(R.array.country_array);
        icon = new int[] { R.drawable.algeria, R.drawable.bahrain,
                R.drawable.comoros, R.drawable.djibouti, R.drawable.egypt,
                R.drawable.iraq, R.drawable.jordan, R.drawable.kuwait,
                R.drawable.lebanon, R.drawable.libya, R.drawable.mauritania,
                R.drawable.morocco, R.drawable.oman, R.drawable.pelastine,
                R.drawable.qatar, R.drawable.saudiarabia, R.drawable.somalia,
                R.drawable.sudan, R.drawable.syria, R.drawable.tunisia,
                R.drawable.emirates, R.drawable.yemen, };
        sm.setMenu(R.layout.drawer_main);
        sm.setShadowWidthRes(R.dimen.shadow_width);
        sm.setShadowDrawable(R.drawable.shadow);
        sm.setMode(SlidingMenu.LEFT_RIGHT);
        sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        sm.setFadeDegree(0.7f);
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        sm.setSecondaryMenu(R.layout.menu_frame);
        sm.setSecondaryShadowDrawable(R.drawable.shadow);
        sm.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mMenuAdapter = new MenuListAdapter(this, title, icon);
        mDrawerList.setAdapter(mMenuAdapter);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    }

    private int seethefirstactivity() {
        // TODO Auto-generated method stub
        int fragmento;
        connMgr = (ConnectivityManager) this
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        networkInfo = connMgr.getActiveNetworkInfo();
        fragmento = prefs.getInt("country", 1);
        if (fragmento == 0) {
            Algeria = new Fragment1();

        } else if (fragmento == 1) {
            Bahrain = new Fragment2();

        } else if (fragmento == 2) {
            Comoros = new Fragment3();

        } else if (fragmento == 3) {
            Djibouti = new Fragment4();

        } else if (fragmento == 4) {
            Egypt = new Fragment5();

        } else if (fragmento == 5) {
            Iraq = new Fragment6();

        } else if (fragmento == 6) {
            Jordan = new Jordan();

        } else if (fragmento == 7) {
            Kuwait = new Fragment8();

        } else if (fragmento == 8) {

            Lebanon = new Lebanon();

        } else if (fragmento == 9) {
            Libya = new Fragment10();

        } else if (fragmento == 10) {
            Mauritania = new Fragment11();

        } else if (fragmento == 11) {
            Morocco = new Fragment12();

        } else if (fragmento == 12) {
            Oman = new Fragment13();

        } else if (fragmento == 13) {
            Pelastine = new Fragment14();

        } else if (fragmento == 14) {
            Qatar = new Qatar();

        } else if (fragmento == 15) {
            Saudi_Arabia = new Fragment16();

        } else if (fragmento == 16) {
            Somalia = new Fragment17();

        } else if (fragmento == 17) {
            Sudan = new Fragment18();

        } else if (fragmento == 18) {
            Syria = new Fragment19();

        } else if (fragmento == 19) {
            Tunisia = new Fragment20();

        } else if (fragmento == 20) {
            Emirates = new Fragment21();

        } else if (fragmento == 21) {
            Yemen = new Fragment22();

        }
        return fragmento;
    }

    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        Fragment frag = null;
        switch (position) {
        case 0:
            ft.replace(R.id.content_frame, Algeria);
            break;
        case 1:
            ft.replace(R.id.content_frame, Bahrain);

            break;
        case 2:
            ft.replace(R.id.content_frame, Comoros);
            break;
        case 3:
            ft.replace(R.id.content_frame, Djibouti);
            break;
        case 4:
            ft.replace(R.id.content_frame, Egypt);
            break;
        case 5:
            ft.replace(R.id.content_frame, Iraq);
            break;
        case 6:
            ft.replace(R.id.content_frame, Jordan);
            break;
        case 7:
            ft.replace(R.id.content_frame, Kuwait);
            break;
        case 8:
            ft.replace(R.id.content_frame, Lebanon);
            break;
        case 9:
            ft.replace(R.id.content_frame, Libya);
            break;
        case 10:
            ft.replace(R.id.content_frame, Mauritania);
            break;
        case 11:
            ft.replace(R.id.content_frame, Morocco);
            break;
        case 12:
            ft.replace(R.id.content_frame, Oman);
            break;
        case 13:
            ft.replace(R.id.content_frame, Pelastine);
            break;
        case 14:
            ft.replace(R.id.content_frame, Qatar);
            break;
        case 15:
            ft.replace(R.id.content_frame, Saudi_Arabia);
            break;
        case 16:
            ft.replace(R.id.content_frame, Somalia);
            break;
        case 17:
            ft.replace(R.id.content_frame, Sudan);
            break;
        case 18:
            ft.replace(R.id.content_frame, Syria);
            break;
        case 19:
            ft.replace(R.id.content_frame, Tunisia);
            break;
        case 20:
            ft.replace(R.id.content_frame, Emirates);
            break;
        case 21:
            ft.replace(R.id.content_frame, Yemen);
            break;
        }
        ft.commit();

    }

}

2 个答案:

答案 0 :(得分:0)

当我处理片段时,我就是这样做的:

MainActivity:

public void changeFragment(int frag, Fragment myNewFragment) {
        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        t.replace(frag, myNewFragment);
        //t.addToBackStack(null);
        t.commit();
}

我的 MyApplication类 y中的函数很小,所以我可以随时调用它们:

public void switchFragmentNormal(Activity a, Fragment newContent) {
        MainActivity fca = (MainActivity)a;
        fca.changeFragment(R.id.main_fragment, newContent);
}

所以这样,从任何片段或幻灯片菜单我都会做一个小小的通话,我会轻易改变它: 当我需要时,我会制作一个“新的东西()”:

FragmentVote fvote = new FragmentVote();
MyApp.switchFragmentNormal(getActivity(), fvote);

更改片段并关闭幻灯片“SlidingMenu- made by:jfeinstein10”这样做:

SlidingMenu sm = getSlidingMenu();
sm.setMode(SlidingMenu.LEFT_RIGHT); 
...
   public void changeFragmentFromList(int frag, Fragment myNewFragment) {
            sm.toggle();    // this closes the slidemenu
            FragmentTransaction t = getSupportFragmentManager().beginTransaction();
            t.replace(frag, myNewFragment);
            t.commit();
    }

并将其命名为: MyApplication类

public void switchFragmentNormalList(Activity a, Fragment newContent) {
        MainActivity fca = (MainActivity)a;
        fca.changeFragmentFromList(R.id.main_fragment, newContent);
}

然后:当我需要时,我会制作一个“新的东西()”:

FragmentVote fvote = new FragmentVote();
MyApp.switchFragmentNormalList(getActivity(), fvote);

然后有时我想添加堆叠我的片段,以便我可以做其他的东西等。

检查一下:

https://stackoverflow.com/a/17978786/2625912

希望能帮到你!

答案 1 :(得分:0)

好的,所以我觉得你没有多用过MyApplication类..没问题,我会告诉你:

MyApplication类我们可以说它是一个“常用”的可访问类,用于随时随地使用方法存储/恢复数据App:

让我们开始吧:

在你的清单中:

<application

        android:name=".MyApplication"

        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="myPackageName"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

如果您在代码中看到“ android:name =”。MyApplication“”表示您将在项目中创建的MyApplication类名。

现在我们只创建一个简单的类并扩展到“Application”

package youPackageName;

public class MyApplication extends Application {


/**
*  IN HERE WILL BE ALL YOUR VARIABLES AND METHODS
*/
}

那怎么用呢? 容易...

我们会说你想保存一个简单的字符串好吗?使用方法来获取和设置该字符串

    package youPackageName;

    public class MyApplication extends Application {

    String mString = "";

// To set my string 
public void setMyString(String s){
this.mString = s;
}
// get string 
public String getMyString(){
return this.mString;
}

// one you seted ones that string it will be abailable at any moment while your app is not destroyed

    }

从您的活动我们将在该app类中调用该方法如何?

MyApplication MyApp;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MyApp = ((MyApplication)getApplicationContext());


//SET
MyApp.setMyString("HELLO");

//GET
String a = MyApp.getMyString();

}

现在!破片!

我的布局非常简单:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<FrameLayout
        android:id="@+id/main_frag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>
中的

                           // myNewFragment: FRAG TO BE REPLACE
public void changeFragment(Fragment myNewFragment) {
        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        t.replace(R.id.main_frag, myNewFragment);
        t.commit();
}

在我们刚刚创建的MyApplication类中

public void switchFragmentNormal(Activity a, Fragment newContent) {
        MainActivity fca = (MainActivity)a;
        fca.changeFragment(newContent);
}

现在最后来自任何片段或SlideMenu

就这样做;

MyApplication MyApp;
public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

MyApp = (MyApplication)getActivity().getApplicationContext();

MyApp.switchFragmentNormal(getActivity(), new FragmentVote());
}

就是这样,现在你有一个可以在任何时候存储变量方法等的类。

: - )