项目之使用TabLayout,实现页签的切换

初始化布局

1、创建数组,mFragments = new BaseFragment[4];

2、实例化,  mFragments[0] = NewsClassfiFragment.newInstance(ONE);

3、设置Tab的模式,TabMode:布局中Tab的行为模式(behavior mode),  mTabs.setTabMode(TabLayout.MODE_FIXED);

4、实例化适配器,  mAdapter = new BaseFragmentAdapter(getChildFragmentManager(), mFragments, titles);

    private String[] titles = {"头条", "NBA", "汽车", "笑话"};
    private BaseFragment[] mFragments;
    private BaseFragmentAdapter mAdapter;
    public static final int ONE = 0;
    public static final int TWO = 1;
    public static final int THREE = 2;
    public static final int FOUR = 3;
    @Override
    protected void initView() {
        mFragments = new BaseFragment[4];
        mFragments[0] = NewsClassfiFragment.newInstance(ONE);
        mFragments[1] = NewsClassfiFragment.newInstance(TWO);
        mFragments[2] = NewsClassfiFragment.newInstance(THREE);
        mFragments[3] = NewsClassfiFragment.newInstance(FOUR);
        mTabs.setTabMode(TabLayout.MODE_FIXED);
        mAdapter = new BaseFragmentAdapter(getChildFragmentManager(), mFragments, titles);
        mViewPager.setAdapter(mAdapter);
        mTabs.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(this);
    }

5、创建 NewsClassfiFragment.class

package com.huading.baselhz;

import android.os.Bundle;

import com.huading.baselhz.base.BaseFragment;

/**
 * 作者:LHZ on 2017/5/8 10:27
 * 界面:
 */
public class NewsClassfiFragment extends BaseFragment {


    public static BaseFragment newInstance(int type) {
        Bundle bundle = new Bundle();
        NewsClassfiFragment fragment = new NewsClassfiFragment();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_news_classfi;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void lazyFetchData() {

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个常见的Android开发问题,需要使用TabLayout和ViewPager2来实现多个面的导航和切换。以下是实现步骤的大致流程: 1. 创建一个新的Android项目,并在布局文件中添加TabLayout和ViewPager2控件。 2. 在MainActivity中,创建一个FragmentPagerAdapter类来管理ViewPager2中的不同面。 3. 在FragmentPagerAdapter类中,创建多个Fragment类来显示不同的面内容。 4. 在每个Fragment中,使用RecyclerView来显示商品列表,并在RecyclerView的Adapter中设置商品列表项的布局和内容。 5. 在MainActivity中,将TabLayout和ViewPager2绑定在一起,并将FragmentPagerAdapter中的Fragment添加到ViewPager2中。 6. 运行应用程序,查看效果。 以下是一个示例代码,供您参考: MainActivity.java ``` import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentPagerAdapter; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager2.adapter.FragmentStateAdapter; import androidx.viewpager2.widget.ViewPager2; import com.google.android.material.tabs.TabLayout; public class MainActivity extends AppCompatActivity { private TabLayout tabLayout; private ViewPager2 viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabLayout = findViewById(R.id.tab_layout); viewPager = findViewById(R.id.view_pager); // 添加面 viewPager.setAdapter(new MyPagerAdapter(this)); // 将TabLayout和ViewPager2绑定在一起 tabLayout.setupWithViewPager(viewPager); } private static class MyPagerAdapter extends FragmentStateAdapter { public MyPagerAdapter(MainActivity activity) { super(activity); } @Override public Fragment createFragment(int position) { switch (position) { case 0: return new RecommendFragment(); case 1: return new CategoryFragment(); case 2: return new CartFragment(); default: return new Fragment(); } } @Override public int getItemCount() { return 3; } } } RecommendFragment.java ``` import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class RecommendFragment extends Fragment { private RecyclerView recyclerView; private List<Goods> goodsList = new ArrayList<>(); @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_recommend, container, false); // 初始化商品列表数据 initGoods(); // 初始化RecyclerView recyclerView = view.findViewById(R.id.recycler_view); LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); recyclerView.setLayoutManager(layoutManager); GoodsAdapter adapter = new GoodsAdapter(goodsList); recyclerView.setAdapter(adapter); return view; } private void initGoods() { for (int i = 0; i < 20; i++) { Goods goods = new Goods(); goods.setImage(R.drawable.ic_launcher_foreground); goods.setName("商品" + i); goods.setDesc("这是一件很好的商品!"); goods.setPrice(100.00); goodsList.add(goods); } } } ``` fragment_recommend.xml ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> ``` Goods.java ``` public class Goods { private int image; private String name; private String desc; private double price; public int getImage() { return image; } public void setImage(int image) { this.image = image; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } } ``` GoodsAdapter.java ``` import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.ViewHolder> { private List<Goods> goodsList; static class ViewHolder extends RecyclerView.ViewHolder { ImageView imageView; TextView nameText; TextView descText; TextView priceText; public ViewHolder(View view) { super(view); imageView = view.findViewById(R.id.image_view); nameText = view.findViewById(R.id.name_text); descText = view.findViewById(R.id.desc_text); priceText = view.findViewById(R.id.price_text); } } public GoodsAdapter(List<Goods> goodsList) { this.goodsList = goodsList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_item, parent, false); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { Goods goods = goodsList.get(position); holder.imageView.setImageResource(goods.getImage()); holder.nameText.setText(goods.getName()); holder.descText.setText(goods.getDesc()); holder.priceText.setText("¥" + String.format("%.2f", goods.getPrice())); } @Override public int getItemCount() { return goodsList.size(); } } ``` goods_item.xml ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <ImageView android:id="@+id/image_view" android:layout_width="80dp" android:layout_height="80dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginLeft="16dp"> <TextView android:id="@+id/name_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:text="商品名称" /> <TextView android:id="@+id/desc_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:text="商品介绍" /> <TextView android:id="@+id/price_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" android:text="商品价格" /> </LinearLayout> </LinearLayout> ``` 以上是一个简单的示例,您可以根据自己的需求和设计来修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值