在FragmentTabHost这个控件中如何获得fragment的对象呢,其实在之前我也不知道怎么获取的,在网上粗略查了一下,没人有详细的说明,要么就是跟我想要的不一样,不过我有一股倔脾气,终于被我知道了
下面就是我的主页面类,如何获得那些fragment对象呢,因为传入的是片段的类类型
package com.gm.money;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import com.gm.money.fragment.TabItemFragmentFirst;
import com.gm.money.fragment.TabItemFragmentFour;
import com.gm.money.fragment.TabItemFragmentThree;
import com.gm.money.fragment.TabItemFragmentTwo;
import com.gm.money.view.BadgeView;
import cxh.voctex.utils.LogUtil;
/**
* @author voctex 2015-09-20
*
*/
@SuppressLint("InflateParams")
public class MainActivity extends FragmentActivity {
private FragmentTabHost tabHost;
private String[] tabTexts = new String[] { "第一", "第二", "第三", "第四" };
private int[] tabImgs = new int[] { R.drawable.selector_tab_item_icon0,
R.drawable.selector_tab_item_icon1,
R.drawable.selector_tab_item_icon2,
R.drawable.selector_tab_item_icon3 };
private Class<?>[] fragments = new Class[] { TabItemFragmentFirst.class,
TabItemFragmentTwo.class, TabItemFragmentThree.class,
TabItemFragmentFour.class };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (FragmentTabHost) findViewById(R.id.tab_fa_fth);
tabHost.setup(this, getSupportFragmentManager(),
R.id.tab_fa_maincontent);
for (int i = 0; i < tabTexts.length; i++) {
TabSpec spec = tabHost.newTabSpec(tabTexts[i]).setIndicator(
getView(i));
tabHost.addTab(spec, fragments[i], null);
}
// 设置tabs之间的分隔线不显示
tabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
tabHost.setCurrentTab(0);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
LogUtil.showI(LogUtil.Voc, "tabId=" + tabId);
}
});
}
public BadgeView getBadgeView(int index) {
BadgeView badgeView = (BadgeView) tabHost.getTabWidget()
.getChildAt(index).getTag();
return badgeView;
}
private View getView(int index) {
final View view = LayoutInflater.from(this).inflate(
R.layout.tab_fa_item, null, true);
LinearLayout linearLayout = (LinearLayout) view
.findViewById(R.id.tab_fa_item_layout);
TextView textView = (TextView) view.findViewById(R.id.tab_fa_item_text);
ImageView imageView = (ImageView) view
.findViewById(R.id.tab_fa_item_img);
textView.setText(tabTexts[index]);
imageView.setImageResource(tabImgs[index]);
BadgeView badgeView = new BadgeView(MainActivity.this, linearLayout);
badgeView.setText("7");
badgeView.setBadgeMargin(7);
badgeView.setTextSize(11.8f);
// badgeView.show();
view.setTag(badgeView);
if (index == 0) {
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LogUtil.showI(LogUtil.Voc,
"I click this linearlayout in first item");
((BadgeView) view.getTag()).toggle();
}
});
}
return view;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
本来我在想,因为fragment的类类型传入了tabhost的addtag方法里面去了,所以一直在想,可能在tabhost这个控件可以获得到那些fragment的对象,结果怎么找都找不到,不过,倒是从addtag这个方法里面看出一些东西
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
tabSpec.setContent(new DummyTabFactory(mContext));
String tag = tabSpec.getTag();
TabInfo info = new TabInfo(tag, clss, args);
if (mAttached) {
// If we are already attached to the window, then check to make
// sure this tab's fragment is inactive if it exists. This shouldn't
// normally happen.
info.fragment = mFragmentManager.findFragmentByTag(tag);
if (info.fragment != null && !info.fragment.isDetached()) {
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.detach(info.fragment);
ft.commit();
}
}
mTabs.add(info);
addTab(tabSpec);
}
看出了什么了没有,看一下,这里有出现fragment的对象,是通过mFragmentManager.findFragmentByTag(tag)获取的,其中mFragmentManager就是传入的getSupportFragmentManager(),而那个tag,我通过一系列打印和调试,确定了就是传入的那些tag的参数,也就是{ “第一”, “第二”, “第三”, “第四” },
所以第一个片段的对象我们可以这样获得
TabItemFragmentFirst firstFragment = (TabItemFragmentFirst) getSupportFragmentManager().findFragmentByTag(tabTexts[0]);
这里的firstFragment 就是第一个片段的对象,以此类推,其他片段的对象都可以以此获得
第一次写博客,本来想写其他的,有好多东西想写,都没时间写,这次本着刚弄好这个问题
可以参考我github上的项目,里面的主界面就是这么实现的,地址为https://github.com/voctex/Kepler
QQ:361561789
可事可以直接加Q联系