Android 解决Tablayout+ViewPager+Fragment中Fragment重复加载数据的问题
在项目实战中,大家或许都遇到过,在fragment中写数据接口。每次点击切换对应的fragment加载数据都有问题。在TabLayout上点击不相邻两个Fragment时候,加载数据加载两次, 而且加载按钮会一直加载.场景:Adapter : FramgmentPagerAdapter点击不相邻的Fragment时候, 数据重复加载.解决办法:Android tabLayout+viewPager
·
Android activity+tabLayout+viewPager+fragment完美的解决传值问题
Android tabLayout+viewPager+fragment处理懒加载
Android 关于动态设置tab显示和隐藏问题
在项目实战中,大家或许都遇到过,在fragment中写数据接口。每次点击切换对应的fragment加载数据都有问题。
在TabLayout上点击不相邻两个Fragment时候,加载数据加载两次, 而且加载按钮会一直加载.
场景:
Adapter : FramgmentPagerAdapter
点击不相邻的Fragment时候, 数据重复加载.
解决办法:
Android tabLayout+viewPager+fragment处理懒加载
这里总结一篇另外一种方式:
从图2中不难看出,主要处理是在TabAcitvity和TabFragment中做的处理,下面把代码粘贴出来
TabActivity.class
/**
* @author 拉莫帅
* @date 2022/4/2
* @address
* @Desc 选项卡
*/
public class TabActivity extends BaseActivity {
private View view;
private TabFragment tabFragment;
private RelativeLayout baseTitle;
@Override
protected BasePresenter createPresenter() {
return null;
}
//初始化布局
@Override
protected View addContentLayout() {
view = getLayoutInflater().inflate(R.layout.activity_tab, baseLayout, false);
return view;
}
//初始化控件
@Override
protected void initView() {
baseTitle = findViewById(R.id.baseTitle);
}
@Override
protected void initListener() {
}
//初始化数据
@Override
protected void initDatas() {
baseTitle.setVisibility(View.GONE);
tabFragment = new TabFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.ll_ment_top, tabFragment);
fragmentTransaction.commit();
}
}
activity_tab.xml
<?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"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/tab_view" />
<LinearLayout
android:id="@+id/ll_ment_top"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="@+id/ll_ment_bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="12"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
资源文件:
<color name="tab_view">#EEEEEE </color>
<color name="main_color">#02C5AF</color>
<color name="home_default_text">#999999</color>
tab_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners
android:topLeftRadius="25dp"
android:topRightRadius="25dp"
android:bottomLeftRadius="25dp"
android:bottomRightRadius="25dp"/>
<solid android:color="@color/main_color"/><!-- 填充颜色 -->
<stroke android:width="0.1mm" android:color="@color/main_color" /><!-- 描边,边框宽度、颜色 -->
</shape>
TabFragment.class
/**
* @author Martin-harry
* @date 2022/4/2
* @address
* @Desc TabFragment
*/
public class TabFragment extends Fragment implements View.OnClickListener {
private View view;
private Context mContext;
private FirstFragment firstFragment;
private SecondFragment secondFragment;
private ThirdFragment thirdFragment;
private RelativeLayout rl_first, rl_second, rl_third;
private TextView tv_first, tv_second, tv_third;
private View view_one, view_two, view_three;
private FragmentManager fragmentManager;
private FragmentTransaction transaction;
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.mContext = context;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_tab, container, false);
initViews();
return view;
}
private void hideFragment(FragmentTransaction transaction) {
if (firstFragment != null) {
transaction.hide(firstFragment);
}
if (secondFragment != null) {
transaction.hide(secondFragment);
}
if (thirdFragment != null) {
transaction.hide(thirdFragment);
}
}
private void initViews() {
rl_first = view.findViewById(R.id.rl_first);
rl_second = view.findViewById(R.id.rl_second);
rl_third = view.findViewById(R.id.rl_third);
rl_first.setOnClickListener(this);
rl_second.setOnClickListener(this);
rl_third.setOnClickListener(this);
tv_first = view.findViewById(R.id.tv_first);
tv_second = view.findViewById(R.id.tv_second);
tv_third = view.findViewById(R.id.tv_third);
view_one = view.findViewById(R.id.view_one);
view_two = view.findViewById(R.id.view_two);
view_three = view.findViewById(R.id.view_three);
view_one.setVisibility(View.VISIBLE);
view_two.setVisibility(View.GONE);
view_three.setVisibility(View.GONE);
tv_first.setTextColor(this.getResources().getColor(R.color.main_color));
tv_second.setTextColor(this.getResources().getColor(R.color.home_default_text));
tv_third.setTextColor(this.getResources().getColor(R.color.home_default_text));
fragmentManager = getFragmentManager();
transaction = fragmentManager.beginTransaction();
firstFragment = new FirstFragment();
transaction.add(R.id.ll_ment_bottom, firstFragment);
transaction.commit();
rl_first.setEnabled(false);
rl_second.setEnabled(true);
rl_third.setEnabled(true);
}
@Override
public void onClick(View v) {
transaction = fragmentManager.beginTransaction();
hideFragment(transaction);
switch (v.getId()) {
case R.id.rl_first:
view_one.setVisibility(View.VISIBLE);
view_two.setVisibility(View.GONE);
view_three.setVisibility(View.GONE);
tv_first.setTextColor(this.getResources().getColor(R.color.main_color));
tv_second.setTextColor(this.getResources().getColor(R.color.home_default_text));
tv_third.setTextColor(this.getResources().getColor(R.color.home_default_text));
if (firstFragment == null) {
firstFragment = new FirstFragment();
transaction.add(R.id.ll_ment_bottom, firstFragment);
} else {
transaction.show(firstFragment);
}
rl_first.setEnabled(false);
rl_second.setEnabled(true);
rl_third.setEnabled(true);
break;
case R.id.rl_second:
view_one.setVisibility(View.GONE);
view_two.setVisibility(View.VISIBLE);
view_three.setVisibility(View.GONE);
tv_first.setTextColor(this.getResources().getColor(R.color.home_default_text));
tv_second.setTextColor(this.getResources().getColor(R.color.main_color));
tv_third.setTextColor(this.getResources().getColor(R.color.home_default_text));
if (secondFragment == null) {
secondFragment = new SecondFragment();
transaction.add(R.id.ll_ment_bottom, secondFragment);
} else {
transaction.show(secondFragment);
}
rl_first.setEnabled(true);
rl_second.setEnabled(false);
rl_third.setEnabled(true);
break;
case R.id.rl_third:
view_one.setVisibility(View.GONE);
view_two.setVisibility(View.GONE);
view_three.setVisibility(View.VISIBLE);
tv_first.setTextColor(this.getResources().getColor(R.color.home_default_text));
tv_second.setTextColor(this.getResources().getColor(R.color.home_default_text));
tv_third.setTextColor(this.getResources().getColor(R.color.main_color));
if (thirdFragment == null) {
thirdFragment = new ThirdFragment();
transaction.add(R.id.ll_ment_bottom, thirdFragment);
} else {
transaction.show(thirdFragment);
}
rl_first.setEnabled(true);
rl_second.setEnabled(true);
rl_third.setEnabled(false);
break;
}
transaction.commit();
}
}
fragment_tab.xml
<?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"
android:background="@color/white"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/rl_first"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1">
<TextView
android:id="@+id/tv_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="First"
android:textColor="@color/main_color" />
<View
android:id="@+id/view_one"
android:layout_width="15dp"
android:layout_height="3dp"
android:layout_below="@+id/tv_first"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:background="@drawable/tab_button" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_second"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1">
<TextView
android:id="@+id/tv_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Second"
android:textColor="@color/home_default_text" />
<View
android:id="@+id/view_two"
android:layout_width="15dp"
android:layout_height="3dp"
android:layout_below="@+id/tv_second"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:background="@drawable/tab_button"
android:visibility="gone" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_third"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1">
<TextView
android:id="@+id/tv_third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Third"
android:textColor="@color/home_default_text" />
<View
android:id="@+id/view_three"
android:layout_width="15dp"
android:layout_height="3dp"
android:layout_below="@+id/tv_third"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:background="@drawable/tab_button"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
总结
最后在自己相对应的fragment中调用接口就可以了
更多推荐
所有评论(0)