我的应用程序的 MainActivity 中有一个抽屉导航。 在此 Activity 中,设置了 AdapterView。 当我单击适配器的某一行时,我希望打开一个新 Activity ,但位于同一个抽屉导航下:
searchResultList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(MainActivity.this, Details.class));
}
});
现在,新 Activity (详细信息)已打开,但不显示抽屉式导航栏。
请告诉我如何做到这一点,而无需为每个 Activity 编写抽屉导航的代码...
提前致谢。
最佳答案
嘿,在你看答案之前,你应该看看这个 Android dev Summit Video ,它会让您知道该怎么做,而且它还有一种轻松实现抽屉导航布局的新方法。
为此,您有两个选择,第一个也是最好的选择是使用 Fragments
而不是您的 Activities
并拥有一个包含您的 的
和一个将包含“Fragment” View 的 FragmentActivity
DrawerLayoutFrameLayout
。
FragmentActivity.java
<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">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
这就是 Android 文档中有关使用 DrawerLayout
的内容,您可以找到更多详细信息 here .
如果您想知道如何在代码中将 Fragments
添加到 Activity
中,您需要执行以下操作:
FragmentManager fm = getSupportFragmentManager();
/*if you are using a FragmentActivity or an AppCompatActivity as
your super class you'll need to use getSupportFragmentManager
method*/
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.YOUR_FRAMELAYOUT_ID, fragment, fargmentTag);
/*Here you should add your frameLayout ID as your first argument
Thats where your new fragment will reside on the screen its like
an iframe.
This will replace the current fragment shown in your frameLayout if any and
add the new one*/
ft.addToBackStack(null);
/*You can add your fragment to your backstack and assign a String value
as its key if you want to get back to it in the future*/
ft.commit();
//By commiting you apply the Fragment transaction and start
//drawing the Fragment on screen
<小时/>
另一个选项是拥有一个 Parent Activity
,其中包含 DrawerLayout
的代码和 View 设置,并且您可以从该 ParentActivity
进行扩展您的 Activity
。您还需要使用一个 parent_activity.xml
,它将您的 DrawerLayout
作为主标记,并有一个 ViewStub
来添加您的每个其中的 Activity
View 。
parent_activity.xml
<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">
<ViewStub
android:id="@+id/layout_stub"
android:inflatedId="@+id/myActivityLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
您应该继承的 ParentActivty
应该如下所示:
ParentActivty.java
public class ParentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parent_activity);
}
public void setUpChildActivityView(int childActivityView){
ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
stub.setLayoutResource(childActivityView);
View inflated = stub.inflate();
}}
您可以从新的Activity
调用setUpChildActivtiyView()
方法来设置您的内容,您也可以在不使用ViewStub的情况下获得相同的结果
code> 但使用 addView() 膨胀 并添加新的
方法。Activity
View 到 ParentActivity
View
我个人更喜欢第一个选项,因为它是文档中的约定,但您可以采用任何一种方式。
关于java - 将 AdapterView 与抽屉导航结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33944197/