简体   繁体   中英

how to call fragment from another fragment in tablayout with viewpager

I want to call a fragment on button click which is a fragment. I tried this code but it's not working. with this code when I click on the button it shows next fragment but not its designing. I see only a blank page.

This is my main xml :-

<LinearLayout
android:id="@+id/main_layout"
android:orientation="vertical"
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- our toolbar -->


<!-- our tablayout to display tabs  -->
<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="scrollable"
    app:tabGravity="fill"
    android:background="#fa4022"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"/>

I have an dashboard tab in this TabLayout. In this dashboard tab i have an button which id is userprofile. after click on this button i want to go on user_profile.xml

public class Dashboard extends Fragment implements View.OnClickListener {
    Button userprofile;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dashboard, container, false);
        userprofile=(Button)rootView.findViewById(R.id.userprofile);

        userprofile.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View view) {
        Fragment fragment = null;
        switch (view.getId()) {
            case R.id.userprofile:
                fragment = new User_Profile();
                replaceFragment(fragment);
                break;
    }
}


private void replaceFragment(Fragment fragment) {
        User_Profile user_profile = new User_Profile();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.pager, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
}

User_Profile class code is :-

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

}

user_profile.xml is :-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="User Profile"
    android:textSize="30dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"/>

If you can see fragment on button click but not designing then its may error in your fragment .xml file. By using proper view problem can be solve. If there is also a problem with same post their .xml file code.

Can you show us some more data please? What's the XML for the pager fragment you're trying to replace the current fragment with? Could you check that you're referring to the layout of that XML file, and not the individual pager element too? (Since transaction.replace requires a fragment container as it's first argument)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM