【发布时间】:2020-01-21 22:14:00
【问题描述】:
我正在尝试在 ViewPager 中先替换片段。而且我有一个小问题,因为在替换后我仍然首先在背景片段中看到并且他们首先从片段中工作侦听器。我究竟做错了什么 ?我替换了 first_fragment 视图,但它不起作用。在其他项目中,我做了一些类似的事情,并且运行良好!
public class FirstFragment extends BaseFragment {
private RecyclerView pathwaysRecyclerView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pathways, container, false);
initializeViews(view);
initRecyclerView();
return view;
}
private void initializeViews(View view) {
pathwaysRecyclerView = view.findViewById(R.id.pathways_recyclerview);
}
private void initRecyclerView(){
pathwaysRecyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 2));
PathwaysAdapter adapter = new PathwaysAdapter(new OnPathwayClickListner(),requireContext());
pathwaysRecyclerView.setAdapter(adapter);
}
private class OnPathwayClickListner implements View.OnClickListener {
@Override
public void onClick(View view) {
navigateToSecondFragment();
}
}
private void navigateToSecondFragment(){
Fragment fragment = new SecondFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_pathways_view, fragment)
.addToBackStack(null)
.commit();
}
@Override
public void updateView() {
}
}
fragment_first.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_pathways_view">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/greyAccent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ebpHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/greyAccent"
android:orientation="vertical" >
<TextView
android:id="@+id/pathways_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/list_of_all_paths"
android:layout_margin="15sp"
app:fontFamily="sans-serif-light"
android:textSize="25sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/vertical_margin"
android:layout_marginStart="@dimen/vertical_margin"
android:layout_marginEnd="@dimen/vertical_margin"
android:layout_marginBottom="@dimen/beetwen_content_line_margin"
android:textSize="@dimen/textSizeMedium"
android:text="@string/list_of_all_paths_description"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/evaluation_background">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/pathways_recyclerview"
android:layout_marginTop="@dimen/beetwen_content_line_margin"
android:layout_marginStart="@dimen/vertical_margin"
android:layout_marginEnd="@dimen/vertical_margin"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
第二个片段
public class SecondFragment extends BaseFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_path, container, false);
return view;
}
}
fragment_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mainAppBackgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mainAppBackgroundColor"
android:orientation="vertical">
<TextView
android:id="@+id/path_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15sp"
app:fontFamily="sans-serif-light"
android:textSize="25sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/list_of_skills"
android:layout_marginTop="15sp"
android:layout_marginStart="15sp"
android:layout_marginEnd="15sp"
app:fontFamily="sans-serif-light"
android:textSize="18sp"
android:textStyle="bold"
/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/vertical_margin"
android:layout_marginTop="5sp"
android:layout_marginEnd="@dimen/vertical_margin" />
</LinearLayout>
</RelativeLayout>
ViewPager:
<com.habitcoach.android.activity.util.NonSwipeableViewPager
android:id="@+id/hsFragmentsContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:paddingBottom="50dp"
android:layout_alignParentLeft="true" />
页面适配器:
class MainViewViewPageAdapter extends FragmentStatePagerAdapter {
MainViewViewPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getItemPosition(Object object) {
BaseFragment f = (BaseFragment) object;
if (f != null) {
f.updateView();
}
return super.getItemPosition(object);
}
@Override
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0:
f = new OneFragment();
break;
case 1:
f = new TwoFragment();
break;
case 2:
f = new ThreeeFragment();
break;
case 3:
f = new FourFragment();
break;
}
return f;
}
@Override
public int getCount() {
return 4;
}
}
【问题讨论】:
-
它实际上与布局本身无关。你能分享你的 ViewPager 片段吗?
-
@EminGuliev 我添加了我的 ViewPager 布局和我的适配器
标签: java android android-fragments android-viewpager