我已在此activity
中创建了一个HomePageActivity
名称activity
设计了xml,在此布局文件中,我在多个片段的帮助下加载数据。
以下是活动的布局文件:
<?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>
这是我的主要活动布局文件。如何在此添加导航抽屉。请帮我完成这件事。
TRIED CODE:
布局文件:
<?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);
}
}
答案 0 :(得分: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
的实例,并使用FragmentManager
和FragmentTransaction
将MainFragment
添加到FrameLayout
的{{1}} {1}}布局(带有@ + id / content_frame的布局)。
执行此操作意味着您的Activity
只有一个Activity
包含一个FrameLayout
(您的MainFragment),但Fragment
将成为所有其他观点(Fragment
和ScrollView
以及子TextViews
)。