android 从后台动态生成tab 使用viewpager + fragment ( 在fragment上生成多个framgent)

本文介绍如何在Android应用中动态创建Tab,并使用ViewPager结合Fragment进行滑动切换。通过在Fragment上生成多个子Fragment,实现了动态生成tab的功能。文章提供了关键代码片段,并提到了在动态操作Tab时遇到的Fragment缓存问题及其解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     由于公司需要,现在在写android,然后就遇到了一个动态生成tab顶部选项框的需求(怎么办,我也很无奈啊),经过两天的怕坑总算是完成了,所以,下面我就要贴代码了(就是这么直接)。

     首先,写一个可以左右滑动的父fragment的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:clipToPadding="false"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="2px"
        android:background="#f7f8fb" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal">
    <android.support.design.widget.TabLayout
        android:id="@+id/found_tabLayout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        app:tabIndicatorColor="@color/a1"
        app:tabSelectedTextColor="@color/a1"
        app:tabTextAppearance="@style/tablayout_textSize"
        app:tabTextColor="@color/a2" />
    </ScrollView>

    <android.support.v4.view.ViewPager
        android:id="@+id/found_viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:id="@+id/show_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:visibility="gone">


        <View
            android:id="@+id/bar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_24"
            android:background="@color/white_100" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="50dp"
                android:src="@mipmap/t_find" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_15"
                android:text="还没有内容"
                android:textColor="#aaaaaa"

                android:textSize="@dimen/sp_15" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

 

然后是父fragment

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gtyc.estudy.R;
import com.gtyc.estudy.student.adapter.MyPagerAdaptre;
import com.gtyc.estudy.student.util.SharedPrenfenceUtil;
import com.gtyc.estudy.student.util.StringVlue;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 发现
 */
public class SecondPageFragment extends Fragment {
    private View mainView;
    TabLayout found_tabLayout;
    ViewPager found_viewPager;
    private ArrayList<String> titleList;
    private MyPagerAdaptre foundAdapter;
    private Context cx;
    private ArrayList mListData;
    private String userId, loginSignId;//用户名,用户账号
    private OkHttpClient okHttpClient = new OkHttpClient();
    private SharedPrenfenceUtil sp;
    private List<Fragment> fragments ;
    private LinearLayout linearLayout;


    private void findId() {
        found_tabLayout = mainView.findViewById(R.id.found_tabLayout);
        found_viewPager = mainView.findViewById(R.id.found_viewPager);
        linearLayout = mainView.findViewById(R.id.show_layout);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(savedInstanceState != null){
            String FRAGMENTS_TAG = "android:support:fragmen
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值