在抽屉片段

时间:2015-11-21 06:55:18

标签: java android android-fragments slidingdrawer

只需要一个解决方法!

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

片段类:

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中的多个类。但还有其他方法可以完成这项工作吗?

1 个答案:

答案 0 :(得分:0)

AuthName "Admins Only" AuthUserFile /home/dd/.htpasswd AuthGroupFile /dev/null AuthType basic require user AuthorizedUser Options -Indexes <Files "admin-ajax.php"> Allow from all Satisfy all </Files> 置于由NavigationDrawer创建的ViewPager内,与将Fragment置于Activity内创建的简单片段中的方式相同。

类Fragment中的两个实现ViewPager,就像那样:

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

这里称为FragmentSlide的每个幻灯片标签都可以作为普通片段充气:

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