Drawerlayout+actionbar+tablayout初始化+侧拉菜单

Drawerlayout+actionbar+tablayout初始化+侧拉菜单

package com.example.yuekaoone;

import android.app.Fragment;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.example.yuekaoone.activity.BaseActivity;
import com.example.yuekaoone.adapter.ViewPagerAdapter;
import com.example.yuekaoone.fragment.AFragment;
import com.example.yuekaoone.fragment.BFragment;
import com.example.yuekaoone.fragment.CFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    DrawerLayout drawerLayout;
    ViewPager viewpager;
    TabLayout tab;
    ActionBarDrawerToggle toggle;
    List<Fragment> list;
    ViewPagerAdapter adapter;
    @Override
    protected void initdata() {

        list = new ArrayList<>();
        list.add(new AFragment());
        list.add(new BFragment());
        list.add(new CFragment());
        adapter = new ViewPagerAdapter(getFragmentManager());
        adapter.setdata(list);

        tab.setupWithViewPager(viewpager);
        viewpager.setAdapter(adapter);
    }

    @Override
    protected void initView() {

        viewpager = findViewById(R.id.viewpager);
        tab = findViewById(R.id.tab);
        drawerLayout = findViewById(R.id.drawerlayout);
        initActionBar();
    }

    private void initActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        toggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                Log.e("zhx","打开抽屉");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                Log.e("zhx","关闭抽屉");
            }
        };

        toggle.syncState();

        drawerLayout.addDrawerListener(toggle);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected int setcon() {
        return R.layout.activity_main;
    }
}

布局文件

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerlayout"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_weight="95"
            android:layout_width="match_parent"
            android:layout_height="0dp">


        </android.support.v4.view.ViewPager>
        <android.support.design.widget.TabLayout
            android:id="@+id/tab"
            android:layout_width="match_parent"
            android:layout_weight="5"
            android:layout_height="0dp"

            ></android.support.design.widget.TabLayout>
    </LinearLayout>
    <fragment
        class="com.example.yuekaoone.fragment.MenuFragment"
        android:layout_gravity="start"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />


</android.support.v4.widget.DrawerLayout>

**

Adapter

**

package com.example.yuekaoone.adapter;

import android.app.Fragment;
import android.app.FragmentManager;
import android.support.annotation.Nullable;
import android.support.v13.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerAdapter extends FragmentPagerAdapter {
    List<Fragment> list;
    /**
     * @param fm
     * @deprecated
     */
    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
        list =new ArrayList<>();
    }

    public void setdata(List<Fragment> list){
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();

    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0:
                return "关注";
            case 1:
                return "消息";
            case 2:
                return "我的";
        }
        return null;
    }

    /**
     * @param i
     * @deprecated
     */
    @Override
    public Fragment getItem(int i) {
        Fragment fragment = list.get(i);

        return fragment;
    }

    @Override
    public int getCount() {
        return list.size();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值