标签:nal port rri protected position content activity center arguments
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/draw" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--标签布局--> <android.support.design.widget.TabLayout android:id="@+id/tab" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabGravity="center" app:tabTextColor="@color/colorPrimary" app:tabIndicatorColor="@android:color/transparent" app:tabSelectedTextColor="@color/colorAccent" app:tabMode="scrollable"> </android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> </LinearLayout> <!-- 菜单栏选项--> <ListView android:id="@+id/lv" android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@color/colorPrimary"> </ListView> </android.support.v4.widget.DrawerLayout>
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{ private List<String> list=new ArrayList<String>(); private String[] title={ "推荐","热点","北京","科技","军事","视频","社会" }; private DrawerLayout draw; private ListView lv; private ActionBarDrawerToggle toggle; private ArrayAdapter<String> adapter; private ViewPager pager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportActionBar().hide(); intdata(); inview(); } public void intdata(){ for (int i=0; i<5;i++){ list.add("sss"+i); } } public void inview(){ draw = (DrawerLayout) findViewById(R.id.draw); lv = (ListView) findViewById(R.id.lv); //初始化ActionBar //参数1:上下文 //参数2:抽屉 //参数3:打开抽屉的描述 //参数4:关闭抽屉的描述 // toggle = new ActionBarDrawerToggle(this,draw, R.string.openDrawer,R.string.closeDrawer); // //设置状态同步 // toggle.syncState(); //设置抽屉打开关闭监听 draw.addDrawerListener(toggle); //设置适配器 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); lv.setAdapter(adapter); lv.setOnItemClickListener(this); //找到标签布局控件 TabLayout tabLayout = (TabLayout) findViewById(R.id.tab); for (String str : title) { //更具标签数生成标签 tabLayout.addTab(tabLayout.newTab().setText(str)); } //TabLayout关联ViewPager pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(new Myadapter(getSupportFragmentManager())); //让TabLayout发生关联 tabLayout.setupWithViewPager(pager); } @Override public boolean onOptionsItemSelected(MenuItem item) { //如果抽屉打开,点击按钮,就会关闭,反之亦然 return toggle.onOptionsItemSelected(item) | super.onOptionsItemSelected(item); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //关闭抽屉 draw.closeDrawer(lv); } class Myadapter extends FragmentPagerAdapter{ public Myadapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return Fragment1.newInstance(title[position]); } @Override public int getCount() { return title.length; } @Override public CharSequence getPageTitle(int position) { return title[position]; } } }
public class Fragment1 extends Fragment { private static final String PAGE_TITLE = "PAGE_TITLE"; private String title; public static Fragment1 newInstance(String title){ Bundle bundle = new Bundle(); bundle.putString(PAGE_TITLE,title); Fragment1 fragment1 = new Fragment1(); fragment1.setArguments(bundle); return fragment1; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //当Fragemnt一旦被创建,我就可以接受对方传递过来的值 title = getArguments().getString(PAGE_TITLE); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setText(title); textView.setTextSize(50); return textView; } }
标签:nal port rri protected position content activity center arguments
原文地址:http://www.cnblogs.com/leshen/p/7470450.html