Android 从作为ViewPager一部分的片段创建新片段
我看到了许多似乎相关的问题,但我没有找到一个正是我所寻找的 我有一个应用程序,在MainActivity中有一个ViewPager。 ViewPager中的一个片段是ListFragment。 我想在用户单击列表项时启用新片段的创建, 使用onListItemClick函数。 新片段将替换ViewPager,并显示其布局和数据。 我还想允许单击“上一步”按钮,这将使用户返回到PageViewer的上一个状态Android 从作为ViewPager一部分的片段创建新片段,android,android-fragments,android-viewpager,android-listfragment,Android,Android Fragments,Android Viewpager,Android Listfragment,我看到了许多似乎相关的问题,但我没有找到一个正是我所寻找的 我有一个应用程序,在MainActivity中有一个ViewPager。 ViewPager中的一个片段是ListFragment。 我想在用户单击列表项时启用新片段的创建, 使用onListItemClick函数。 新片段将替换ViewPager,并显示其布局和数据。 我还想允许单击“上一步”按钮,这将使用户返回到PageViewer的上一个状态 @Override public void onListItemClick(ListVi
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
StationDescriptionFragment newFragment = new StationDescriptionFragment();
Bundle args = new Bundle();
args.putString(StationDescriptionFragment.ARG_DESCRIPTION, this.stationsList.get(position).getDescription());
newFragment.setArguments(args);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
getListView().setItemChecked(position, true);
}
此代码位于片段内部,片段是ViewPager的一部分。
此代码结束,然后应用程序在该函数结束后立即崩溃。我确信这个函数结束了
坠机原因是:
FragmentManagerImpl.throwException(RuntimeException) line: 462
StationDescriptionFragment类:
public class StationDescriptionFragment extends Fragment {
final static String ARG_DESCRIPTION = "description";
String mCurrentDescription = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
mCurrentDescription = savedInstanceState.getString(ARG_DESCRIPTION);
}
return inflater.inflate(R.layout.description_view, container, false);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
updateDescription("Some Description");
}
public void updateDescription(String description) {
TextView desc = (TextView) getActivity().findViewById(R.id.description);
desc.setText(description);
mCurrentDescription = description;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(ARG_DESCRIPTION, mCurrentDescription);
}
}
description\u view.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textSize="18sp" />
任何帮助都将不胜感激。
谢谢
新片段将替换ViewPager,并显示其
布局和数据。我还想允许单击后退按钮,这
将用户返回到PageViewer的上一个状态
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
StationDescriptionFragment newFragment = new StationDescriptionFragment();
Bundle args = new Bundle();
args.putString(StationDescriptionFragment.ARG_DESCRIPTION, this.stationsList.get(position).getDescription());
newFragment.setArguments(args);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
getListView().setItemChecked(position, true);
}
如果您想将ViewPager
(我假设它在另一个片段中)与其页面片段一起替换,那么请使用正常的FragmentManager
,而不是由getChildFragmentManager()
返回的片段,该片段旨在实现嵌套片段getChildFragmentManager()
将在当前片段的视图中查找该id(以放置新片段),我假设R.id.fragment\u container
是使用ViewPager
保存片段的容器的id:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
getListView().setItemChecked(position, true); // call this before the transaction
StationDescriptionFragment newFragment = new StationDescriptionFragment();
Bundle args = new Bundle();
args.putString(StationDescriptionFragment.ARG_DESCRIPTION, this.stationsList.get(position).getDescription());
newFragment.setArguments(args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
这个崩溃没有帮助,你能发布完整的堆栈跟踪吗?非常感谢你的回答-就像你说的,问题是getChildFragmentManager。我对新片段中未显示的文本有问题。desc.setText(说明);似乎不起作用。我会设法弄明白的。再次感谢你,你真的帮了我。@gioravered我很高兴能帮上忙。出现该问题的一个可能原因是使用
getActivity()搜索TextView
代码>(这是导致许多细微错误的原因)。如果在活动中的其他地方有一个带有该id的TextView
,您可能最终会更新该id,而不是片段中的TextView
。如果要更改片段视图中的视图,请始终使用getView()
:TextView desc=(TextView)getView().findViewById(R.id.description)代码>。可能不是你的情况,但试试看。谢谢你的提示。我已经试过了,但仍然没有文本。片段会自动替换,但不会显示文本。而且,它只替换特定的片段,视图寻呼机仍然保留。我想用一个常规片段替换整个视图寻呼机。有什么想法吗?我最终在一个对话框中显示了这个字符串-这对应用程序来说更好,而且它确实有效!再次感谢!