TabLayout使用二
来源:互联网 发布:淘宝联盟怎么微信推广 编辑:程序博客网 时间:2024/06/05 00:07
package cn.hnshangyu.zuyu.fragment.myself;import android.os.Build;import android.os.Bundle;import android.support.annotation.RequiresApi;import android.support.design.widget.TabLayout;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentStatePagerAdapter;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.TextView;import java.util.ArrayList;import butterknife.Bind;import cn.hnshangyu.zuyu.BaseActivity;import cn.hnshangyu.zuyu.R;import cn.hnshangyu.zuyu.utils.LogUtils;import cn.hnshangyu.zuyu.utils.UIUtils;/** * 我的模块中我的收藏 */public class MyCollectActivity extends BaseActivity { @Bind(R.id.tab_layout_menu) TabLayout mTl; @Bind(R.id.vp) ViewPager mVp; /** * 直播 */ private CollectLiveFragment mLive; /** * 点播 */ private CollectOnDemandFragment mOnDemand; /** * 商品 */ private CollectCommodityFragment mCommodity; /** * 人才 */ private CollectTalentsFragment mTalents; /** * 项目 */ private MyCollectFragment mProject; /** * 供需 */ private CollectSupplyDemandFragment mSupplyDemand; private ArrayList<Fragment> mFragmentList; /** * 适配器 */ private DemandAdapter mAdapter; private String[] stringName; /** * 图标 */ private int[] tabIcons = { R.drawable.selector_tab, R.drawable.selector_tab1, R.drawable.selector_tab2, R.drawable.selector_tab3, R.drawable.selector_tab4, R.drawable.selector_tab5 }; @Override protected int getLayoutId() { return R.layout.activity_my_collect; } @Override protected void initData(Bundle savedInstanceState) { super.initData(savedInstanceState); stringName = getResources().getStringArray(R.array.my_collect_tab); //初始化各fragment mLive = CollectLiveFragment.newInstance(); mOnDemand = CollectOnDemandFragment.newInstance(); mCommodity = CollectCommodityFragment.newInstance(); mTalents = CollectTalentsFragment.newInstance(); mProject = MyCollectFragment.newInstance(); mSupplyDemand = CollectSupplyDemandFragment.newInstance(); //将fragment装进列表中 mFragmentList = new ArrayList<>(); mFragmentList.add(mLive); mFragmentList.add(mOnDemand); mFragmentList.add(mCommodity); mFragmentList.add(mTalents); mFragmentList.add(mProject); mFragmentList.add(mSupplyDemand); //设置TabLayout的模式 可滚动// mTl.setTabMode(TabLayout.MODE_FIXED); mTl.setTabMode(TabLayout.MODE_SCROLLABLE); mAdapter = new DemandAdapter(getSupportFragmentManager()); //viewpager加载adapter mVp.setAdapter(mAdapter); //TabLayout加载viewpager mTl.setupWithViewPager(mVp); /** * 一定要在设置适配器之后设置Icon */ mTl.getTabAt(0).setCustomView(getTabView(0)); mTl.getTabAt(1).setCustomView(getTabView(1)); mTl.getTabAt(2).setCustomView(getTabView(2)); mTl.getTabAt(3).setCustomView(getTabView(3)); mTl.getTabAt(4).setCustomView(getTabView(4)); mTl.getTabAt(5).setCustomView(getTabView(5)); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override protected void initView() { setTitleText(getString(R.string.my_collect)); onShowTopBack(true); } public View getTabView(int position) { View view = LayoutInflater.from(this).inflate(R.layout.item_tab, null); ImageView iv_head = (ImageView) view.findViewById(R.id.iv_head); TextView tv_item = (TextView) view.findViewById(R.id.tv_item); TextView tv_number = (TextView) view.findViewById(R.id.tv_number); tv_item.setText(stringName[position]); if (position == 0) { tv_number.setVisibility(View.VISIBLE); } else { tv_number.setVisibility(View.GONE); } iv_head.setImageResource(tabIcons[position]); return view; } /** * 适配器 */ public class DemandAdapter extends FragmentStatePagerAdapter { public DemandAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); }// //此方法用来显示tab上的名字// @Override// public CharSequence getPageTitle(int position) {// return getResources().getStringArray(R.array.my_collect_tab)[(position % mFragmentList.size())];// } }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:paddingBottom="@dimen/padding_5" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv_head" android:layout_width="30dp" android:layout_height="30dp" android:paddingLeft="@dimen/padding_5" android:paddingRight="@dimen/padding_5" android:paddingTop="@dimen/padding_5" android:src="@drawable/icon_live_select" /> <TextView android:id="@+id/tv_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/iv_head" android:layout_alignRight="@id/iv_head" android:layout_below="@id/iv_head" android:gravity="center_horizontal" android:text="@string/live" android:textColor="@color/yellow_text_bg" android:textSize="@dimen/small_size" /> <TextView android:id="@+id/tv_number" android:layout_width="15dp" android:layout_height="15dp" android:layout_alignRight="@id/iv_head" android:background="@drawable/yellow_circle_shap" android:gravity="center" android:text="3" android:textColor="@color/white" android:textSize="8sp" /></RelativeLayout>
阅读全文
0 0
- TabLayout使用二
- 二.tablayout结合viewPager使用
- TabLayout使用
- TabLayout使用
- TabLayout使用
- TabLayout使用
- TabLayout使用
- TabLayout使用
- TabLayout使用
- tablayout使用
- 使用TabLayout
- TabLayout使用
- Tablayout使用
- TabLayout使用
- TabLayout使用
- TabLayout使用
- TabLayout使用
- 二. TabLayout 主方法
- 找朋友
- 顺序查找法
- drools 嵌套对象的处理
- ThreadLocal
- Linux常用命令-
- TabLayout使用二
- Volley框架详解
- HTML+CSS进阶学习摘录(CSS图形)(六)
- RecycleView 滚动 平滑滚动到某位置
- MD5升级加密算法
- 网页背景图片不重复的铺满屏幕(HTML/JSP)
- ML实践-万事开头难
- 正则表达式的学习(摘录)
- C语言指针专题——序