在TabActivity

时间:2017-01-06 17:11:17

标签: android android-fragments fragment android-tabactivity

对于重复的问题我很抱歉,但我没有得到我的问题的答案。 我使用TabActivity创建应用程序,并尝试从片段本身替换一个片段,我在https://developer.android.com/training/basics/fragments/fragment-ui.html中读取了如何做到这一点,我在我的片段中创建了我想要用另一个替换的接口, 我在我的MainActivity中实现了界面,并且在运行我的应用程序时它仍然显示容器本身。

这是我的代码:

主要活动:

public class MainActivity extends FragmentActivity implements New2.OnReplaceFragment {
   private SectionsPagerAdapter mSectionsPagerAdapter;
   private ViewPager mViewPager;
   public static MainActivity instance = null;

   public static MainActivity getInstance(){
       return instance;
   }

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
       mViewPager = (ViewPager) findViewById(R.id.frame_container);
       mViewPager.setAdapter(mSectionsPagerAdapter);
       TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
       tabLayout.setupWithViewPager(mViewPager);
        tabLayout.getTabAt(0).setIcon(R.drawable.icon_info);
        tabLayout.getTabAt(1).setIcon(R.drawable.icon_heart_rate_sensor_jpg);
        tabLayout.getTabAt(2).setIcon(R.drawable.icon_graph_jpg);
        instance = this;
    }
    @Override
    public void onReplaceFragment(Class fragmentClass) {
        Fragment fragment = null;
        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // Insert the fragment by replacing any existing fragment
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_container,fragment);

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back


        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
    }

    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            switch (position) {
                case 0:
                  //  New1 tab1 = new New1();
                    return New1.newInstance();
                case 1:
                    return New2.newInstance();
                case 2:
                    return New3.newInstance();
                default:
                    return null;
            }
        }
        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "SECTION 1";
                case 1:
                    return "SECTION 2";
                case 2:
                    return "SECTION 3";
            }
            return null;
        }
    }
}

我要替换的片段

片段:

public class New2 extends Fragment {
    TextView name;
    Button change;
    ImageView image1;
    Animation anime;
    private OnReplaceFragment dataPasser;

    public static New2 newInstance(){
        New2 fragment = new New2();
        return fragment;
    }
    public New2(){

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
         View rootView = inflater.inflate(R.layout.fragment_new2, container, false);
        name = (TextView) rootView.findViewById(R.id.nameTt);
        change = (Button) rootView.findViewById(R.id.changeBtn);
        image1 = (ImageView) rootView.findViewById(R.id.image1);
        anime = AnimationUtils.loadAnimation(getActivity().getApplicationContext(),R.anim.zoom);
        change.setOnClickListener(changeName);
        return rootView;
    }

    View.OnClickListener changeName = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            image1.startAnimation(anime);
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run(){
                    dataPasser.onReplaceFragment(Result.class);
                }
            },1000);
        }
    };
    public interface OnReplaceFragment {
        public void onReplaceFragment(Class fragmentClass);
    }

    @Override
    public void onAttach(Activity a) {
        super.onAttach(a);
        try {
            dataPasser = (OnReplaceFragment) a;
        } catch (ClassCastException e) {
            throw new ClassCastException(a.toString() + " must implement onDataPass");
        }
    }
}

我要展示的片段

public class Result extends Fragment {

TextView textView;
Button btnBack;
public static Result instance = null;

public static Result getInstance(){
    return instance;
}

public Result() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_result, container, false);
    textView = (TextView) v.findViewById(R.id.text11);
    btnBack = (Button) v.findViewById(R.id.btnBack);
    textView.setText("working!!");
    Toast.makeText(getActivity().getApplicationContext(),"working",Toast.LENGTH_LONG).show();
    return v;
}

}

主要活动XML:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.hercules.tadhosttutrial.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:background="@color/red"
    android:theme="@style/AppTheme.AppBarOverlay">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

New2 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"
tools:context="com.example.hercules.tadhosttutrial.New2"
android:background="@color/yellow">

<!-- TODO: Update blank fragment layout -->

<Button
    android:id="@+id/changeBtn"
    android:layout_width="80dp"
    android:layout_height="40dp"
    android:layout_gravity="center_horizontal"
    android:text="change"/>

<ImageView
    android:id="@+id/image1"
    android:background="@drawable/icon_complete"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    />

结果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="@color/colorAccent"
    tools:context="com.example.hercules.tadhosttutrial.Result">

  <!-- TODO: Update blank fragment layout -->

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

  <TextView
      android:id="@+id/text11"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="WORKING"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:textSize="70dp"
      />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我的意思是,将MainActivity作为所有片段的主要容器,使用制表符或只是常规片段,

1-主要活动XML:删除ViewPager,添加FrameLayout代替(使用相同的ID)

2-使用此XML创建新片段TabsFragment

<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"
tools:context="com.example.hercules.tadhosttutrial.New2"
android:background="@color/yellow">

<android.support.v4.view.ViewPager
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>

3-将主要活动的SectionsPagerAdapterViewPager初始化为TabsFragment

这一部分:

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

和此:

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.frame_container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setIcon(R.drawable.icon_info);
tabLayout.getTabAt(1).setIcon(R.drawable.icon_heart_rate_sensor_jpg);
tabLayout.getTabAt(2).setIcon(R.drawable.icon_graph_jpg);

4-我认为在新文件中移动SectionsPagerAdapter类也更好。

现在,如果您希望将应用的默认视图作为标签,则可以MainActivityonCreate()显示TabsFragment,通过调用您的方法:

onReplaceFragment(TabsFragment.class);

现在每件事都应该可以正常工作,因为这里的想法是将主要活动中显示的片段替换为另一个片段 在这种情况下TabsFragmentResultNew2

不要替换viewpager片段(因为我告诉你这是通过适配器管理的)而不是通过调用replace()

你可能需要玩这个,它不是最终的代码,只是给你一些想法。