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<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();
    }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀:

    None Found