动作条和片段的ICS 4.0.3 [英] ActionBar and Fragments on ICS 4.0.3

查看:137
本文介绍了动作条和片段的ICS 4.0.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个Android应用程序,我有一些问题的动作条。

我需要类似Instagram的或Twitter的东西为Android:我有一个操作栏3个选项卡和这些选项卡有一些按钮;每个按钮具有一个onclick()来替换一个新使用FragmentTransaction(和方法替换())

(例如''FragmentN'')的实际片段

我的第一个问题是,当我选择的动作条选项卡,选中的选项卡不取代FragmentN,但它本身放置在上面,所以我继续看到这两个片段,一个在另一个之上。

我也有另外一个问题,但我认为这是链接到previous ......不过,我解释一下,你用一个例子。 我从动作条中选择选项卡A,从这个标签我preSS按钮3所以FragmentA3取代碎裂。在此之后,我从动作条中选择TABC。 现在,我的动作条,标签A全选,总是一次但不是又看到碎裂我看FragmentA3我选择previously。

我该如何解决这些问题呢? 非常感谢你。

编辑:这是我的code

MainActivity

 进口android.os.Bundle;
    进口android.annotation.Sup pressLint;
    进口android.annotation.TargetApi;
    进口android.app.ActionBar;
    进口android.app.ActionBar.Tab;
    进口android.app.Activity;
    进口android.app.Fragment;
    进口android.app.FragmentTransaction;


    @燮pressLint(NewApi)
    @TargetApi(11)
    公共类ABActivity延伸活动{
@覆盖
     公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    动作条动作条= getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // TAB 1
    TAB键= actionBar.newTab();
    tab.setIcon(R.drawable.p);
    字符串LABEL1 =一;
    TabListener< Tab1Fragment> TL1 =新TabListener< Tab1Fragment>(这一点,
            LABEL1,Tab1Fragment.class);
    tab.setTabListener(TL1);
    actionBar.addTab(标签);

    // TAB2
    标签= actionBar.newTab();
    tab.setIcon(R.drawable.m);
    字符串LABEL2 =两化;
    TabListener< Tab2Fragment> TL2 =新TabListener< Tab2Fragment>(这一点,
            LABEL2,Tab2Fragment.class);
    tab.setTabListener(TL2);
    actionBar.addTab(标签);

    // TAB3
    标签= actionBar.newTab();
    tab.setIcon(R.drawable.s);
    字符串LABEL3 =三;
    TabListener< Tab3Fragment> TL3 =新TabListener< Tab3Fragment>(这一点,
            LABEL3,Tab3Fragment.class);
    tab.setTabListener(TL3);
    actionBar.addTab(标签);
     }

   私有类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英尺){
  }}}
 

Tab1Fragment

 进口android.annotation.Sup pressLint;
   进口android.app.Fragment;
   进口android.app.FragmentTransaction;
   进口android.os.Bundle;
   进口android.view.LayoutInflater;
   进口android.view.View;
   进口android.view.ViewGroup;
   进口android.widget.Button;
   进口android.widget.LinearLayout;

   公共类Tab1Fragment扩展片段{
 片f;
     @覆盖
     公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){

    返回(的LinearLayout)inflater.inflate(R.layout.tab1,集装箱,假);
  }

      @覆盖
       公共无效onActivityCreated(包savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
F =这一点;

。按钮屁股=(按钮)getActivity()findViewById(R.id.butt);
butt.setOnClickListener(新View.OnClickListener(){

@燮pressLint(NewApi)
公共无效的onClick(视图v){
 片段诺沃=新Nuova的();
FragmentTransaction交易= getFragmentManager()的BeginTransaction()。

transaction.replace(android.R.id.content,诺沃);
transaction.addToBackStack(诺沃);

 器transaction.commit();
   }});
}
 }
 

Nuova的

 进口android.app.Fragment;
   进口android.os.Bundle;
   进口android.view.LayoutInflater;
   进口android.view.View;
   进口android.view.ViewGroup;
   进口android.widget.LinearLayout;

   公共类Nuova的扩展片段{

 @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){

        返回(的LinearLayout)inflater.inflate(R.layout.nuova,集装箱,假);
    }}
 

tab1.xml

 < XML版本=1.0编码=UTF-8&GT?;
 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / TAB1
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:重力=中心>


<按钮
    机器人:ID =@ + ID /对接
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=按钮/>

<的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:文本=一/>

    < / LinearLayout中>
 

nuova.xml

 < XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      机器人:ID =@ + ID /诺沃
      机器人:layout_width =match_parent
      机器人:layout_height =match_parent
      机器人:方向=垂直
      机器人:重力=中心>

<的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:文本=NUOVA/>
 < / LinearLayout中>
 

解决方案

在在的onClick方法你Tab1Fragment你新创建的Nuovo更换mFragment实例

  

替换被添加到容器的现有片段。这在本质上是一样的调用remove(片段)的添加具有相同containerViewId所有当前已添加的片段,然后添加(INT,片段,字符串)这里给出相同的参数。

(它看起来像mFragment现在reffer到另一个对象(诺沃),但我可能是错的 - 我会检查一下明天)

所以,在第一次替换该行 transaction.replace(android.R.id.content,诺沃);

  transaction.replace(android.R.id.content,诺沃,nuovoTag);
 

然后修改公共无效onTabSelected(TAB键,FragmentTransaction英尺)具有以下code:

 片段FR = getFragmentManager()findFragmentByTag(nuovoTag)。
如果(FR!= NULL)
  ft.remove(FR)

  如果(mFragment == NULL){
      mFragment = Fragment.instantiate(mActivity,mClass.getName());
      ft.add(android.R.id.content,mFragment,MTAG);
  } 其他 {
      ft.attach(mFragment);
  }
 

I'm trying to develop an Android app and I have some problems with the ActionBar.

I need something similar to Instagram or Twitter for Android: I have an Action Bar with 3 tabs and these tabs have some buttons; every button has an OnClick() that replaces the actual fragment with a new one (for example ''FragmentN'') using FragmentTransaction (and the method replace()) .

My first problem is that when I select a tab from the ActionBar, the selected tab doesn't replace "FragmentN" but it places itself on top so I continue seeing both fragment, one above the other.

I have also another problem but I think it's linked to the previous...however I explain you with an example. I select tab A from the ActionBar and from this tab I press button 3 so "FragmentA3" replaces "FragmentA". After this, I select tabC from the ActionBar. Now I select, always from the ActionBar, the tab A again but instead of seeing again "FragmentA" I see "FragmentA3" that I choose previously.

How can I solve these problems? Thank you very much.

EDIT: This is my code.

MainActivity

    import android.os.Bundle;
    import android.annotation.SuppressLint;
    import android.annotation.TargetApi;
    import android.app.ActionBar;
    import android.app.ActionBar.Tab;
    import android.app.Activity;
    import android.app.Fragment;
    import android.app.FragmentTransaction;


    @SuppressLint("NewApi")
    @TargetApi(11)
    public class ABActivity extends Activity {
@Override
     public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    //TAB 1
    Tab tab = actionBar.newTab(); 
    tab.setIcon(R.drawable.p);
    String label1 = "one";
    TabListener<Tab1Fragment> tl1 = new TabListener<Tab1Fragment>(this,
            label1, Tab1Fragment.class);
    tab.setTabListener(tl1);
    actionBar.addTab(tab);

    //TAB2
    tab = actionBar.newTab(); 
    tab.setIcon(R.drawable.m);
    String label2 = "two";
    TabListener<Tab2Fragment> tl2 = new TabListener<Tab2Fragment>(this,
            label2, Tab2Fragment.class);
    tab.setTabListener(tl2);
    actionBar.addTab(tab);

    //TAB3       
    tab = actionBar.newTab(); 
    tab.setIcon(R.drawable.s);
    String label3 = "three";
    TabListener<Tab3Fragment> tl3 = new TabListener<Tab3Fragment>(this,
            label3, Tab3Fragment.class);
    tab.setTabListener(tl3);
    actionBar.addTab(tab);
     }

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

Tab1Fragment

   import android.annotation.SuppressLint;
   import android.app.Fragment;
   import android.app.FragmentTransaction;
   import android.os.Bundle;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.Button;
   import android.widget.LinearLayout;

   public class Tab1Fragment extends Fragment {
 Fragment f;
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return (LinearLayout) inflater.inflate(R.layout.tab1, container, false);
  }

      @Override
       public void onActivityCreated (Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
f=this;

Button butt = (Button) getActivity().findViewById(R.id.butt);
butt.setOnClickListener(new View.OnClickListener() {

@SuppressLint("NewApi")
public void onClick(View v) {
 Fragment nuovo = new Nuova();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

transaction.replace(android.R.id.content, nuovo);
transaction.addToBackStack("nuovo");

 transaction.commit();
   } });
}
 }

Nuova

   import android.app.Fragment;
   import android.os.Bundle;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.LinearLayout;

   public class Nuova extends Fragment {

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        return (LinearLayout) inflater.inflate(R.layout.nuova, container, false);
    } }

tab1.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">


<Button
    android:id="@+id/butt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="one" />

    </LinearLayout>

nuova.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/nuovo"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:gravity="center">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="nuova" />
 </LinearLayout>

解决方案

In your Tab1Fragment in onClick method you replace mFragment instance with newly created nuovo

Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

(It looks like mFragment now reffer to another object(nuovo), but i may be wrong -- I'll check it tommorow)

So, at first replace this line transaction.replace(android.R.id.content, nuovo);

with

transaction.replace(android.R.id.content, nuovo, "nuovoTag");

Then modify your public void onTabSelected(Tab tab, FragmentTransaction ft) with the following code:

Fragment fr = getFragmentManager().findFragmentByTag("nuovoTag");
if (fr != null)
  ft.remove(fr);

  if (mFragment == null) {
      mFragment = Fragment.instantiate(mActivity, mClass.getName());
      ft.add(android.R.id.content, mFragment, mTag);
  } else {
      ft.attach(mFragment);
  }

这篇关于动作条和片段的ICS 4.0.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆