日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 多个类的对象,android – 为多个对象使用泛型类(actionBar选项卡)

發(fā)布時間:2025/3/19 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 多个类的对象,android – 为多个对象使用泛型类(actionBar选项卡) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目前我使用ABS,ActionBar Tabs和TabsAdapter / ViewPager為我的應(yīng)用程序制作一個漂亮的標(biāo)簽布局.我有5個類別標(biāo)簽 – 最終用戶將能夠添加新類別(我稍后會設(shè)置它).所以,目前,我有一個主要的SherlockFragmentActivity和許多SherlockFragment類別文件.在主要SFA的onCreate中,我構(gòu)建了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);

我想做的是創(chuàng)建一個新的解決方案,CategoryFragment而不是使用所有特定的選舉,Geo,Econ等.任何人都可以想象一個解決方案嗎?理想情況下,我想將一個字符串傳遞給添加的選項卡,以便CategoryFragment可以根據(jù)字符串進(jìn)行膨脹.我想要這個解決方案,因?yàn)榇a在多個類中是非常冗余的,當(dāng)所有類真正做的是從SQL db在線加載東西時,只獲取自己類別的數(shù)據(jù).

這是我的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 mTabs = new ArrayList();

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) {}

}

總結(jié)

以上是生活随笔為你收集整理的android 多个类的对象,android – 为多个对象使用泛型类(actionBar选项卡)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。