我创建了ActionBarTab
,我需要在女ListView
tab
中添加ListViews
,listView
有差异数据。
但我无法在Tabs
中显示我的MainActivity.xml
。
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
static String [] Years;
static ArrayAdapter<String> adapter;
static ListView Lists;
public static Context context;
public static Activity activity;
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
private String[] tabs = { "tab 1", "tab 2", "tab 3","tab 4","tab 5" };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(tabs[i])
.setTabListener(this));
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
Years = context.getResources().getStringArray(R.array.YEAR);
Lists = (ListView) activity.findViewById(R.id.list);
adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1, Years);
Lists.setAdapter(adapter);
case 1:
case 2:
case 3:
case 4:
default:
return null;
}
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
return "Section " + (position + 1);
}
}
}
:
switch case 0 :
注意:在ListView
我无法显示activity_main.xml
。
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</android.support.v4.view.ViewPager>
:
simple_list_item_1.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
:
string-array
这是我string.xml
中的listView
,我从这里填写<string-array name="YEAR">
<item>1992</item>
<item>1993</item>
<item>1994</item>
<item>1995</item>
<item>1996</item>
<item>1997</item>
<item>1998</item>
<item>1999</item>
</string-array>
:
{{1}}
答案 0 :(得分:1)
fragment_section_listview.xml
:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</FrameLayout>
注意:从ListView
删除activity_main.xml
将其更改为:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
然后在class
:
MainActivity
public static class LstView extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_listview, container, false);
Years = context.getResources().getStringArray(R.array.YEAR);
Lists = (ListView) rootView.findViewById(R.id.list);
adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1, Years);
Lists.setAdapter(adapter);
return rootView;
}
}
然后write
中的switch case
:
switch (i) {
case 0:
return new LstView();
Other Code .....