如何使用viewpager从tablayout中的另一个片段调用片段

时间:2017-09-25 15:24:49

标签: android

我想在点击按钮时调用片段,这是一个片段。我试过这段代码,但它没有用。使用此代码,当我单击按钮时,它显示下一个片段,但不显示其设计。我只看到一个空白页面。

这是我的主要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"/>

我在这个TabLayout中有一个仪表板标签。在此仪表板选项卡中,我有一个id为userprofile的按钮。单击此按钮后,我想继续使用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类代码是: -

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是: -

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

2 个答案:

答案 0 :(得分:0)

如果您可以看到按钮上的片段而不是设计,那么它可能会在您的片段.xml文件中出错。通过使用正确的视图问题可以解决。如果同一帖子的.xml文件代码也存在问题。

答案 1 :(得分:0)

您能告诉我们更多数据吗?您尝试用当前片段替换的寻呼机片段的XML是什么? 你能检查一下你是指那个XML文件的布局,而不是单个的pager元素吗? (因为transaction.replace需要一个片段容器作为它的第一个参数)