我想在每个标签中设置不同的内容。我是android的新手。如何更改代码以便管理每个选项卡的内容?
我所拥有的:
问题是我在每个标签上都有相同的内容。
这是MainActivity
:
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener
{
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Here we load the xml layout we created above
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// 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);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++)
{
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
其余代码在MainActivity
内内部:
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
班级SectionPagerAdapter
:
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 new PlaceholderFragment();
}
@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;
}
}
PlaceholderFragment
包含gridview
:
public class PlaceholderFragment extends Fragment
{
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// Here we inflate the layout we created above
GridView gridView = (GridView) rootView.findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(MainActivity.this.getApplicationContext()));
return rootView;
}
}
和MyAdapter
我在其中添加了项目内容:
private class MyAdapter extends BaseAdapter
{
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("APO Supreme", R.drawable.snb_apo_supreme));
items.add(new Item("Arbor Blacklist", R.drawable.snb_arbor_blacklist));
items.add(new Item("Arbor Draft", R.drawable.snb_arbor_draft));
items.add(new Item("Arbor Relapse", R.drawable.snb_arbor_relapse));
items.add(new Item("Capita Defenders of Awesome", R.drawable.snb_capita_defenders_of_awesome));
items.add(new Item("Capita Outsiders", R.drawable.snb_capita_outsiders));
items.add(new Item("Capita Ultrafear", R.drawable.snb_capita_ultrafear));
items.add(new Item("DC Focus", R.drawable.snb_dc_focus));
items.add(new Item("DC Mega", R.drawable.snb_dc_mega));
items.add(new Item("DC Tone", R.drawable.snb_dc_tone));
items.add(new Item("ROME Agent", R.drawable.snb_rome_agent));
items.add(new Item("ROME Agent Rocker", R.drawable.snb_rome_agent_rocker));
items.add(new Item("ROME Hammerhead", R.drawable.snb_rome_hammerhead));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i)
{
return items.get(i);
}
@Override
public long getItemId(int i)
{
return items.get(i).picId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
TextView name;
if(v == null)
{
v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)getItem(i);
picture.setImageResource(item.picId);
name.setText(item.name);
return v;
}
private class Item
{
final String name;
final int picId;
Item(String name, int drawableId)
{
this.name = name;
this.picId = drawableId;
}
}
}
我知道它非常混乱且代码很多here's folder with project
P.S。我在Android Studio中工作
感谢您的帮助
答案 0 :(得分:2)
我认为你的问题是
@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 new PlaceholderFragment();
}
ViewPager必须从每个页面项返回不同的片段。
创建三个片段并为每个片段使用相同的布局。 然后将这些片段放入ArrayList并返回getItem方法所需的片段,取决于位置编号。
编辑: 例如:
private List<Fragment> mFragments = new Vector<Fragment>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_fragment_activity_layout);
mFragments.add(new FirstFragment());
mFragments.add(new SecondFragment());
...
...
}
在SectionsPagerAdapter中使用
@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 mFragments.get(position);
}
然后在FirstFragment和SecondFragment和其他片段中使用R.layout.activity_main
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_main, container, false);
GridView gridView = (GridView) v.findViewById(R.id.gridview);
return v;
}
现在每个片段中的onResume()方法在适配器中使用不同的自定义内容
public void onResume(){
gridView.setAdapter(new MyAdapter(getActivity()));
}
您应该为每个gridView使用自定义适配器来显示不同的信息,或者您可以传递适配器不同的内容。阅读有关适配器以及如何将数据内容传递到其中的更多信息。