国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > Material Design TabLayout篇

Material Design TabLayout篇

来源:程序员人生   发布时间:2016-11-29 08:57:38 阅读次数:2556次

以下是在Tablayout中添加ViewPaGER 或Fragment,由于我的demo需求,我没有配置viewpager和fragment ,只需要搜索1下viewpager或fragment的适配器便可!
别搜了 甩 链接 我也不复制他人的代码 了 看链接吧
http://www.bubuko.com/infodetail⑼89657.html
/***************************************
* @desc 供应列表 猜想是不是可以与采购、图库列表共用1个activity ?
* @auther 李建飞
* @time 2016/9/18 14:41
**************************/
public class SupplyActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener, CommonAdapter.OnItemClickListioner {
private SupplyActivity supplyActivity;
/容器区/
private String[] titles = new String[]{“已发布”, “审核中”, “未通过”, “已过期”};//tablayout的标题
private List mDataList;
/控件区/
private ViewPager vpager_act_supply;
private RecyclerView recy_act_supply;
private TabLayout tab_act_supply;// 4 个标签头

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_supply);
    supplyActivity = this;
    initData();
    initView();
}


private void initData() {
    mDataList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        mDataList.add("商品" + i);
    }
}

private void initView() {

// vpager_act_supply = fView(R.id.vpager_act_supply);
recy_act_supply = fView(R.id.recy_act_supply);
tab_act_supply = fView(R.id.tab_act_supply);
setMyTitle(“我的供应列表”);
Toolbar titleTB = fView(R.id.titleTB);
titleTB.inflateMenu(R.menu.toolbar_menu_addsupp);// 添加“添加供应”按钮
titleTB.setBackgroundColor(Color.parseColor(“#F5F5F5”));//修改title背风景为类白色
titleTB.setOnMenuItemClickListener(this);
tab_act_supply.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
// vp_act_supply.setCurrentItem(tab.getPosition());
// DebugLogUtil.getInstance().Debug(“onTabSelected选中了” );
}

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
             /*上次点击的不是这个tab*/

// DebugLogUtil.getInstance().Debug(“onTabUnselected” );
}

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            /*屡次点击同1个tab*/
            //    DebugLogUtil.getInstance().Debug("onTabReselected" + tab.getPosition());
        }
    });
    tab_act_supply.setTabsFromPagerAdapter(pagerAdapter);
    tab_act_supply.setScrollPosition(0, 0f, true);// 默许选择第1项
    recy_act_supply.setLayoutManager(new LinearLayoutManager(this));

// final LinearLayout.LayoutParams midParam = new LinearLayout.LayoutParams(DensityUtil.getDpFromScreenWidth(supplyActivity, 50), LinearLayout.LayoutParams.WRAP_CONTENT);
// midParam.setMargins(10, 0, 0, 0);
final LinearLayout.LayoutParams leftParam = new LinearLayout.LayoutParams(DensityUtil.getDpFromScreenWidth(supplyActivity, 20), LinearLayout.LayoutParams.WRAP_CONTENT);
recy_act_supply.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
CommonAdapter adapter = new CommonAdapter(this, mDataList, R.layout.adapter_supplylist) {
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ImageView pic_suplist = (ImageView) holder.fMview(R.id.pic_suplist);
// LinearLayout tvs_suplist = (LinearLayout) holder.fMview(R.id.tvs_suplist);
// LinearLayout bts_suplist = (LinearLayout) holder.fMview(R.id.bts_suplist);
TextView name_suplist = (TextView) holder.fMview(R.id.name_suplist);
name_suplist.setText(mDataList.get(position));
pic_suplist.setLayoutParams(leftParam);// 简单控制1下左边图片的宽度 动态适配屏幕
// tvs_suplist.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// tvs_suplist.setLayoutParams(midParam);
// bts_suplist.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
// bts_suplist.setLayoutParams(leftParam);
}
};
adapter.setOnItemClickListioner(this);
recy_act_supply.setAdapter(adapter);

}


/**
 * viewpager  tablayout共用的适配器
 */
private PagerAdapter pagerAdapter = new PagerAdapter() {
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (position == 0) {

        }
        return getTextViewForTest(container, titles[position]);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
};

@NonNull
private Object getTextViewForTest(ViewGroup container, String title) {
    TextView tv_title = new TextView(SupplyActivity.this);
    tv_title.setTextSize(60.f);
    tv_title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    tv_title.setText(title);
    container.addView(tv_title);
    return tv_title;
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.addSupply:
            startActivity(GivePriceActivity.class, null);
            break;
        default:
            break;
    }
    return false;
}

@Override
public void onItemClick(int position) {
    Toast.makeText(SupplyActivity.this, "你好...." + position, Toast.LENGTH_SHORT).show();
}

}

小诀窍:
TabLayout 怎样设置进入界面后默许选择第1项呢 tab_act_supply.setScrollPosition(0, 0f, true);

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生