我已经遵循了一堆用于在所有活动中实现相同NavigationDrawer的示例,但迄今为止没有成功。 我有2个问题:在所有活动中都具有相同的NavigationDrawer
1. 我定义了一个BaseActivity并覆盖了它的setContentView。 但在setContentView我似乎无法访问包含我的NavDrawer项目的ListView。 (drawerListView返回null)
- 我怎么重用我NavDrawer的代码?我在这里找不到一个好例子。 我不喜欢将DrawerLayout,FrameLayout和ListView添加到我的所有片段布局xmls中。 现在唯一的解决办法有NavigationDrawer在我所有的网页是在我脑海中是这样的:
我改变我的片段布局,使整个XML嵌入的帧结构是这样的: 使我所有的碎片布局都有自己的NavDrawer。这是我不喜欢的,我知道的是 一个不好的解决方案。 (我甚至不知道这是否会工作,并在我的BaseActivity我不会面对具有相同的ID(“drawerList”),并使用相同的ID很多FrameLayouts许多困难列表视图....
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
all my xml content to form a fragment
</FrameLayout>
<!-- Use any Views you like -->
<ListView
android:id="@+id/drawerList"
android:layout_width="240dp"
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#ffffff"
android:layout_gravity="left"
/>
</android.support.v4.widget.DrawerLayout>
那我该怎么办?有什么好处和明显的例子是高度赞赏。
的代码为我BaseActivity
public class BaseActivity extends ActionBarActivity implements OnItemClickListener
{
private DrawerLayout drawerLayout;
private String[] properties;
private ListView drawerListView;
protected DrawerLayout fullLayout;
protected FrameLayout actContent;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent2=new Intent(BaseActivity.this, ActivitySearchForSale.class);
startActivity(intent2);
}
@Override
public void setContentView(int layoutResID) {
fullLayout= (DrawerLayout) getLayoutInflater().inflate(layoutResID, null); // Your base layout here
actContent= (FrameLayout) fullLayout.findViewById(R.id.drawer_layout);
drawerListView = (ListView) findViewById(R.id.drawerList);
properties = getResources().getStringArray(R.array.propertyTypeArray);
drawerListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, properties));
drawerListView.setOnItemClickListener((OnItemClickListener) this);
getLayoutInflater().inflate(layoutResID, actContent, true); // Setting the content of layout your provided to the act_content frame
super.setContentView(fullLayout);
}
我所定义的DrawerLayout和我不为我的activity_main.xml中我的代码不知道如何将其包含在我的其他碎片布局中。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
style="@style/StyleForSimpleTextView"
android:text="zzzzz"
/>
</FrameLayout>
<!-- Use any Views you like -->
<ListView
android:id="@+id/drawerList"
android:layout_width="240dp"
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#ffffff"
android:layout_gravity="left"
/>
</android.support.v4.widget.DrawerLayout>
我activity_search_for_sale.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context="com.*.*.ActivitySearchForSale"
tools:ignore="MergeRootFrame" />
我fragment_search_for_sale
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<LinearLayout android:id="@+id/llSearchForSale"
style="@style/StyleForParentVerticalLinearLayouts">
.
.
.
</LinearLayout>
</ScrollView>
的活动
Java类:
public class ActivitySearchForSale extends BaseActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_for_sale);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, FragmentSearchForSale.newInstance(3)).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
而且我有一个Java类为我的片段,该片段onCreateView如同fo llows:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.fragment_search_for_sale, container, false);
return rootView;
}
感谢您的回答。 actContent =(FrameLayout)fullLayout.findViewById(R.id.mainContent);和drawerListView =(ListView)fullLayout.findViewById(R.id.drawerList); (从你的回答)努力获得列表视图。但是布局文件呢?我应该如何改变它们? – Tina 2014-10-06 13:20:14
这只适用于我的main_activity而不适用于其他 – Tina 2014-10-06 13:25:01
@Tina我已更新我的回答以反映您的第一条评论。您的第二条评论尚不清楚,请进一步解释。 – Simas 2014-10-06 13:28:50