Tablayout+ViewPager動態新增fragment懶載入
一個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>
相關文章
- TabLayout+ViewPager 懶載入TabLayoutViewpager
- Fragment 懶載入實踐Fragment
- tablayout+viewpager+fragment懶載入TabLayoutViewpagerFragment
- Android優化--Fragment懶載入Android優化Fragment
- TabLayout-ViewPager填充Fragment和懶載入TabLayoutViewpagerFragment
- TabLayout+ViewPager+fragment實現懶載入TabLayoutViewpagerFragment
- Fragment的Adapter分析及懶載入FragmentAPT
- TabLayout+ViewPager+Fragment懶載入實現TabLayoutViewpagerFragment
- android viewpager使用Fragment懶載入,滑到當前fragment才進行資料載入AndroidViewpagerFragment
- Android tabLayout+viewPager+fragment處理懶載入AndroidTabLayoutViewpagerFragment
- Android 在 ViewPager 中使用 Fragment 的懶載入AndroidViewpagerFragment
- ViewPager+TabLayout+Fragment懶載入機制完全解析ViewpagerTabLayoutFragment
- 多層巢狀後的 Fragment 懶載入實現巢狀Fragment
- TabLayout+ViewPager+Fragment實現懶載入完全解決方案TabLayoutViewpagerFragment
- Tablayout+Viewpager+Fragment組合使用以及懶載入機制TabLayoutViewpagerFragment
- Android開發丶使用懶載入修復tablayout+viewpager+fragment的預載入AndroidTabLayoutViewpagerFragment
- 高德地圖fragment 動態載入地圖 巢狀問題地圖Fragment巢狀
- 懶載入
- Android:Fragment懶載入的實現以及自己的封裝思路AndroidFragment封裝
- 滾動載入圖片(懶載入)實現原理
- 動態建立FragmentFragment
- 懶載入和預載入
- iOS: 懶載入iOS
- 為.Net專案新增動態庫載入路徑
- 【譯】懶載入元件元件
- 圖片懶載入
- Vue元件懶載入Vue元件
- Ribbon - 懶載入
- vue路由懶載入Vue路由
- Vue 的懶載入Vue
- Swift中懶載入Swift
- hibernate懶載入
- spring 懶載入Spring
- 什麼是hibernate懶載入?什麼時候用懶載入?為什麼要用懶載入?(轉)
- DLL動態庫動態載入
- Vue-Router學習第二彈動態路由\懶載入\巢狀路由Vue路由巢狀
- Vue 路由按需載入(路由懶載入)Vue路由
- 圖片預載入和懶載入