Problem with replace fragment in ViewPage

I am trying replace fragment first on second in ViewPager. And I have small problem becouse after replace still i see in background fragment first and they are working listner from fragment first. What am i doing wrong ? I replace first_fragment view but it doesn't work. In other project i were done somthing simular and it's working well !

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>

second fragment

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" />

PageAdapter:

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;
}
}

1 answer

  • answered 2020-01-22 13:24 user5607081

    I modified your code.check below solution.

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager viewPager = (ViewPager) findViewById(R.id.hsFragmentsContainer);
        viewPager.setAdapter(new MainViewViewPageAdapter(getSupportFragmentManager()));
    }}
    

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    <android.support.v4.view.ViewPager
        android:id="@+id/hsFragmentsContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    MainViewViewPageAdapter.java

    public class MainViewViewPageAdapter extends FragmentStatePagerAdapter {
    public MainViewViewPageAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public int getItemPosition(@NonNull Object object) {
        return super.getItemPosition(object);
    }
    @Override
    public Fragment getItem(int position) {
        Fragment f = null;
        switch (position) {
            case 0:
                f = new FirstFragment();
                break;
            case 1:
                f = new SecondFragment();
                break;
        }
        return f;
    }
    @Override
    public int getCount() {
        return 2;
    }}
    

    FirstFragment.java

    public class FirstFragment extends Fragment {
    private RecyclerView pathwaysRecyclerView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, 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);
    }
    
    public class OnPathwayClickListner implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            navigateToSecondFragment();
        }
    }
    
    private void navigateToSecondFragment(){
        Fragment fragment = new SecondFragment();
        getFragmentManager().beginTransaction().replace(R.id.fragment_pathways_view, fragment).addToBackStack(null).commit();
    }}
    

    SecondFragment.java

    public class SecondFragment  extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_second, container, false);
        return view;
    }
    

    }

    PathwaysAdapter.java

    class PathwaysAdapter extends RecyclerView.Adapter<PathwaysAdapter.MyViewHolder>{
    FirstFragment.OnPathwayClickListner onPathwayClickListner;
    Context requireContext;
    ArrayList<String> list;
    public PathwaysAdapter(FirstFragment.OnPathwayClickListner onPathwayClickListner, Context requireContext) {
        this.onPathwayClickListner=onPathwayClickListner;
        this.requireContext=requireContext;
        list=new ArrayList<>();
        for (int i=1;i<=20;i++){
            list.add("Pathway "+i);
        }
    }
    @NonNull
    @Override
    public PathwaysAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row, viewGroup, false);
        return new PathwaysAdapter.MyViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull PathwaysAdapter.MyViewHolder myViewHolder, int i) {
        myViewHolder.textview.setText(list.get(i));
    }
    
    @Override
    public int getItemCount() {
        return list.size();
    }
    
    class MyViewHolder extends RecyclerView.ViewHolder {
         RelativeLayout layout;
         TextView textview;
         public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textview = itemView.findViewById(R.id.textview);
            layout = itemView.findViewById(R.id.layout);
            layout.setOnClickListener(onPathwayClickListner);
        }}}
    

    row.xml

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="#FF9800"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="5dp"
    card_view:cardUseCompatPadding="true">
    <RelativeLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="100dp">
        <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="#ffffff" />
    
    </RelativeLayout>