Android实现滑动标签页

本文实例为大家分享了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 fragments = new ArrayList<>();
    private FragmentAdapter fragmentAdapter;

    private void initViewPager() {
        List titles = new ArrayList<>();
        titles.add("精选");
        titles.add("体育");
        titles.add("娱乐");
        titles.add("军事");
        titles.add("美食");
        titles.add("旅游");
        titles.add("风景");
        for(int i=0; i 
 

activity_tab_layout.xml




    

        

        
        
    

    

        

        

    

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 {

    private Context mContext;
    private List mFruitList;

    public FruitAdapter(List 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 fragments;
    private List titles;

    public FragmentAdapter(@NonNull FragmentManager fm, int behavior, List fragments, List 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();
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android实现滑动标签页)