tablayout实现添加图片与文字

现在项目使用Tablayout实现添加文字与图片一起,点击某项是,实现文字更换颜色,图片更换。

具体实现效果。

下面贴出具体实现代码:

 

布局文件 icon_view:

 

<?xml version="1.0" encoding="utf-8"?>
<com.zhy.autolayout.AutoLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="65dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_centerInParent="true"
    android:orientation="vertical"
    android:padding="10px">

    <ImageView
        android:id="@+id/tabicon"
        android:layout_width="24dp"
        android:layout_height="24dp" />

    <TextView
        android:id="@+id/tabtext"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="商城"
        android:textColor="@color/black"
        android:textSize="14sp" />

</com.zhy.autolayout.AutoLinearLayout>

Fragment的适配器

 

public class PagerAdapter extends FragmentPagerAdapter {
    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return str[position];

    }

    @Override
    public Fragment getItem(int position) {
        Log.e("position", position + "");
        Fragment fragment = null;
        if (position == 2) {
            fragment = new CreatorShopFragment();
            Bundle bundle = new Bundle();
            bundle.putString("CatalogId", "0");
            bundle.putString("mAccountIdAdmin", hisShopBean.getData().getMemberInfo().get(0).getAccount_ID_admin() + "");//品牌店铺id
            bundle.putString("Member_ID", mUserId);//创客id
            fragment.setArguments(bundle);
        } else {
            fragment = new SafAccountFragment();
            Bundle bundle = new Bundle();
            bundle.putString("CatalogId", "0");
            bundle.putString("catalogName", str[position]);
            fragment.setArguments(bundle);
        }

        return fragment;
    }

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


    public View getTabView(int position) {
        View v = LayoutInflater.from(MakerShopActivity.this).inflate(R.layout.icon_view, null);
        ImageView iv = v.findViewById(R.id.tabicon);
        TextView tv = v.findViewById(R.id.tabtext);
        iv.setBackgroundResource(ints[position]);
        tv.setText(str[position]);
        if (position == 0) {
            tv.setTextColor(v.getResources().getColor(R.color.saf_indus_blue));
        }
        return v;
    }

}

最后调用:

 

mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
vpContent.setAdapter(mPagerAdapter);
tab.setupWithViewPager(vpContent);

for (int i = 0; i < tab.getTabCount(); i++) {
    tab.getTabAt(i).setCustomView(mPagerAdapter.getTabView(i));
}


tab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                tab.getCustomView().findViewById(R.id.tabicon).setBackgroundResource(R.drawable.mallhomeblue);
                break;
            case 1:
                tab.getCustomView().findViewById(R.id.tabicon).setBackgroundResource(R.drawable.mallvideoblue);
                break;
            case 2:
                tab.getCustomView().findViewById(R.id.tabicon).setBackgroundResource(R.drawable.mallgoodsblue);
                break;
            case 3:
                tab.getCustomView().findViewById(R.id.tabicon).setBackgroundResource(R.drawable.mallnewblue);
                break;
            case 4:
                tab.getCustomView().findViewById(R.id.tabicon).setBackgroundResource(R.drawable.mallactivityblue);
                break;
        }
        TextView textView = tab.getCustomView().findViewById(R.id.tabtext);
        textView.setTextColor(getResources().getColor(R.color.saf_indus_blue));


    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                tab.getCustomView().findViewById(R.id.tabicon).setBackgroundResource(R.drawable.mallome);
                break;
            case 1:
                tab.getCustomView().findViewById(R.id.tabicon).setBackgroundResource(R.drawable.mallvideo);

                break;
            case 2:
                tab.getCustomView().findViewById(R.id.tabicon).setBackgroundResource(R.drawable.mallgoods);
                break;
            case 3:
                tab.getCustomView().findViewById(R.id.tabicon).setBackgroundResource(R.drawable.mallnew);
                break;
            case 4:
                tab.getCustomView().findViewById(R.id.tabicon).setBackgroundResource(R.drawable.mallactivity);
                break;
        }
        TextView textView = tab.getCustomView().findViewById(R.id.tabtext);
        textView.setTextColor(getResources().getColor(R.color.black));

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
});
private String[] str = {"首页", "视频", "商品", "上新", "活动"};
private int[] ints = {R.drawable.mallhomeblue, R.drawable.mallvideo, R.drawable.mallgoods, R.drawable.mallnew, R.drawable.mallactivity};

最后,完成。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TabLayout本身并不支持左右滑动效果,但可以结合ViewPager实现。具体实现步骤如下: 1.在布局文件中添加TabLayout和ViewPager: ``` <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMode="scrollable" app:tabGravity="fill"/> <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent"/> ``` 其中,tabMode设置为scrollable,表示TabLayout可以左右滑动;tabGravity设置为fill,表示TabLayout中的Tab均分宽度。 2.创建FragmentPagerAdapter,并设置给ViewPager: ``` public class MyPagerAdapter extends FragmentPagerAdapter { private List<Fragment> mFragments; private List<String> mTitles; public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) { super(fm); mFragments = fragments; mTitles = titles; } @Override public Fragment getItem(int position) { return mFragments.get(position); } @Override public CharSequence getPageTitle(int position) { return mTitles.get(position); } @Override public int getCount() { return mFragments.size(); } } // 在Activity中设置PagerAdapter List<Fragment> fragments = new ArrayList<>(); fragments.add(new Fragment1()); fragments.add(new Fragment2()); fragments.add(new Fragment3()); List<String> titles = new ArrayList<>(); titles.add("Tab1"); titles.add("Tab2"); titles.add("Tab3"); MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), fragments, titles); ViewPager viewPager = findViewById(R.id.view_pager); viewPager.setAdapter(adapter); ``` 3.将ViewPager设置给TabLayout: ``` TabLayout tabLayout = findViewById(R.id.tab_layout); tabLayout.setupWithViewPager(viewPager); ``` 至此,TabLayout就能够实现左右滑动效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值