简体   繁体   English

在抽屉片段中实现滑动标签

[英]Implementing sliding tabs in drawer fragment

Just need a workaround! 只需要一个解决方法!

I had already implemented drawer, now need to place sliding tabs in one of those fragments. 我已经实现了抽屉,现在需要在其中一个片段中放置滑动标签。

Fragment Class: 片段类:

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
}

Method i am using needs to extend the same 'FragmentTwo' class to 'ActionBarActivity'. 我正在使用的方法需要将相同的'FragmentTwo'类扩展为'ActionBarActivity'。 Here is the code: 这是代码:

public class FragmentTwo extends ActionBarActivity {

// Declaring Your View and Variables

Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[] = {“Test1”, “Test2”, “Test3”};
int Numboftabs = 3;

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

// Creating The Toolbar and setting it as the Toolbar for the activity

toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);

// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);

// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);

// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});

// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

I know a class cannot be extended to more than one class in java. 我知道一个类不能扩展到java中的多个类。 But is there some other way to get this done? 但还有其他方法可以完成这项工作吗?

Placing a ViewPager inside a Fragment that is created by a NavigationDrawer is the same way as placing a ViewPager inside a simple Fragment created inside an Activity. ViewPager放置在由NavigationDrawer创建的Fragment中与将ViewPager放置在ViewPager内创建的简单Fragment中的方式相同。

Inside of class Fragment two implement the ViewPager like that: 类Fragment中的两个实现了ViewPager

public class FragmentTwo extends Fragment {
        private ViewPager pager;
        private ViewPagerAdapter adapter;
        private SlidingTabLayout tabs;
        private static CharSequence titles[];
        private final int numOfTabs = 3;

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_slidetab, container, false);

    // set your titles here
            adapter =  new ViewPagerAdapter(getActivity().getSupportFragmentManager(),titles, numOfTabs);

            pager = (ViewPager) rootView.findViewById(R.id.pager);
            pager.setAdapter(adapter);

            tabs = (SlidingTabLayout) rootView.findViewById(R.id.tabs);
            tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

            tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
                @Override
                public int getIndicatorColor(int position) {
                    return getResources().getColor(R.color.tabsScrollColor);
                }
            });

            tabs.setViewPager(pager);

            //mSwipeRefreshLayout = (SwipeRefreshLayout) layout.findViewById(R.id.swipeRefreshLayout);

            return rootView;
        }
    }

For the ViewPager you need an Adapter class: 对于ViewPager您需要一个Adapter类:

 public class ViewPagerAdapter extends FragmentStatePagerAdapter {

        CharSequence Titles[];
        int NumbOfTabs;

        public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
            super(fm);

            this.Titles = mTitles;
            this.NumbOfTabs = mNumbOfTabsumb;

        }

        @Override
        public Fragment getItem(int position) {

            if(position == 0)
            {
                FragmentForDay tab1 = FragmentSlide.newInstance(0);
                return tab1;
            }
            else if(position == 1)
            {
                FragmentSlide tab2 = FragmentSlide.newInstance(1);
                return tab2;
            }
            else
            {
                FragmentSlide tab3 = FragmentSlide.newInstance(2);
                return tab3;
            }

        }

    @Override
    public CharSequence getPageTitle(int position) {
        return Titles[position];
    }

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

And each of your slide tabs, called FragmentSlide here, can be inflated as a normal Fragment: 每个名为FragmentSlide的幻灯片都可以作为普通片段充气:

   public class FragmentSlide extends Fragment {

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

                // Inflate the layout for this fragment
                return rootView;
            }

  public static FragmentSlide newInstance(int selectedIdForIndex) {
        FragmentSlide fragment = new FragmentSlide();

        Bundle args = new Bundle();
        args.putInt(ARG_LAYOUT_ID, selectedIdForIndex);
        fragment.setArguments(args);

        return fragment;
        }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM