gpt4 book ai didi

java - 我如何从 fragment 打开新布局并返回 fragment

转载 作者:行者123 更新时间:2023-11-29 22:48:05 25 4
gpt4 key购买 nike

我有一个 fragment 有 3 种布局:A.java, B.java, C.java, Newpage.Java, Main.Java

和布局:act_main, act_a, act_b, act_c, act_newpage

我想通过单击按钮从 fragment 创建和打开新布局,然后再次返回 fragment 。我试过了,但没有用。我有什么错?

    //in Main.Java
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(3);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}

private void setupTabIcons() {
tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("A");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.A_40_negative, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);

tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("B");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.B_40_negative, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);

tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText("C");
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.C_40_negative, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabTwo);
}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new A(), "A");
adapter.addFrag(new B(), "B");
adapter.addFrag(new C(), "C");
viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

//in A.Java;
public class A extends Fragment {
public A() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.act_A, container, false);

btnOpenNewLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Newpage.class);
startActivity(intent);
}
});
return view;
}
}


//in Newpage.Java;
public class Newpage extends Fragment{

public Newpage() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.act_newpage, container, false);
return view;
}
}

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.blabla.myapp">
<dist:module dist:instant="true" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/MyMaterialTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Newpage">
</activity>
</application>

</manifest>
error:android.content.ActivityNotFoundException: Unable to find explicit activity class {.......}; have you declared this activity in your AndroidManifest.xml?

我该怎么做?怎么了?感谢您的帮助。

最佳答案

改变

//in Newpage.Java;
public class Newpage extends Fragment{

public Newpage() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.act_newpage, container, false);
return view;
}
}

//in Newpage.Java;
public class Newpage extends Activity{

public Newpage() {
}
public View onCreate(Bundle savedInstanceState) {
setContentView(R.layout.act_newpage);
}
}

要返回上一个 Activity ,您需要在单击按钮时调用 finish()

关于java - 我如何从 fragment 打开新布局并返回 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58163455/

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