- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序的 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
。
<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 。
<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
应该如下所示:
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/
当我打算执行 thirdactivity.class 时,Logcat 会这样说,这是什么意思?该 Activity 已建成。但是当模拟时,程序停止了。 11-11 10:17:25.824: E/A
我的代码显示此错误...AdapterView 是原始类型。 AdapterView 泛型类型 AdapterView 的引用应该被参数化,我该怎么办? public void onItemClick
我正在实现 AdapterView产生一个AbsListView -like 类,我可以使用 CursorAdapter在布局中。我正在实现这个,因为我想使用方便的自动数据更新行为 CursorAda
在我的小部件中,当 ListView 中的一个项目被长按时,我希望启动该项目(然而,在我目前的编码中,正常的点击也可以做到这一点,但现在我正在让长按做一些事情这样我就可以设置它以供以后需要更改长按的功
我创建了扩展“AdapterView”的类 A,并创建了扩展“BaseAdapter”以提供数据的类 D。 问题是每个 AdapterView 项目都没有将其长度拉伸(stretch)到 Adapte
所以我为我正在使用的饼图 View 创建了一个自定义 AdapterView。我不确定我做错了什么,但当我尝试旋转饼图时,饼图非常不稳定。我怀疑正在进行太多绘图,因为 traceview 显示对 Ca
我的 ListView 有一个 OnItemClickListener,如下所示: OnItemClickListener myClickHandler = new OnItemClickListen
我正在尝试(出于学习目的)我自己实现一个简单的 AdapterView,其中项目来自基本适配器(来自 sdk 示例的 ImageAdapter)。 实际代码是这样的: public class
在我的应用程序中,我实现了一个抽屉导航,它显示支票簿的银行账户列表。我编写了一个处理此列表的 AccountAdapter 类,并在我的 NavigationDrawerFragment 类中使用以下
我的应用程序的 MainActivity 中有一个抽屉导航。在此 Activity 中,设置了 AdapterView。当我单击适配器的某一行时,我希望打开一个新 Activity ,但位于同一个抽屉
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
我一直在关注许多关于如何优化像画廊这样的列表的教程,但是我无法让我的适配器重新循环 View 。这是我的代码: @Override public View getView(int position,
如果字符串显示在 listAdapter 的多个项目之一中,是否可以使用 android-espresso 进行验证? 尝试这样的事情: onData(anything()).inAdapterVie
这是我的问题,我有这样的类架构: Activity fragment RecyclerView(有一个适配器) 适配器: 有一个 ViewHolder 类: public static class N
我使用的是 Gallery View ,其中对应于每个项目的 View 非常重要,由文本和多个按钮组成。 当我单击以拖动画廊 View (不在其中一个按钮上的某处)时,按钮的可绘制状态变为已按下,并且
我有一个动态列表,我想在带有单选按钮的适配器 View 中显示该列表。在一组。 我用过: public class Type2Adapter extends BaseAdapter{ Cont
如标题:我想知道如何获取我的 AdapterView 中所有当前显示的项目的列表(数组)。 为什么?在我的 AdapterView 中显示的对象需要在用户关闭 AdapterView 后释放一个监听器
我尝试设置 AdapterView.OnItemClickListener 来工作,但它不断出现错误,我似乎找不到有效的答案。 mListView.setOnClickListener(new Ada
我创建了一个使用 LayoutInflater 的 Adapter arraylist,以便图像保持比例。我正在尝试对所选项目标题(即“牛肉”)进行简单的 toast 。一切都按预期工作,除了我从 t
我通过扩展 AdapterView 创建了一个自定义的 ListView,我想在这个 View 中处理关键事件。我已经覆盖了 onKeyDown,但从未看到它被调用。我什至尝试覆盖 dispatchK
我是一名优秀的程序员,十分优秀!