Android滑动标签页

在这里插入图片描述

TabLayoutActivity.java

package cn.edu.zufe.app0406;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.os.Bundle;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import cn.edu.zufe.app0406.adapter.FragmentAdapter;

public class TabLayoutActivity extends AppCompatActivity {

    private AppBarLayout abl;
    private Toolbar tbTool;
    private TabLayout tl;
    private ViewPager vp;
    private SwipeRefreshLayout srl;
    private List<Fragment> fragments = new ArrayList<>();
    private FragmentAdapter fragmentAdapter;

    private void initViewPager() {
        List<String> titles = new ArrayList<>();
        titles.add("精选");
        titles.add("体育");
        titles.add("娱乐");
        titles.add("军事");
        titles.add("美食");
        titles.add("旅游");
        titles.add("风景");
        for(int i=0; i<titles.size(); i++) {
            tl.addTab(tl.newTab().setText(titles.get(i)));
        }
        for(int i=0; i<titles.size(); i++) {
            fragments.add(new FruitFragment());
        }
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_SET_USER_VISIBLE_HINT, fragments, titles);
        vp.setAdapter(fragmentAdapter);
        tl.setupWithViewPager(vp);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);

        abl = (AppBarLayout) findViewById(R.id.abl);
        tbTool = (Toolbar) findViewById(R.id.tb_tool);
        tl = (TabLayout) findViewById(R.id.tl);
        vp = (ViewPager) findViewById(R.id.vp);
        srl = (SwipeRefreshLayout) findViewById(R.id.srl);

        setSupportActionBar(tbTool);
        initViewPager();
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @SuppressLint("RestrictedApi")
            @Override
            public void onRefresh() {
                FruitFragment fragment = new FruitFragment();
                for(int i = 0; i < fragments.size(); i++) {
                    fragment = (FruitFragment) fragments.get(i);
                    if(fragment != null && fragment.isAdded() && fragment.isMenuVisible()) {
                        break;
                    }
                }
                fragment.refreshFruit();
                srl.setRefreshing(false);
            }
        });
    }
}

activity_tab_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".TabLayoutActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/abl" >

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            android:id="@+id/tb_tool" />

        <com.google.android.material.tabs.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tl"
            app:tabMode="scrollable" />
        
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/srl" >

        <androidx.viewpager.widget.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/vp" >

        </androidx.viewpager.widget.ViewPager>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

FruitAdapter.java

package cn.edu.zufe.app0406.adapter;

import android.content.Context;
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 com.bumptech.glide.Glide;
import com.google.android.material.card.MaterialCardView;

import java.util.List;

import cn.edu.zufe.app0406.R;
import cn.edu.zufe.app0406.bean.Fruit;


public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {

    private Context mContext;
    private List<Fruit> mFruitList;

    public FruitAdapter(List<Fruit> mFruitList) {
        this.mFruitList = mFruitList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private MaterialCardView cardView;
        private ImageView ivFruit;
        private TextView tvFruit;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = (MaterialCardView) itemView;
            ivFruit = (ImageView) cardView.findViewById(R.id.iv_fruit);
            tvFruit = (TextView) cardView.findViewById(R.id.tv_fruit);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.card_info_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Fruit fruit = mFruitList.get(position);
        holder.tvFruit.setText(fruit.getName());
        Glide.with(mContext).load(fruit.getImageUrl()).into(holder.ivFruit);
    }

    @Override
    public int getItemCount() {
        return mFruitList.size();
    }
}

FragmentAdapter.java

package cn.edu.zufe.app0406.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.List;

public class FragmentAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;
    private List<String> titles;

    public FragmentAdapter(@NonNull FragmentManager fm, int behavior, List<Fragment> fragments, List<String> titles) {
        super(fm, behavior);
        this.fragments = fragments;
        this.titles = titles;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值