gpt4 book ai didi

android - 同一屏幕中具有多个 fragment 的抽屉导航

转载 作者:行者123 更新时间:2023-11-30 02:45:21 25 4
gpt4 key购买 nike

我创建了一个 activity 名称 HomePageActivity 在此 activity 中设计了 xml,在此布局文件中我正在加载数据多个 fragment 。

这是 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>

这是我的主要 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" >
<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>

Activity 代码:

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);

}

最佳答案

您可以尝试以下...

使用标准的 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 实例,并使用 FragmentManagerFragmentTransaction 添加 MainFragmentActivity 布局(带@+id/content_frame 的那个)的 FrameLayout

这样做意味着您的 Activity 将只有一个 FrameLayout 包含一个 Fragment(您的 MainFragment),但那个 Fragment 将成为所有其他 View (ScrollViewTextViews 以及子 Fragments)的父级。

关于android - 同一屏幕中具有多个 fragment 的抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25134305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com