How to add swipe in tabs

拈花ヽ惹草 提交于 2019-12-24 23:40:00

问题


I am using support library to create action bar in my app. I have added actions in action bar thats working perfect. Now I edit tabs below that. But for changing tabs I have to click on tabs. I want to add swipe in this code. But Its difficult for me as I am taking reference from one link thats only show to add tabs and change them with on click on them. So please someone help me to add swipe from screen to change tabs.

Code-

public class Types extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.types);
        setupTabs();        
    }


    private void setupTabs() {
        android.support.v7.app.ActionBar ab = getSupportActionBar();
        ab.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );
        Tab tab = ab.newTab()
        .setText( R.string.frag1).setTabListener(new MyTabListener(this, Type1.class.getName()));
        ab.addTab(tab);

        tab = ab.newTab()
        .setText( R.string.frag2).setTabListener(new MyTabListener( this, Type2.class.getName() ) );
        ab.addTab(tab);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

     @Override
        public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_search:
        homeActivity();
        return true;

        default:
        return super.onOptionsItemSelected(item);   
        }
     }

     public void homeActivity() {
        Toast.makeText(this, "Home Option Selexted", Toast.LENGTH_SHORT).show();
     }

private class MyTabListener implements ActionBar.TabListener
{
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mFragName;

    public MyTabListener( Activity activity, String fragName )
    {
        mActivity = activity;
        mFragName = fragName;
    }

    @Override
    public void onTabReselected( Tab tab, 
        FragmentTransaction ft )
    {    
    }

    @Override
    public void onTabSelected( Tab tab, 
        FragmentTransaction ft )
    {
        mFragment = Fragment.instantiate( mActivity, mFragName );
        ft.add( android.R.id.content, mFragment );
    }

    @Override
    public void onTabUnselected( Tab tab, FragmentTransaction ft )
    {
        ft.remove( mFragment );
        mFragment = null;
    }
}

回答1:


Create ViewPagerAdapter class-

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    private final int PAGES = 4;

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

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new Type1();
            case 1:
                return new Type2();
            case 2:
                return new Type3();
            case 3:
                return new Type4();
            default:
                throw new IllegalArgumentException("The item position should be less or equal to:" + PAGES);
        }
    }

    @Override
    public int getCount() {
        return PAGES;
    }
}

Then in your Typle class-

public class Types extends ActionBarActivity {

    private ViewPager viewPager;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pagerad);
        viewPager = (ViewPager) findViewById(R.id.pager);
        ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDisplayShowHomeEnabled(true);
        viewPager.setOnPageChangeListener(onPageChangeListener);
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
        addActionBarTabs();
    }

    private ViewPager.SimpleOnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
    @Override
    public void onPageSelected(int position) {
    super.onPageSelected(position);
    ab.setSelectedNavigationItem(position);
      }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_search:
        homeActivity();
        return true;

        default:
        return super.onOptionsItemSelected(item);   
        }
     }

     public void homeActivity() {
        Toast.makeText(this, "Home Option Selexted", Toast.LENGTH_SHORT).show();
     }

    private void addActionBarTabs() {
        ab = getSupportActionBar();
        String[] tabs = { "TYPE 1", "TYPE 2", "TYPE 3", "TYPE 4" };
        for (String tabTitle : tabs) {
            ActionBar.Tab tab = ab.newTab().setText(tabTitle).setTabListener(tabListener);
            ab.addTab(tab);
        }
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    }

    private ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        }
    };

}   

Finally create pagerad xml-

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

Then create Each Type fragment like-

public class Type1 extends Fragment{

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



回答2:


You need to use the ViewPager. When implementing the viewpager from the android tutorial you simply need to focus on the getitem method of the SectionsPagerAdapter. Here you could pick the right fragment based on the position of the position of the "view".

Your challenge is that the swipe part of the app is now one activity only, and each view now must be implemented as a fragment. But this layout seems compatible with your current code (eg similar to the onTabSelected method).

You types.xml file should be a ViewPager:

<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="com.example.Types" />

Next, declare a ViewPager and SectionsPagerAdapter in you Types activity:

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

Now, in you OnCreate add:

// Create the adapter that will return a fragment for each swipe screen
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

The SectionsPagerAdapter is implemented as:

    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.
        if (position == 2) {
            return new FragmentA();
        } else if (position == 1) {
            return new FragmentB();
        } else {
            // Pattern from you code...
            return Fragment.instantiate( mActivity, mFragName );
        }
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

That's all :)



来源:https://stackoverflow.com/questions/21496358/how-to-add-swipe-in-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!