【问题标题】:how to load default fragment in frame layout in MainActivity?如何在 MainActivity 的框架布局中加载默认片段?
【发布时间】:2018-03-16 17:21:33
【问题描述】:

我在 mainActivity 中有 2 个片段frameLayout。 每当我运行我的应用程序时,我希望将我的 Fragment1 加载到我的 frameLayout default 中,它位于 mainActivity 中。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.prank.fragmentbackstack.MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frame"
    >

</FrameLayout>
</RelativeLayout>

Frag.class

import android.widget.Button;
    import android.widget.TextView;
    public class Frag extends Fragment {
        String Tag1="frag1";

        Button b;

        public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle saveInstanceState)
        {View view = inflater.inflate(R.layout.fragment, viewGroup, false);
            b=(Button)view.findViewById(R.id.button);

            return view;
        }
    }

【问题讨论】:

  • 在您的活动的onCreate 方法中执行...
  • 怎么样?我真的不知道?
  • 到目前为止你做了什么??显示一些代码
  • 我想将 Frag.java 添加到 Frame Layout default

标签: android android-fragments


【解决方案1】:

使用此代码动态添加您的片段

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

【讨论】:

【解决方案2】:

试试这个:

public class MainActivity extends AppCompatActivity {

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

        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager()
                    .beginTransaction()
                    .replace(R.id.frame, new Frag())
                    .commit();
        }
    }
}

【讨论】:

    【解决方案3】:

    使用此代码只需将 frameLayout 容器替换为 onCreate 内的 Fragment1 :

    getFragmentManager().beginTransaction()
                    .replace(R.id. frameLayout, new Fragment1())
                    .commit();
    

    【讨论】:

      【解决方案4】:

      在你的主要活动中使用它:

      frag_name frag_name = new frag_name();
                          FragmentManager manager = this.getSupportFragmentManager();
                          manager.beginTransaction().replace(R.id.youtframelayout, frag_name, frag_name.getTag()).commit();
      

      它初始化 fragment 并将其替换为您的 FrameLayout

      希望它有效! :)

      【讨论】:

        【解决方案5】:

        使用 sigleton 设计模式解决。

        在你的fragment

        public static HomeFragment newInstance() {
        
            Bundle args = new Bundle();
        
            HomeFragment fragment = new HomeFragment();
            fragment.setArguments(args);
            return fragment;
        }
        

        activity 中的onCreate 方法中添加fragment

        HomeFragment homeFragment = HomeFragment.newInstance();
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.yourFrameLayoutId, homeFragment)
                    .commit();
        

        【讨论】:

          【解决方案6】:

          制作 pushfragment 类并在您想添加或更新时使用。

          pushFragments(R.id.frame, this, YourFragment, false, false, YourFragment.class.getSimpleName());
          
          public static void pushFragments(int container, AppCompatActivity activity, Fragment fragment, boolean animate, boolean shouldAdd, String tag) {
                  FragmentManager manager = activity.getSupportFragmentManager();
                  FragmentTransaction ft = manager.beginTransaction();
                  if (animate) { //For animation
                      ft.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
                  }
          
                  if (shouldAdd) { //for add in stack
                      ft.replace(container, fragment, tag);
                      ft.addToBackStack(tag);
                      ft.commitAllowingStateLoss();
                  } else {
                      manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                      ft.replace(container, fragment, tag);
                      ft.commit();
                  }
              }
          

          将 Frag 传递给 pushFragments 并在 Mainactivity 的 onCreate 中添加调用。

          【讨论】:

            【解决方案7】:

            设置这个:

             viewPager.setCurrentItem(index);
            

            index = index of fragment 你要显示

            【讨论】:

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