【问题标题】:Implementing sliding tabs in drawer fragment在抽屉片段中实现滑动标签
【发布时间】:2015-11-21 06:55:18
【问题描述】:

只需要一个解决方法!

我已经实现了抽屉,现在需要在其中一个片段中放置滑动标签。

片段类:

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);
}
}

我使用的方法需要将相同的“FragmentTwo”类扩展到“ActionBarActivity”。代码如下:

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);
}

我知道在 java 中一个类不能扩展到多个类。但是还有其他方法可以完成这项工作吗?

【问题讨论】:

    标签: java android android-fragments slidingdrawer


    【解决方案1】:

    在由 NavigationDrawer 创建的 Fragment 中放置 ViewPager 与在 Activity 中创建的简单 Fragment 中放置 ViewPager 相同。

    类 Fragment 2 内部像这样实现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;
            }
        }
    

    对于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;
        }
        }
    

    您的每个幻灯片选项卡(此处称为 FragmentSlide)都可以像普通 Fragment 一样膨胀:

       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;
            }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多