gpt4 book ai didi

android - 单击按钮更改 fragment

转载 作者:行者123 更新时间:2023-11-30 00:21:54 27 4
gpt4 key购买 nike

我试图在单击按钮时更改 fragment 。该按钮在一个 fragment 内,我想转到另一个 fragment 。此代码没有给出任何错误,但没有更改为所需的 fragment 。它只是显示容器的背景。请帮助我为什么它只显示容器的颜色而不是更改为新 fragment 。

这是我的第一个 fragment -

public class IntroFragment1 extends Fragment {

public IntroFragment1() {
// Required empty public constructor
}

public static IntroFragment1 newInstance() {
return new IntroFragment1();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_intro1, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

/**
* Button to go to next tab in tutorial
* */
Button nextScreen = getView().findViewById(R.id.nextTabButtonIntro);
nextScreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (vb1 != null) {
vb1.vibrate(300);
}

FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment frag = IntroFragment2.newInstance();
transaction.replace(R.id.containerIntroScreen, frag);
transaction.commit();
}
});

}//End of onViewCreated
}

这是第一个 fragment 的 XML。下一个按钮应该带我到下一个 fragment -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.example.fitbitsampleapp.AppIntroTabbedView.IntroFragment1">


<TextView
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="casual"
android:text="Track your daily activities. Stay healthy, Stay smart."
android:textSize="26dp" />

<Button
android:id="@+id/skipIntoButton"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:background="@drawable/background_button"
android:fontFamily="casual"
android:text="skip"
android:textAllCaps="true"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
android:textColor="#000" />

<Button
android:id="@+id/nextTabButtonIntro"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:background="@drawable/background_button"
android:fontFamily="casual"
android:text="next"
android:textAllCaps="true"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
android:textColor="#000" />

</LinearLayout>

这是 fragment ,我想去-

public class IntroFragment2 extends Fragment {

public IntroFragment2() {
// Required empty public constructor
}

public static android.support.v4.app.Fragment newInstance() {
IntroFragment2 fragment = new IntroFragment2();
return fragment;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {


// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_intro2, container, false);
}

第二个 fragment 的 XML-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
tools:context="com.example.fitbitsampleapp.AppIntroTabbedView.IntroFragment2">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:text="2nd fragment" />

</RelativeLayout>

这是具有 fragment 的 Activity 布局-

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.fitbitsampleapp.AppIntroTabbedView.IntroScreen">


<android.support.v4.view.ViewPager
android:background="#00F111"
android:id="@+id/containerIntroScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</android.support.design.widget.CoordinatorLayout>

因此,应用最初会按预期打开第一笔交易。但是,当我单击 fragment 1 中的下一个按钮时,它应该将我带到 fragment 2。但它只显示 fragment 事务后 ViewPager 的背景颜色。

编辑:这也是我的主要 Activity ,其中包含 fragment -

public class IntroScreen extends AppCompatActivity {
public static Vibrator vb1;
public Button nextScreen;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro_screen);

/***/
vb1 = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

/**
* Create the adapter that will return a fragment
* for each of the N primary sections of the activity.
* */
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

/** Set up the ViewPager with the sections adapter.*/
ViewPager mViewPager = findViewById(R.id.containerIntroScreen);
mViewPager.setAdapter(mSectionsPagerAdapter);

}


/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

private SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}


@Override
public android.support.v4.app.Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
switch (position) {
case 0:
return IntroFragment1.newInstance();

case 1:
return IntroFragment2.newInstance();

default:
return IntroFragment2.newInstance();
}
}


@Override
public int getCount() {
return 2;
}

}
}

最佳答案

由@MikeM 解决。在上面的评论中。

因为我已经在使用 ViewPager,所以所需要的只是为我的 ViewPager 提供正确的项目编号。

int THE_POSITION_OF_THE_FRAGMENT_IN_VIEW_PAGER = 1;
mViewPager.setCurrentItem(THE_POSITION_OF_THE_FRAGMENT_IN_VIEW_PAGER);

关于android - 单击按钮更改 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46136185/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com