我已经按照一系列示例在所有活动中实现相同的NavigationDrawer,但迄今为止没有成功。 我有两个问题:
1。 我已经定义了一个BaseActivity并覆盖了它的setContentView。 但是在setContentView中,我似乎无法访问包含我的NavDrawer项目的ListView。 (drawerListView返回null)
我更改了我的片段布局,以便整个xml嵌入到这样的帧布局中: 所以我的所有片段布局都有他们自己的NavDrawer。这就是我所做的和NT一样,我知道的是 糟糕的解决方案。 (我甚至不确定这是否有效,而且在我的BaseActivity中,我很难遇到许多具有相同ID的ListView(" 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);
}
我的activity_main.xml的代码,我已经定义了DrawerLayout,但我不知道如何在我的其他片段布局中包含它。
<?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如下:
@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;
}
答案 0 :(得分:1)
好像你忘了在刚膨胀的布局中找到drawerList
视图。
相反,您尝试findViewById
处理尚未设置的活动内容。
此外,您似乎试图将DrawerLayout转换为FrameLayout,这可能不是您想要的。
创建一个包含抽屉的布局(例如drawer_layout.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">
<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>
覆盖setContentView
中的BaseActivity
,如下所示:
@Override
public void setContentView(int layoutResID) {
// All activities share the drawer
drawer = (DrawerLayout) getLayoutInflater().inflate(R.layout.drawer_layout, null);
drawerListView = (ListView) fullLayout.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);
// After creating the drawer add the activity's layout as the first child
ViewGroup layout = getLayoutInflater().inflate(layoutResID, null);
drawer.addView(layout, 0);
super.setContentView(fullLayout);
}
现在您的activity_main.xml
将如下所示:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
style="@style/StyleForSimpleTextView"
android:text="zzzzz"/>
</FrameLayout>
你设置它:
setContentView(R.layout.activity_main);
这将适用于您的所有活动,只要他们extend BaseActivity
。