ViewPager+TabLayout+DrawerLayout的侧滑的多条目

XML

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

<!--
内容布局
菜单布局: layout_gravity
 -->

<!--
    match_parent 根据父布局适应
     wrap_content 根据内容适应
  -->

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/bottom_indicator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"

        app:tabTextColor="@color/colorPrimary"
        app:tabSelectedTextColor="@color/colorAccent"

        app:tabIndicatorColor="@android:color/transparent"
        app:tabIndicatorHeight="1dp"
        />

    <!-- app:tabTextAppearance="" -->

    <android.support.v4.view.ViewPager
        android:id="@+id/contents"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_indicator"/>
</RelativeLayout>

<FrameLayout
    android:id="@+id/left_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#ffffff" />

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

第一个Fragment的布局

<android.support.design.widget.TabLayout
    android:id="@+id/top_indicator"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:tabMode="scrollable"
    />

<android.support.v4.view.ViewPager
    android:id="@+id/contents"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/top_indicator"
    app:layout_constraintBottom_toBottomOf="parent"/>

侧滑的布局

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menus"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

侧滑多条目的布局

<ImageView
        android:id="@+id/icon"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:src="@mipmap/ic_launcher"/>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"/>

MainActivity加载侧滑

private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle toggle;
    private ViewPager contents;

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

        //为空才添加Fragment: 左边菜单s
        if(savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.left_drawer, new LeftFragment())
                    .commit();
        }

        //初始化View
        initView();

        //加载数据
        initData();

    }

    private void initData() {
    }

    private void initView() {
        //允许标题栏展示左边icon
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        drawerLayout = findViewById(R.id.drawer);

        //actionbar 和 DrawerLahyout中间人, 把中间人创建出来
        toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open_drawer, R.string.close_drawer);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        contents = findViewById(R.id.contents);

        //绑定adapter
        contents.setAdapter(new MainPageAdapter(getSupportFragmentManager()));
        TabLayout tabLayout = findViewById(R.id.bottom_indicator);
        //创建底部三个tab
        //tabLayout.addTab();
        //tabLayout.addTab();
        //tabLayout.addTab();
        //1 adapter里返回标题
        //2 根据ViewPager设置TabLayout
        tabLayout.setupWithViewPager(contents);
        //tabLayout.getTabAt(0).setText("首页");
        //tabLayout.getTabAt(1).setText("视频");
        //tabLayout.getTabAt(2).setText("我的");
        //tabLayout.getTabAt(0).setIcon();
    }

    //显示具体页面  0 - 3
    public void showPage(int position) {
        contents.setCurrentItem(position);
        drawerLayout.closeDrawer(Gravity.START);
    }

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

侧滑的加载

private ListView menus;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_left_drawer, container, false);
}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    menus = view.findViewById(R.id.menus);
    menus.setAdapter(new LeftMenuAdapter(getActivity()));

    menus.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //图片 不做跳转
            if(position == 0) {
                return;
            }

            //切换页面
            ((MainActivity)getActivity()).showPage(position - 1);
        }
    });
}

侧滑的加载

public class MainPageAdapter extends FragmentPagerAdapter {

private String[] menus = new String[]{
        "首页", "视频", "我的"
};

public MainPageAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {
    //使用集合存起来,不重复创建Fragment
    /*List<Fragment> fragmentList = new ArrayList<>();
    fragmentList.add(new HomeFragment());
    fragmentList.add(new VideoFragment());
    fragmentList.add(new MineFragment());
    return fragmentList.get(i);*/

    //FragmentPagerAdapter
    //show hide
    //add remove

    switch (i) {
        case 0:
            return new HomeFragment();
        case 1:
            return new VideoFragment();
        case 2:
            return new MineFragment();
        default:
            return new Fragment();
    }
}

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

@Override
public int getCount() {
    return menus.length;
}

}

public class LeftMenuAdapter extends BaseAdapter {

private String[] menus = new String[]{
        "首页", "视频", "我的"
};

private Context mContext;

public LeftMenuAdapter(Context mContext) {
    this.mContext = mContext;
}

@Override
public int getCount() {
    return menus.length + 1;
}

private final int ITEM_COUTN = 2;
private final int IMAGE_TYPE = 0;
private final int TEXT_TYPE = 1;

@Override
public int getViewTypeCount() {
    return ITEM_COUTN;
}

@Override
public int getItemViewType(int position) {
    return position == 0 ? IMAGE_TYPE : TEXT_TYPE;
}

@Override
public String getItem(int position) {
    //预防postion == 0的情况
    if (position == 0) {
        return null;
    }
    return menus[position - 1];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(
                getItemViewType(position) == IMAGE_TYPE ? R.layout.menu_image_item : R.layout.menu_text_item
                , parent, false);
        viewHolder = new ViewHolder(convertView);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    //
    if (getItemViewType(position) == TEXT_TYPE) {
        viewHolder.bindData(getItem(position));
    }
    //!!!!!!!!!!!!!!!!!!!!!!!!!!
    return convertView;
}

class ViewHolder {
    ImageView imageView;
    TextView textView;

    public ViewHolder(View itemView) {
        imageView = itemView.findViewById(R.id.icon);
        textView = itemView.findViewById(R.id.text);
        itemView.setTag(this);
    }

    public void bindData(String text) {
        textView.setText(text);
    }

    public void displayIcon() {

    }
}

}

第一个fragment的加载

public class HomePageAdapter extends FragmentPagerAdapter {

private String[] pageNames = new String[]{"推荐", "小视频", "视频", "热点", "北京", "娱乐", "财经", "军事"};

public HomePageAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {
    switch (i) {
        case 0:
            return new RecommendFragment();
        default:
            return new BaseFragment();
    }
}

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

@Override
public int getCount() {
    return pageNames.length;
}

}

第一个fragment的页面

private TabLayout tabLayout;
    private ViewPager contents;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    tabLayout = view.findViewById(R.id.top_indicator);
    contents = view.findViewById(R.id.contents);
    //
    //Fragment嵌套Fragment,使用getChildFragmentManager()
    //getFragmentManager();
    contents.setAdapter(new HomePageAdapter(getChildFragmentManager()));
    //TableLayout

    tabLayout.setupWithViewPager(contents);

    //FragmentActivity; Fragment 3.0//封装了Fragment, 让你在低版本Android上能用
    //
    //AppCompatActivity;//封闭了ActionBar
}

加载名字的代码

@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());

        textView.setText(getClass().getSimpleName());

        return textView;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值