【发布时间】:2016-08-05 02:56:34
【问题描述】:
我有一个扩展 AppCompatActivity 的活动 (MainActivity),因为我在我的应用中使用了一些材料设计元素。
然后我有一个带有几个字段和一个按钮的数组适配器。这个适配器有一个单独的视图,并被注入到我的 MainActivity 布局中。
当我单击适配器视图上的按钮时,我想打开一个显示一堆文本的新片段,但是,我似乎不能这样做,我认为这是因为我没有在我的主要活动?我在另一篇文章中读到,我应该能够扩展 AppCompatActivity 并且仍然能够引用片段管理器......这是我打开片段的代码:
在我的自定义数组适配器中,一个按钮的 onClick():
holder.desc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
JobDescFragment fragment= new JobDescFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
我得到的错误是它无法解析 getSupportFragmentManager()。我究竟做错了什么?
我正在适配器中导入 android.support.v4.app.Fragment 和 .FragmentManager。
提前感谢您的帮助!
编辑:
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<com.lorentzos.flingswipe.SwipeFlingAdapterView
android:id="@+id/frame"
android:background="@color/laborswipe_lightgray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
android:layout_gravity="top" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</merge>
【问题讨论】:
-
分享包含 fragment_container 的布局文件可能会有所帮助
标签: android android-layout android-fragments android-arrayadapter android-fragmentactivity