joh*_*gru 11 android android-fragments navigation-drawer
我正在使用developer.android.com指南来构建应用程序.当我在Android Studio中创建一个新项目时,我选择了"导航:导航抽屉".我在互联网上搜索了我的问题的答案,但我找不到任何有用的.对此抱歉,我是编程新手.
http://developer.android.com/design/patterns/navigation-drawer.html http://developer.android.com/training/implementing-navigation/nav-drawer.html
这就是我希望布局如下:

title_section*not section_title;)
小智 25
如今,导航抽屉是一种全新的趋势设计.我们使用两种布局:主要内容布局和抽屉列表布局,同时为导航抽屉活动设计xml.layout(布局).我在这里回答你所有愚蠢的问题.
单击导航抽屉时,如何让我的应用在主视图中打开一个新片段?
只需在抽屉列表项上添加clicklistener,并根据单击列表项的位置替换主要内容中的片段.
示例代码:
// The click listener for ListView in the navigation drawer
@SuppressWarnings("unused")
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) {
Fragment newFragment;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
switch (position) {
case 0:
newFragment = new f1();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case 1:
newFragment = new f2();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case 2:
newFragment = new f3();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case 3:
newFragment = new f4();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
}
//DrawerList.setItemChecked(position, true);
setTitle(ListTitles[position]);
DrawerLayout.closeDrawer(DrawerList);
}
Run Code Online (Sandbox Code Playgroud)
这里是f1,f2.f3和f4是不同的片段,每个片段都有自己的布局.你必须通过继承fragment类为它们创建单独的java类.
单击导航抽屉时,是否可以使用标签打开多个可滑动的片段?
为了在片段中实现选项卡,您可以在该特定片段中使用tabhost.假设您要在片段f_main中添加制表符.
F_main.xml的布局
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
Run Code Online (Sandbox Code Playgroud)
然后使用相应的布局和java类创建其他片段f_tab1和f_tab2.两个标签片段的布局可以相同或不同.在这里,我采用相同或共同的布局.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/google_map"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="MAP"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
F_tab1.java片段的代码
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class F_tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.friends_list, container,false);
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
另一个片段的代码.即F_tab2.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class F_tab2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.friends_list, container,false);
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,只需在前面提到的抽屉列表中使用clicklistener,即可在抽屉列表中的项目单击上加载F_main,这将进一步加载主内容视图中F_main fragmnet中的选项卡.
如何使"标题"可扩展/可折叠?
好吧,我不知道NV抽屉是否提供此功能.但它提供了一个功能,可根据所选的抽屉项目或加载的主要内容片段切换操作栏标题.
如导航抽屉设计指南中所述,您应该在抽屉可见时修改操作栏的内容,例如更改标题并删除与主要内容相关的操作项.下面的代码显示了如何通过使用ActionBarDrawerToggle类的实例覆盖DrawerLayout.DrawerListener回调方法来实现此目的,如下所示
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31174 次 |
| 最近记录: |