Android 对多个对象使用泛型类(actionBar选项卡)
目前,我使用ABS、ActionBar选项卡和TabsAdapter/ViewPager为我的应用程序创建了一个漂亮的选项卡布局。我在顶部有5个以上的分类标签-最终用户将能够添加新的分类(我稍后会设置)。因此,目前,我有一个主要的Android 对多个对象使用泛型类(actionBar选项卡),android,android-layout,tabs,android-actionbar,actionbarsherlock,Android,Android Layout,Tabs,Android Actionbar,Actionbarsherlock,目前,我使用ABS、ActionBar选项卡和TabsAdapter/ViewPager为我的应用程序创建了一个漂亮的选项卡布局。我在顶部有5个以上的分类标签-最终用户将能够添加新的分类(我稍后会设置)。因此,目前,我有一个主要的SherlockFragmentActivity,其中包含许多SherlockFragment类别文件。在针对主SFA的onCreate中,我构建了actionBar并添加了所有选项卡,如下所示: mTabsAdapter.addTab(bar.newTab().set
SherlockFragmentActivity
,其中包含许多SherlockFragment
类别文件。在针对主SFA的onCreate中,我构建了actionBar并添加了所有选项卡,如下所示:
mTabsAdapter.addTab(bar.newTab().setText(R.string.login),
LoginFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Geographics"),
GeoFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.economics),
EconFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.elections),
ElectionsFragment.class, null);
我想做的是创建一个新的解决方案,CategoryFragment
,而不是使用所有特定的选举、地理位置、经济等。有人能想象一个解决方案吗理想情况下,我只想将一个字符串传递给添加的选项卡,这样CategoryFragment就可以根据该字符串进行膨胀。我喜欢这种解决方案,因为在多个类中,代码非常冗余,而类真正做的只是从SQL dB online加载内容,只获取其自身类别的数据
这是我的TabsAdapter类:
public class TabsAdapter extends FragmentPagerAdapter
implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private Polling activity;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
final class TabInfo {
private final Class<?> clss;
private final Bundle args;
//private final String title;
//This string is implemented only as part of my attempt!
TabInfo(Class<?> _class, Bundle _args, String _title) {
clss = _class;
args = _args;
title = _title;
}
}
/*Constructor method that adds a TabsAdapter to each tab that is created.
* It also adds the ViewPager to each tab so that the user can swipe to change tabs.
*/
public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
this.activity = (Polling) activity;
mActionBar = activity.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
/*This is the method I've been trying to use to solve the problem, but it's not really cutting it!*/
public void buildTabs() throws ClassNotFoundException {
String [] tabs = {"Econ", "Elections", "Geo", "Politics", "Science", "Finance", "Religion",
"Military", "International" };
final String resource = "R.string.";
mTabsAdapter.addTab(bar.newTab().setText("Login"),
LoginFragment.class, null, "Login");
for (int j = 0; j < tabs.length; j++) {
String res = resource + tabs[j];
String clas = tabs[j] + "Fragment";
String total = "com.davekelley.polling." + clas;
mTabsAdapter.addTab(bar.newTab().setText(tabs[j]),
CategoryFragment.class, null, tabs[j]);
}
}
/*A fairly simple method that sets the TabInfo for each tab so that the TabsAdapter
* knows which class the tab that is being added actually belonds to. It also updates
* the UI interface when each tab is added.
*/
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args, String title) {
TabInfo info = new TabInfo(clss, args, title);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
public int getCount() {
return mTabs.size();
}
/*A method that is used in other classes to allow each tab Fragment to
* access its inherited methods from a mother-class, in this case, SherlockFragment
*/
public int getPosition(SherlockFragment fragment) {
for (int j = 1; j < mTabs.size(); j++) {
TabInfo info = (TabInfo) mActionBar.getTabAt(j).getTag();
if (info.title.matches(mTabs.get(j).title)) {
return j;
}
}
return -1;
}
public SherlockFragment getItem(int position) {
TabInfo info = mTabs.get(position);
return (SherlockFragment)Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
/*This method reads the user's selection for a new tab and sets that tab as
* the new current focus.*/
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
selectInSpinnerIfPresent(position, true);
}
private void selectInSpinnerIfPresent(int position, boolean animate) {
try {
View actionBarView = findViewById(R.id.abs__action_bar);
if (actionBarView == null) {
int id = getResources().getIdentifier("action_bar", "id", "android");
actionBarView = findViewById(id);
}
Class<?> actionBarViewClass = actionBarView.getClass();
Field mTabScrollViewField = actionBarViewClass.getDeclaredField("mTabScrollView");
mTabScrollViewField.setAccessible(true);
Object mTabScrollView = mTabScrollViewField.get(actionBarView);
if (mTabScrollView == null) {
return;
}
Field mTabSpinnerField = mTabScrollView.getClass().getDeclaredField("mTabSpinner");
mTabSpinnerField.setAccessible(true);
Object mTabSpinner = mTabSpinnerField.get(mTabScrollView);
if (mTabSpinner == null) {
return;
}
Method setSelectionMethod = mTabSpinner.getClass().getSuperclass().getDeclaredMethod("setSelection", Integer.TYPE, Boolean.TYPE);
setSelectionMethod.invoke(mTabSpinner, position, animate);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
/* This is the method that actually draws the newest tab onto the screen when
* it is selected.*/
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
sp = getSharedPreferences("prefs", MODE_PRIVATE);
SharedPreferences.Editor preferencesEditor = sp.edit();
preferencesEditor.putInt("lastPosition", mViewPager.getCurrentItem());
preferencesEditor.commit();
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
public void onTabReselected(Tab tab, FragmentTransaction ft) {}
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {}
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {}
}
公共类TabsAdapter扩展了FragmentPagerAdapter
实现ActionBar.TablListener、ViewPager.OnPageChangeListener{
私有最终上下文mContext;
私人投票活动;
私人终审法院;
专用最终查看页面mViewPager;
private final ArrayList mtab=new ArrayList();
最后一个类TabInfo{
私人期末班;
私有最终包args;
//私人最终字符串标题;
//此字符串仅作为我尝试的一部分实现!
TabInfo(类、包、参数、字符串、标题){
clss=_类;
args=_args;
title=_title;
}
}
/*构造函数方法,该方法向创建的每个选项卡添加一个TabsAdapter。
*它还将ViewPager添加到每个选项卡,以便用户可以滑动以更改选项卡。
*/
公共选项卡dapter(SherlockFragmentActivity活动、ViewPager寻呼机){
super(activity.getSupportFragmentManager());
mContext=活动;
this.activity=(轮询)活动;
mActionBar=activity.getSupportActionBar();
mViewPager=寻呼机;
mViewPager.setAdapter(此);
mViewPager.setOnPageChangeListener(此);
}
/*这是我一直试图用以解决问题的方法,但它并不能真正解决问题*/
public void buildTabs()引发ClassNotFoundException{
String[]tabs={“经济”、“选举”、“地理”、“政治”、“科学”、“金融”、“宗教”,
“军事”、“国际”};
final String resource=“R.String。”;
MTABAdapter.addTab(bar.newTab().setText(“登录”),
LoginFragment.class,null,“Login”);
对于(int j=0;jString clas = tabs[j] + "Fragment";
String total = "com.davekelley.polling." + clas;
Class<?> theClass = Class.forName (total);