我正在试图弄清楚如何在每个标签中显示两个不同的数组列表,因为此时我的行星和星星数组列表并显示在我的所有标签下。
如果我为每个标签创建一个活动+类,那么我确实可以正确地使用它,但这样做太多了,无法为每个标签创建一个活动+类
如何在不同的标签中显示每个标签,例如行星标签下的行星数组列表和星标下标签是星形数组列表,但只是为所有标签重复使用相同的活动,但只为每个标签填充新的文本数据< / p>
这是我的主要活动:
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
mPager= (ViewPager) findViewById(R.id.viewPager);
mTabs=(SlidingTabLayout) findViewById(R.id.tabs);
mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
mTabs.setDistributeEvenly(true);
mTabs.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
mTabs.setViewPager(mPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class MyPagerAdapter extends FragmentPagerAdapter {
String[] tabs;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabs=getResources().getStringArray(R.array.tabs);
}
@Override
public Fragment getItem(int position) {
MyFragment myFragment=MyFragment.getInstance(position);
return myFragment;
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
@Override
public int getCount() {
return 5;
}
}
public static class MyFragment extends Fragment {
private TextView textView;
private ListView mainListView;
private ArrayAdapter<String> listAdapter ;
public static MyFragment getInstance(int position){
MyFragment myFragment=new MyFragment();
Bundle args=new Bundle();
args.putInt("position", position);
myFragment.setArguments(args);
return myFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout=inflater.inflate(R.layout.fragment_my, container, false);
mainListView=(ListView) layout.findViewById(R.id.mainListView);
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );
String[] stars = new String[] { "Eridanus", "Cassiopeia", "Scorpius", "Crux",
"Cancer", "Leo", "Canis Major", "Taurus"};
ArrayList<String> starsList = new ArrayList<String>();
planetList.addAll( Arrays.asList(stars) );
listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.simplerow, planetList);
mainListView.setAdapter( listAdapter );
return layout;
}
}
}
答案 0 :(得分:0)
您可以使用基于pagerposition的开关案例,如
ArrayList<String> planetList = new ArrayList<String>();
switsh(pagerposition){
Case 0:
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
planetList.addAll( Arrays.asList(planets) );
break;
Case 1:
String[] stars = new String[] { "Eridanus", "Cassiopeia", "Scorpius", "Crux", "Cancer", "Leo", "Canis Major", "Taurus"};
planetList.addAll( Arrays.asList(stars) );
break;
and so on.....
}