【问题标题】:Fragment hide not working in Android片段隐藏在Android中不起作用
【发布时间】:2015-03-10 09:52:22
【问题描述】:

我的要求是,我有一个主要活动,我想在单独的片段中显示图像和文本而不是进度条。 我有一个单独的片段,我想在用户单击 MainActivity 中的按钮时显示该片段。

我的问题是,当我启动应用程序时,默认情况下会显示片段。我希望仅在用户单击按钮时才显示片段。当我启动应用程序时,我希望显示活动。

我有一个如下的activity_main xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background" >

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >

            <TableLayout
                android:id="@+id/tableLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="*" >

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dip" >

                    <TextView
                        android:id="@+id/text1"
                        style="@style/textForLabel"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dip"
                        android:text="@string/text1" />

                    <Spinner
                        android:id="@+id/spinner1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="20dip"
                        android:layout_weight="1"
                        android:drawSelectorOnTop="true"
                        android:prompt="@string/spinner1"
                        android:spinnerMode="dialog" >
                    </Spinner>
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/text2"
                        style="@style/textForLabel"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dip"
                        android:text="@string/label2" />

                    <Spinner
                        android:id="@+id/spinner2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="20dip"
                        android:layout_span="6"
                        android:layout_weight="1"
                        android:drawSelectorOnTop="true"
                        android:prompt="@string/spinner2"
                        android:spinnerMode="dropdown" />
                </TableRow>
            </TableLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/text3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textColor="@color/red"
                    android:textSize="@dimen/text_medium"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <fragment
        android:id="@+id/progress_bar_fragment"
        android:name="com.test.pushnotificationeclipse.ProgressBarFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

fragemnt_progress_bar.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/NoActionBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_all" >

    <ImageView
        android:id="@+id/imageView_frag_pgbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text_frag_pgbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView_frag_pgbar"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="74dp"
        android:textColor="@color/basic_color"
        android:textSize="@dimen/text_medium_large" />

</RelativeLayout>

我的 ProgressBarFragment.java 如下

public class ProgressBarFragment extends Fragment {

    @InjectView(R.id.text_frag_pgbar)
    TextView textView;

    @InjectView(R.id.imageView_frag_pgbar)
    ImageView imageView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_progress_bar, container,
                false);
        ButterKnife.inject(this, view);

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}

我的 MainActivity.java 如下:当我传递 true tohideAndShowFragment() 时,片段应该被隐藏,当我传递 false 时片段应该被显示。

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        context = getApplicationContext();
        hideAndShowFragment(true);
    }

    public void hideAndShowFragment(boolean hide) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ProgressBarFragment pf = new ProgressBarFragment();
        if (hide) {
            ft.hide(pf).commit();
        } else {
            ft.show(pf).commit();
        }
    }
}

【问题讨论】:

    标签: android android-activity android-fragments fragment android-fragmentactivity


    【解决方案1】:

    这里的问题是您的hideAndShowFragment 方法。您正在创建 ProgressBarFragment 的新实例,但 FragmentTransaction 的隐藏和显示方法仅适用于附加的片段。

    相反,您应该通过 id 获取已在 XML 中定义的片段。

    public void hideAndShowFragment(boolean hide) {
        FragmentManager fm = getFragmentManager();
        ProgressBarFragment pf = 
            (ProgressBarFragment) fm.findFragmentById(R.id.progress_bar_fragment);
        FragmentTransaction ft = fm.beginTransaction();
        if (hide) {
            ft.hide(pf).commit();
        } else {
            ft.show(pf).commit();
        }
    }
    

    另外,这一行是关于:

    context = getApplicationContext();
    

    您的Activity 也是一个Context 对象,因此除非您需要特定于应用程序的上下文,否则您通常希望使用this,因为您需要在活动中使用上下文。

    【讨论】:

    • 这可以很好地隐藏片段。但是 show(pf).commit() 不能正常工作
    【解决方案2】:
     FragmentManager fm = getFragmentManager();
        fm.beginTransaction()
                  .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
                  .show(yourfragment)
                  .commit();
    
    Ps:-Dont Don't mess with the visibility flags of the container - FragmentTransaction.hide/show does that internally for you.
    

    源:-“https://stackoverflow.com/a/16490344/1632286

    【讨论】:

      【解决方案3】:

      您最好在代码中获取片段的实例并使用官方文档中的技术,如下所示和here(搜索“newInstance”)。将以下静态方法添加到您的片段中。

      public static ProgressFragment newInstance() {
          DetailsFragment f = new DetailsFragment();
      
          // Supply args here if needed.
          /*Bundle args = new Bundle();
          args.putInt("index", index);
          f.setArguments(args);*/
      
          return f;
      }
      

      完成后,您可以使用按钮单击方法中的以下片段事务代码将片段添加到顶视图布局。

      FragmentManager fm = getSupportFragmentManager();
      FragmentTransaction tr = fm.beginTransaction();
      tr.add(android.R.id.content, ProgressFragment.newInstance(), "ProgressFragmentTag");
      tr.commit();
      

      然后,如果需要,您可以使用以下代码删除片段;

      FragmentManager fm = getSupportFragmentManager();
      ProgressFragment frag = fm.findFragmentByTag("ProgressFragmentTag")
      if (frag!=null)
      {
         FragmentTransaction tr = fm.beginTransaction();
         tr.remove(frag).commit();
      }
      

      希望这会有所帮助!

      【讨论】:

        【解决方案4】:

        我通过添加片段而不是显示它来解决它:

        public void hideAndShowFragment(boolean hide) {
                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ProgressBarFragment pf = (ProgressBarFragment) fm.findFragmentById(R.id.progress_bar_fragment);
                if (hide) {
                    ft.hide(pf).commit();
                } else {
        ft.add(android.R.id.content, pf, "ProgressFragmentTag").commit();
                }
            }
        

        【讨论】:

          【解决方案5】:
                    int count =1;
                     if (count == 1) {
          
          
                         FragmentManager fm = getFragmentManager();
                         FragmentTransaction fragmentTransaction = 
                         fm.beginTransaction();
                         fragmentTransaction.replace(R.id.labelframe, new 
                          labelfregaments());
                         fragmentTransaction.commit(); 
                         count =0;
                     }
                     else if (count == 0) {
          
          
          
                         FragmentManager fm = getFragmentManager();
                         labelfregaments fs = (labelfregaments)fm.findFragmentById(R.id.labelframe);
                         FragmentTransaction fragmentTransaction = fm.beginTransaction();
                         fragmentTransaction.hide(fs);
                         fragmentTransaction.commit();
          
          
                         count=1;
          
                     }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-04-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-12-30
            • 1970-01-01
            • 2019-06-27
            相关资源
            最近更新 更多