gpt4 book ai didi

android - 我如何在android中的选项卡式 Activity 中管理布局文件?

转载 作者:行者123 更新时间:2023-11-29 19:44:16 24 4
gpt4 key购买 nike

解释:

我在创建项目时进行了选项卡式 Activity 。其中 viewpager 中的所有 fragment 只有一个布局文件。我想为不同的 fragment 设置不同的布局,并使用动态 fragment 动态添加选项卡。

假设,我有 5 个大小,那么将创建五个选项卡,如果我有 2 个,则将创建两个选项卡。

这是我的 MainActivity.java,里面还有内部类

public class MainActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;

public ViewPager mViewPager;

String[] title={"SHOES","BAG","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

mViewPager = (ViewPager) findViewById(R.id.container);

mSectionsPagerAdapter.noOfFragment(10);
mSectionsPagerAdapter.addPageTitle(title);
mViewPager.setAdapter(mSectionsPagerAdapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {

private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {
}

public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);

return rootView;
}
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

int val=0;
List<String> pageTitle=new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {

return PlaceholderFragment.newInstance(position);
}

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

}

public void addPageTitle(String[] title){
for(int i=0;i<title.length;i++){
pageTitle.add(title[i]);
}
}

@Override
public CharSequence getPageTitle(int position) {
return pageTitle.get(position);
}
public void noOfFragment(int size){
val=size;
}
}
}

在上面的代码中我已经创建了一个动态标签。我想为每个标签分开布局。

请帮我解决一下

最佳答案

您应该为每个 View 创建一个 fragment 并在 onViewChanged 上设置 View

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.YOUR_VIEW, container, false);
}

你应该创建你的自定义适配器,它看起来像这样

public class MyAdapter extends FragmentPagerAdapter {

List<Fragment> fragments;
List<String> titles;

public MyAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
super(fm);

this.fragments = fragments;
this.titles = titles;

}


@Override
public Fragment getItem(int position) {
return fragments.get(position);
}

@Override
public int getCount() {
return fragments.size();
}

@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}

然后你的 Activity 你应该创建 fragment 列表并将它传递给适配器

    MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(), fragmentList, titleList);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
TabLayout tabLayout= (TabLayout) findViewById(R.id.tabs);
viewPager.setAdapter(myAdapter);
tabLayout.setupWithViewPager(viewPager);

关于android - 我如何在android中的选项卡式 Activity 中管理布局文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38095576/

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