سؤال

From what I understand from this thread, fragments can be easily replaced with another.

However in my case, I have 2 fragments combined in scrollable Activity, so when I say "move", I mean going from the fragment left to the right or right to the left without replacing any fragment with another. Is this somehow possible?

هل كانت مفيدة؟

المحلول

you could use ViewPager for that. And on your adapter class you will have to switch between the fragments via getItem(). Eclipse/new AndroidProject/ swipe with/out tabs. And check the example code generated by Android.


edit:

create xml file call it main_activity.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" >
<android.support.v4.view.PagerTitleStrip
    android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="#FAFAFA"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textColor="#36466E" />

create a class call it Main

       public class Main extends FragmentActivity {
        SectionsPagerAdapter mSectionsPagerAdapter;
        ViewPager mViewPager;
        Context ctx;
        static MySQLiteHelper db;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mSectionsPagerAdapter = new SectionsPagerAdapter(
                    getSupportFragmentManager());
            mSectionsPagerAdapter.notifyDataSetChanged();
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

            @Override
            public Fragment getItem(int position) {
                Fragment fragment = null;
                switch(position){
                case 0:
                    fragment = new GridApp();
                break;
                case 1:
                    fragment = new ListApp();
                break;
                }
                return fragment;
            }

            @Override
            public int getCount() {
                return 2;
            }
            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                case 0:
                    return "Select App";
                case 1:
                    return "Selected Apps";
                }
                return null;
            }
        }
}

now create a class called GridApp and ListApp

GridApp class.

    public class GridApp extends Fragment {

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.gridpp,
                container, false);

}
}

ListApp class

        public class GridApp extends Fragment {

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.listapp,
                container, false);

}
}

and you are done.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top