簡體   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