问题描述
我想实现标签导航,但是我想确保人们有旧版本的Android系统仍然可以使用我的应用程序。
记住ATM的应用程序是相当简单的,我只是希望能够了解如何实现布局,然后我会添加缺少位。
不管怎样,我有一个集装箱的活动,扩展片段活动(以确保兼容性),而这项活动创建使用动作条(我相信我的问题在于这里TabView的)。该应用程序将尝试创建三个选项卡并将其添加到动作条,我想,以确保用户可以来回滚动使用横向导航。
下面是TabListener我想实现:
公共静态类TabListener<吨延伸片断>实现ActionBar.TabListener {
私人片段mFragment;
私人最终活动mActivity;
私人最终字符串MTAG;
私人最终类别< T> mClass;
公共TabListener(活动活动,字符串变量,类< T> CLZ){
mActivity =活动;
MTAG =标签;
mClass = CLZ;
}
公共无效onTabSelected(TAB键,FragmentTransaction英尺){
如果(mFragment == NULL){
mFragment = Fragment.instantiate(mActivity,mClass.getName());
ft.add(android.R.id.content,mFragment,MTAG);
} 其他 {
ft.attach(mFragment);
}
}
公共无效onTabUnselected(TAB键,FragmentTransaction英尺){
如果(mFragment!= NULL){
ft.detach(mFragment);
}
}
公共无效onTabReselected(TAB键,FragmentTransaction英尺){
}
}
下面是我的进口,因为我想确保我用的是支持库:
进口android.app.ActionBar;
进口android.app.ActionBar.Tab;
进口android.app.Activity;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentTransaction;
进口android.support.v4.view.ViewPager;
进口android.view.Menu;
然而,Eclipse的是给我用的 TabListener 方法的问题。它告诉我如下:类型LayoutContainer.TabListener必须实现继承的抽象方法ActionBar.TabListener.onTabSelected(ActionBar.Tab,FragmentTransaction)
当我选择添加未实现的方法的Eclipse基本上添加的 OnTabSelected OnTabReselected 和 OnTabUnselected 的方法,但是这一次,路过不支持版本该片段( android..app.Fragment )作为参数。
在如何通过支持库再拍实施横向导航,以确保兼容性任何想法?
动作条= getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(假);
actionBar.setDisplayShowTitleEnabled(假);
塔巴= actionBar.newTab()的setText()。
塔布= actionBar.newTab()的setText()。
碎片碎裂=新AFragmentTab();
片段fragmentB =新BFragmentTab();
tabA.setTabListener(新MyTabsListener(碎裂));
tabB.setTabListener(新MyTabsListener(fragmentB));
actionBar.addTab(塔巴);
actionBar.addTab(塔布);
该选项卡监听器如下:
保护类MyTabsListener实现ActionBar.TabListener {
私人片段片段;
公共MyTabsListener(片段片段){
this.fragment =片段;
}
公共无效onTabSelected(TAB键,FragmentTransaction英尺){
ft.add(R.id.layout2,片段,NULL);
}
公共无效onTabReselected(TAB键,FragmentTransaction英尺){
}
公共无效onTabUnselected(TAB键,FragmentTransaction英尺){
ft.remove(片段);
}
}
,然后你让一个类为每个标签:
公共类BFragmentTab扩展片段{
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
{
返回inflater.inflate(R.layout.login,集装箱,假);
}
}
但请不支持的操作栏下方的Android 3.0版本的头脑。如果你想使用它在旧版本我建议你使用actionBarSherlock库。
I am trying to implement Tab Navigation, but I want to make sure people that have older versions of Android can still use my application.
The app in mind ATM is fairly simple, I just want to be able to understand how to implement the layout and then I'll add the missing bits.
Anyhow, I have a Container Activity that extends Fragment Activity (to ensure compatibility), and this Activity creates a TabView using an ActionBar (I believe my problem resides here). The app will try to create three tabs and add them to the ActionBar, and I want to make sure the user can scroll back and forth using lateral navigation.
Here is TabListener I am trying to implement:
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
Here are my imports, because I wanted to make sure I was using the support library:
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.Menu;
However, Eclipse is giving me issues with the TabListener methods. It is telling me the following: "The type LayoutContainer.TabListener must implement the inherited abstract method ActionBar.TabListener.onTabSelected(ActionBar.Tab, FragmentTransaction)"
When I select Add unimplemented methods Eclipse basically adds the OnTabSelected OnTabReselected and OnTabUnselected methods, but this time, passing the non-support version of the Fragment (android..app.Fragment) as a parameter.
Any ideas on how to make another implementation of lateral navigation through the support library to ensure compatibility?
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
tabA = actionBar.newTab().setText("");
tabB = actionBar.newTab().setText("");
Fragment fragmentA = new AFragmentTab();
Fragment fragmentB = new BFragmentTab();
tabA.setTabListener(new MyTabsListener(fragmentA));
tabB.setTabListener(new MyTabsListener(fragmentB));
actionBar.addTab(tabA);
actionBar.addTab(tabB);
The tab listener is as follows:
protected class MyTabsListener implements ActionBar.TabListener{
private Fragment fragment;
public MyTabsListener(Fragment fragment){
this.fragment = fragment;
}
public void onTabSelected(Tab tab, FragmentTransaction ft){
ft.add(R.id.layout2, fragment, null);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
and then you make a class for each tab:
public class BFragmentTab extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.login, container, false);
}
}
But keep in mind that the action bar isn't supported for android versions below 3.0 . If you want to use it in older versions I suggest you use actionBarSherlock Library.
这篇关于实现使用支持库一TabListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!