gpt4 book ai didi

Android 选项卡式 Activity : Action Bar Tabs with ViewPager: different layout for each tab

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

我完全停电了,我不熟悉使用 fragment 。

我用eclipse助手新建了一个Android项目:

enter image description here

这里是创建的默认类的 fragment :

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

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

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class
// below).
return PlaceholderFragment.newInstance(position + 1);
}

@Override
public int getCount() {
// Show 3 total pages.
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
static int sec;
private static final String ARG_SECTION_NUMBER = "section_number";

/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
sec = sectionNumber;
Log.i("ARG_SECTION_NUMBER"," "+sec);
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int jjj = sec;

Log.i("onCreateView"," "+sec);
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}

SectionsPagerAdapter 用作适配器并返回 Fragments3 个 fragment 都具有相同的布局

fragment_main.xml

如何将不同的布局分配给 3 个 fragment ?

PlaceholderFragment 包含一个带有 section_number 的 Bundle:

args.putInt(ARG_SECTION_NUMBER, sectionNumber);

我可以使用此信息来确定要在每个 fragment 中显示的布局吗?

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

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

最佳答案

您需要为每个页面制作不同的类。页面从 fragment 扩展。您可以为每个 fragment 加载不同的 layout-xml。

public class FirstFragment extends Fragment{

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


View rootView = inflater.inflate(R.layout.<yourxmlhere>, container,
false);
return rootView;
}

}

您知道 fragment ,但您的适配器仍需要知道哪个页面(位置)是哪个 fragment 。这是由以下函数决定的:

@Override
public Fragment getItem(int position) {
switch (position){
case 0:
//page 1
return new FirstFragment();
break;

case 1:
//page 2
return new SecondFragment();
break;
default:
//this page does not exists
return null;
}

确保您设置了正确的页数!

@Override
public int getCount() {
//the amount of pages your adapter knows
return <youramountofpages>;
}

这应该让您启动并运行。

编辑:您可以只删除整个 placeholderfragment 类。它不再需要了。

关于Android 选项卡式 Activity : Action Bar Tabs with ViewPager: different layout for each tab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28466936/

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