TabLayout加FrameLayout实现点击切换Fragment(无过渡动画)

直接搬代码过来了

	//这里写在前面
	@InjectView(R.id.fl_frame)
    FrameLayout mFrame;
    @InjectView(R.id.tv_top)
    TextView tvLoginTop;
    private List<Fragment> fragments;
    private OnlineFragment onlineFragment;
    private OfflineFragment offlineFragment;
    private FragmentManager manager;
    		//这里写在onCreate
    		manager = getSupportFragmentManager();
    		//bundle1可以给fragment传值
    		Bundle bundle1 = new Bundle();
    		onlineFragment = new OnlineFragment();
   			offlineFragment = new OfflineFragment();
            onlineFragment.setArguments(bundle1);
            offlineFragment.setArguments(bundle1);
            fragments = new ArrayList<Fragment>();
        	fragments.add(onlineFragment);
        	fragments.add(offlineFragment);
        	//添加tab文字
        	ArrayList<String> titles = new ArrayList<>();
        	titles.add(getString(R.string.online));
        	titles.add(getString(R.string.offline));
        	for (int i = 0; i < titles.size(); i++) {
            	mTbGoodsList.addTab(mTbGoodsList.newTab().setText(titles.get(i)));
        	}
        	//绑定TabLayout和FrameLayout
        	manager.beginTransaction()
                .add(R.id.fl_frame, fragments.get(0))
                .commit();
                //选中的样式,layout是字体样式,放在下面
                final TextView tabSelectView = (TextView) LayoutInflater.from(当前Activity.this).inflate(R.layout.tablayout_select, null);
        	tabSelectView.setTextSize(16);//字体大小
        	tabSelectView.setText(R.string.online);//设置文字
        	//设置默认第一个选中且设置样式
       	 	mTbGoodsList.getTabAt(0).setCustomView(tabSelectView);
        	//点击tablayout的点击事件
        	mTbGoodsList.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
            //点击
                switchFragment(tab.getPosition());
                tabSelectView.setText(tab.getText());
                tab.setCustomView(tabSelectView);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            //没有被点击
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            //重新被点击
            }
        });
        	//方法类,用于切换fragment
     private void switchFragment(int position) {
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment showFragment = fragments.get(position);
        Fragment hideFragment = fragments.get(hide);
        if (!showFragment.isAdded()) {
            transaction.add(R.id.fl_frame, showFragment);
        }
        transaction.show(showFragment);
        transaction.hide(hideFragment);
        transaction.commit();
        hide = position;
    }
            

对应的acvtivity的样式(仅供参考)

	<com.google.android.material.tabs.TabLayout
        android:id="@+id/tb_goods_list"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        app:tabGravity="fill"
        app:tabMaxWidth="0dp"
        app:tabMode="fixed"
        app:tabTextAppearance="@style/TabLayoutTextStyle" />
        
    <FrameLayout
        android:id="@+id/fl_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

我的R.layout.tablayout_select的xml样式(仅供参考)

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:textSize="18sp"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_marginHorizontal="16dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

如果有什么不懂的可以私信或者在下面评论嗷

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值