繁体   English   中英

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

[英]how to call fragment from another fragment in tablayout with viewpager

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

这是我的主要 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 是用户配置文件。 单击此按钮后,我想访问 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"/>

如果您可以在单击按钮时看到片段但没有进行设计,那么它可能会在您的片段.xml文件中出现错误。 通过使用适当的视图问题可以解决。 如果同样的帖子也有问题,他们的.xml文件代码。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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