【问题标题】:Navigation Drawer with multiple fragments in same screen在同一屏幕中具有多个片段的导航抽屉
【发布时间】:2014-08-05 08:28:48
【问题描述】:

我在这个activity 中创建了一个activity 名称HomePageActivity 设计了xml,并且在这个布局文件中我正在借助多个片段加载数据。

这里是activity的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/homePageBackground"
android:orientation="vertical" >

<fragment
    android:id="@+id/announcementHomefragment"
    android:name="in.app.AnnouncementFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/homePageBackground" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:background="@color/homePageBackground" >

        <TextView
            android:id="@+id/newsTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:text="@string/new_list"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/white"
            android:textStyle="bold" />

        <fragment
            android:id="@+id/newshomefragment"
            android:name="in.app.NewsFragment"
            android:layout_width="wrap_content"
            android:layout_height="190dp"
            android:layout_below="@id/newsTitle"
            android:layout_marginTop="-15dp" />

        <TextView
            android:id="@+id/productTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/newshomefragment"
            android:layout_gravity="center"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:text="@string/product_in_home"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/white"
            android:textStyle="bold" />

        <fragment
            android:id="@+id/proCategoryhomefragment"
            android:name="in.app.CategoryFragment"
            android:layout_width="wrap_content"
            android:layout_height="170dp"
            android:layout_below="@id/productTitle"
            android:layout_marginTop="-15dp" />

        <TextView
            android:id="@+id/trainingTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/proCategoryhomefragment"
            android:layout_gravity="center"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="2dp"
            android:gravity="center"
            android:text="@string/trainings_in_home"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/white"
            android:textStyle="bold" />

        <fragment
            android:id="@+id/trainingfragment"
            android:name="in.app.TrainingFragment"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:layout_below="@id/trainingTitle"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="-15dp" />
    </RelativeLayout>
</ScrollView>

</LinearLayout>

这是我的主要活动布局文件。我如何在此添加导航抽屉。请帮我完成这项工作。

试用代码:

布局文件:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/homePageBackground"
android:orientation="vertical" >
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="265dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#181818"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:paddingLeft="5dp" />
</android.support.v4.widget.DrawerLayout>

<fragment
    android:id="@+id/announcementHomefragment"
    android:name="in.app.AnnouncementFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/homePageBackground" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:background="@color/homePageBackground" >

        <TextView
            android:id="@+id/newsTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:text="@string/new_list"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/white"
            android:textStyle="bold" />

        <fragment
            android:id="@+id/newshomefragment"
            android:name="in.app.NewsFragment"
            android:layout_width="wrap_content"
            android:layout_height="190dp"
            android:layout_below="@id/newsTitle"
            android:layout_marginTop="-15dp" />

        <TextView
            android:id="@+id/productTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/newshomefragment"
            android:layout_gravity="center"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:text="@string/product_in_home"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/white"
            android:textStyle="bold" />

        <fragment
            android:id="@+id/proCategoryhomefragment"
            android:name="in.app.CategoryFragment"
            android:layout_width="wrap_content"
            android:layout_height="170dp"
            android:layout_below="@id/productTitle"
            android:layout_marginTop="-15dp" />

        <TextView
            android:id="@+id/trainingTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/proCategoryhomefragment"
            android:layout_gravity="center"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="2dp"
            android:gravity="center"
            android:text="@string/trainings_in_home"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/white"
            android:textStyle="bold" />

        <fragment
            android:id="@+id/trainingfragment"
            android:name="in.app.TrainingFragment"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:layout_below="@id/trainingTitle"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="-15dp" />
    </RelativeLayout>
</ScrollView>

</LinearLayout>

活动代码:

public class HomePageActivity extends Activity {
public static String cookie;
SharedPreferences sharedPreferences;
ActionBar actionBar;
Editor editSharedPreferences;
// private SwipeRefreshLayout mSwipeRefreshLayout;
Activity activity;
private static String trackerId;
Tracker t;
private static Boolean isPitchAlreadyRunning;

List<String> dataList;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

protected void onCreate(Bundle savedInstanceState) {
    activity = (Activity) HomePageActivity.this;
    actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(true);
    trackerId = getResources().getString(R.string.tracker_id);
    t = ((GoogleAnalyticsApplication) getApplication())
            .getTracker(GoogleAnalyticsApplication.TrackerName.APP_TRACKER);
    // Force to the Application to show the Action Overflow
    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class
                .getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {
        // Ignore
        ex.printStackTrace();
    }

    dataList = new ArrayList<String>();
    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

    dataList.add("Announcement");
    dataList.add("Profile");
    dataList.add("Logout");

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        SelectItem(0);
    }

    sharedPreferences = getSharedPreferences(
            BConstant.B_LOGIN_CHECK, BConstant.PRIVATE_MODE);
    cookie = sharedPreferences.getString(
            BConstant.WEB_SERVICES_COOKIES, "");
    Boolean isFromSplashScreen = sharedPreferences.getBoolean(
            BConstant.FROM_SPLASH_SCREEN, false);
    editSharedPreferences = sharedPreferences.edit();

    if (isFromSplashScreen) {
        overridePendingTransition(R.anim.slide_in_right,
                R.anim.slide_out_left);
        editSharedPreferences.putBoolean(BConstant.FROM_SPLASH_SCREEN,
                false);
        editSharedPreferences.commit();
    }
    isPitchAlreadyRunning = sharedPreferences.getBoolean(
            BConstant.IS_PITCH_ALREADY_RUNNING, false);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.landing_page);

}

@Override
public void onStart() {
    super.onStart();
    // EasyTracker.getInstance(this).activityStart(this);
    // Get an Analytics tracker to report app starts & uncaught exceptions
    // etc.
    if (!trackerId.equals(BConstant.DEFAULT_TRACKERID)) {
        t.setScreenName("Home Page");

        t.send(new HitBuilders.AppViewBuilder().build());
    }
}

@Override
public void onStop() {
    super.onStop();
    // EasyTracker.getInstance(this).activityStop(this);
    // Stop the analytics tracking
    if (!trackerId.equals(BConstant.DEFAULT_TRACKERID))
        GoogleAnalytics.getInstance(this).reportActivityStop(this);
}

@Override
public void onResume() {
    super.onResume();
    isPitchAlreadyRunning = sharedPreferences.getBoolean(
            BConstant.IS_PITCH_ALREADY_RUNNING, false);
    invalidateOptionsMenu();
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem pitchStartedMenu = menu.findItem(R.id.pitchStratInTraining);
    if (!isPitchAlreadyRunning) {
        pitchStartedMenu.setVisible(false);
    } else {
        pitchStartedMenu.setVisible(true);
    }
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //Some logic is here
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Some logic here
    return true;
}

public void SelectItem(int possition) {

    switch (possition) {
    case 0:
        Intent intent = new Intent(this, HomePageActivity.class);
        startActivity(intent);
        break;

    default:

        break;
    }

    mDrawerList.setItemChecked(possition, true);
    // set the title of the page
    // setTitle(dataList.get(possition).getItemName());
    mDrawerLayout.closeDrawer(mDrawerList);

}

}

【问题讨论】:

  • 您的抽屉式导航代码在哪里?据我所知,这是不可能的。我什至检查了谷歌游戏商店,它们甚至都没有。我尝试了很多,但 itemclicklistner 不起作用。寻找正确答案
  • 不要指望别人给你代码。尝试先自己实现导航抽屉,如果遇到搜索并尝试解决方案的问题,请提出问题
  • 让我粘贴我已经尝试过的内容
  • @IllegalArgument 请查看我更新的问题

标签: android android-fragments navigation-drawer slidingdrawer


【解决方案1】:

您可以尝试以下...

使用标准的DrawerLayout 布局文件作为您的Activity 布局文件,换句话说就是以下...

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="265dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#181818"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:paddingLeft="5dp" />
</android.support.v4.widget.DrawerLayout>

创建一个扩展 Fragment 的新类(例如称为 MainFragment)。使用您最初为 Activity 设置的布局文件(带有ScrollView 的文件)作为 MainFragment 的布局。

在您的Activity 中创建MainFragment 的实例并使用FragmentManagerFragmentTransactionMainFragment 添加到Activity 布局的FrameLayout(带有@+id/content_frame 的那个) )。

这样做意味着您的 Activity 将只有一个 FrameLayout 包含一个 Fragment(您的 MainFragment),但 Fragment 将成为所有其他视图的父级(ScrollView 和 @ 987654339@ 和孩子Fragments)。

【讨论】:

  • @Squnok 如何在片段中使用片段?
  • @rup35h :允许嵌套Fragments。我不是 100% 确定它会与您的布局文件一起使用,但请尝试一下。如果没有,则将 &lt;fragment ... /&gt; 声明替换为 FrameLayouts,并在 MainFragment 中使用 getChildFragmentManager() 创建 Fragments 的实例,并使用 FragmentTransactions 将它们添加到 FrameLayouts
  • 仔细阅读本教程:androidbegin.com/tutorial/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多