I want PagerSlidingTabStrip to be Fragment.

Asked 2 years ago, Updated 2 years ago, 44 views

The code below is extensions ActionBarActivity, but is it possible to use extensions Fragment or Activity?The most important thing I want to do is to rewrite it to Fragment, but if it is not possible, I would like to know how to do it if it is Activity.

If possible, I would appreciate it if you could let me know how to do it.Thank you for your cooperation.

public class PagerSlidingTabStrip extensions ActionBarActivity{
    @ Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ViewPager=(ViewPager) findViewById (R.id.pager);
        pager.setAdapter(newTestAdapter(getSupportFragmentManager()));


        PagerSlidingTabStrip=(PagerSlidingTabStrip) findViewById(R.id.tabs);
        tabs.setViewPager(pager);

    }

    @ Override
    public boolean onCreateOptionsMenu(Menu){
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @ Override
    public boolean onOptionsItemSelected(MenuItem){
        switch(item.getItemId()){

            case android.R.id.home:
                finish();
                break;
            case R.id.menu_home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    private class TestAdapter extensions FragmentPagerAdapter {

        private final String[]TITLES={"A", "B", "C"};

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

        @ Override
        public android.support.v4.app.Fragment getItem(int position){
            switch(position){
                case0:
                    return new AFragment();
                case1:
                    return new BFragment();
            }

            return null;
        }


        @ Override
        public intgetCount(){
            return TITLES.length;
        }

        @ Override
        publicCharSequence getPageTitle(int position){
            return TITLES [position];
        }
    }
}

Additional
Usually, the screen made with Fragmnet looks like the following without any special processing.
Enter a description of the image here

However, if you just transition the code like the one above, it will be displayed, but
Naturally, ActionbarActivity will not be displayed, and you will not be able to open Actionbar??NavigationDrawer in the image above.

Enter a description of the image here

I think it's about the processing part of Drawer you told me earlier, but I don't know what to do with other Fragments because I didn't do anything in particular...
If you don't mind, could you please let me know?

android

2022-09-30 20:36

2 Answers

It can be either Fragment or Activity.
If you use ActionBar, you should leave ActionBarActivity and make the PagerSlidingTabStrip part Fragment.
If you want Fragment to include ActionBar, you can't put ActionBar in Fragment, so you should use ToolBar.
However, when using ToolBar, I think it is common to place ToolBar in Activity and place Fragment under it.
If you tell me why you want to use Fragment, I think you will get a more specific answer.

Additional
Answers to comments.

If that's the case, I think it's okay to keep ActionBarActivity (although I think you should change the class name).

What about this configuration?
* MainActivity
Inherits ActionBarActivity and handles Drawer.
Switch between Fragment by selecting the Drawer menu.
* TabFragment
Inherited Fragment and placed PagerSlidingTabStrip.

Regarding "Examples of PagerSlidingTabStrip in Fragment" in particular, Fragment, so there is no need for anything special, and I think you can use the method used in Activity as it is.


2022-09-30 20:36

I would like to add that it is long, so I will create a new answer

Is this the configuration you want to create?

Enter a description of the image here

For the configuration shown in the image above, the code is as follows, but what do you not understand (or is it not like this?)

public class MainActivity extensions ActionBarActivity{

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle;
    private ListView mMenuListView;

    @ Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        mDrawerLayout=(DrawerLayout) findViewById (R.id.drawer_layout);

        // Configuring DrawerToggle
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.app_name, R.string.app_name);
        mToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mToggle);
        mToggle.syncState();

        // Drawer ListView Menu
        String [ ] items = {"Menu 1", "Menu 2", "Menu 3" };
        ArrayAdapter<String>adapter=newArrayAdapter<(this,android.R.layout.simple_list_item_1,items);

        // Configuring ListView on Drawer
        mMenuListView= (ListView) findViewById (R.id.drawer_menu_listview);
        mMenuListView.setAdapter(adapter);
        mMenuListView.setOnItemClickListener(newAdapterView.OnItemClickListener(){
            // ListView Selection Event for Drawer
            @ Override
            public void onItemClick(AdapterView<?>parent, View view, int position, longid) {
                switch(position){
                    case0:
                        // Display FragmentA when the first item is selected
                        getSupportFragmentManager().beginTransaction().replace(R.id.container,FragmentA.newInstance()) .commit();
                        break;
                    case1:
                        // Display FragmentB if second item is selected
                        getSupportFragmentManager().beginTransaction().replace(R.id.container,FragmentB.newInstance()) .commit();
                        break;
                }
                mDrawerLayout.closeDrawer(mMenuListView);
            }
        });
    }

    @ Override
    public boolean onCreateOptionsMenu(Menu){
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @ Override
    public boolean onOptionsItemSelected(MenuItem){
        return mToggle.onOptionsItemSelected(item)||super.onOptionsItemSelected(item);
    }
}
<android.support.v4.widget.DrawerLayout
    android: id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <ListView
        android:id="@+id/drawer_menu_listview"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#eeddee"/>
</android.support.v4.widget.DrawerLayout>
public class FragmentAextendsFragment{

    public static FragmentA newInstance(){
        return new FragmentA();
    }

    publicFragmentA(){
    }

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

    @ Override
    publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_a, container, false);

        // Configure MyPagerAdapter for ViewPager
        ViewPager=(ViewPager)view.findViewById(R.id.pager);
        pager.setAdapter(new MyPagerAdapter(getActivity()));

        // Configure ViewPager for PagerSlidingTabStrip
        PagerSlidingTabStrip=(PagerSlidingTabStrip)view.findViewById(R.id.tabs);
        tabs.setViewPager(pager);

        return view;
    }

    private class MyPagerAdapter extensions PagerAdapter {

        // page content data
        private String [ ] mContents = {"Page 1", "Page 2", "Page 3", "Page 4", "Page 5" };
        private Context mContext;

        public MyPagerAdapter (Context context) {
            mContext=context;
        }

        @ Override
        public intgetCount(){
            return mContents.length;
        }

        @ Override
        publicCharSequence getPageTitle(int position){
            // Show Title n for now
            return "Title" + (position+1);
        }

        @ Override
        publicObject instantiateItem(ViewGroup container, int position){
            // Show TextView for now
            TextView textView = new TextView (mContext);
            textView.setText(mContents[position]);
            container.addView(textView);
            return textView;
        }

        @ Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @ Override
        public boolean isViewFromObject(View view,Object object){
            return view.equals(object);
        }
    }
}
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android: orientation="vertical">
    <com.astuetz.PagerSlidingTabStrip
        android: id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"/>
    <android.support.v4.view.ViewPager
        android: id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
public class FragmentBextends Fragment{

    public static FragmentB newInstance(){
        return new FragmentB();
    }

    publicFragmentB(){
    }

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

    @ Override
    publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflator.inflate(R.layout.fragment_b, container, false);
    }
}
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android: orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 2"/>
</LinearLayout>


2022-09-30 20:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.