Tablayout+ViewPager動態新增fragment懶載入

天才第一步_發表於2017-11-08

一個fragment通過tab集合的數量來動態建立

效果圖(這裡就一個fragment)

       

1匯入依賴

       implementation 'com.android.support:design:28.0.0'

2main佈局檔案,design版本低的話tabIndicatorFullWidth方法可能會出現找不到 ,實現的是下劃線和內容寬度一致

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="lingniu.renhekeji.com.myapplication.MainActivity">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="#EA5404"
        app:tabIndicatorHeight="3dp"
        app:tabIndicatorFullWidth="false"
        app:tabTextAppearance="@style/TabLayoutTextStyle"
        app:tabSelectedTextColor="#EA5404"
        app:tabTextColor="#666666"
        app:tabMode="scrollable"
        app:tabBackground="@color/transparent"
        app:tabRippleColor="@color/transparent"
        />

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


</LinearLayout>

3MainActivity


import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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

public class MainActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPage;
    private List<Fragment> frags;
    private List<String> titles;
    private Fragment[] f;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPage= (ViewPager) findViewById(R.id.viewPage);
        tabLayout= (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        frags=new ArrayList<>();
        frags.add(new Fragment1());
        titles=new ArrayList<>();
        titles.add("標題1");
        titles.add("標題2");
        titles.add("標題3");
        titles.add("標題4");
        titles.add("標題5");
        tabLayout.setupWithViewPager(viewPage);
        Myadapter adapter=new Myadapter(getSupportFragmentManager());
        //聯動
        viewPage.setAdapter(adapter);
        viewPage.setOffscreenPageLimit(titles.size());
    }
    //介面卡
    class Myadapter extends FragmentPagerAdapter {

        public Myadapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return getfragment(position);
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }
    //動態建立Fragment的方法
    public Fragment  getfragment(int position){
        f=new Fragment[5];
        Fragment fg = f[position];
        if (fg == null) {
            fg = Fragment1.getiniturl(position+"");
            f[position] = fg;
        }
        return fg;
    }
}

 4BaseFragment

package lingniu.renhekeji.com.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * 懶載入
 */
public abstract class BaseFragment extends Fragment {
    private boolean isViewCreated;
    private boolean isLoadDataComplete;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && !isLoadDataComplete && isViewCreated) {
            isLoadDataComplete = true;
            loadData();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        initView(view);

        isViewCreated = true;
        return view;
    }

    protected abstract void initView(View view);

    protected abstract int getLayoutId();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getUserVisibleHint() && !isLoadDataComplete) {
            isLoadDataComplete = true;
            loadData();
        }
    }

    protected abstract void loadData();
}

5fragment


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

/**
 * Created by dell on 2019/12/9.
 */

public class Fragment1 extends BaseFragment{
    private TextView tv;
    //回撥用來接收引數
    public static Fragment1 getiniturl(String gc_id) {
        Fragment1 twoFragment = new Fragment1();
        Bundle bundle = new Bundle();
        bundle.putString("gc_id", gc_id);
        twoFragment.setArguments(bundle);
        return twoFragment;
    }

    @Override
    protected void initView(View view) {
        //獲取引數
        String  gc_id = getArguments().getString("gc_id");
        tv=view.findViewById(R.id.tv);
        tv.setText("標題"+gc_id);
    }

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

    @Override
    protected void loadData() {

    }
}


5建立fragment佈局

 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sssssssssssssssss" />
</LinearLayout>

 

 

相關文章