Android activity+tabLayout+viewPager+fragment完美的解决传值问题
Android tabLayout+viewPager+fragment处理懒加载
Android 解决Tablayout+ViewPager+Fragment中Fragment重复加载数据的问题
这里在给大家总结一篇,之前自己遇到的一个问题,百度无果后,自己非常实用的一个技巧。
需求:
在做项目中,对于tab标签,有一个需求是通过后台接口,返回的status来展示或者隐藏tab标签
项目中应用的是TabLayout+ViewPager+Fragment
public class OrderLatestActivity extends BaseActivity implements TongYongView {
private View view;
private TabLayout tab;
private NoScrollViewPager vp;
private List<Fragment> tabTitle;
private RightReFragment reFragment;
private EasyReFragment easyReFragment;
private ImReFragment imReFragment;
private DoctoReFragment doctoReFragment;
private List<String> tabMsg;
private TabOrderAdapter orderAdapter;
private TongYongPresentImpl tongYongPresent;
private UserInfo userInfo;
private FormBody.Builder builder;
private DoctorInfoBean doctorInfoBean;
private Handler order_latest_handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case Constants.ONLINE_STATUS:
Utils.showDialog(false, OrderLatestActivity.this, "");
doctorInfoBean = (DoctorInfoBean) msg.obj;
//是否显示tab标签
String status = doctorInfoBean.getStatus();
Log.e("status >>>>", "handleMessage: " + status);
if (status .equals("3")) {
((ViewGroup) tab.getChildAt(0)).getChildAt(2).setVisibility(View.VISIBLE);
} else {
((ViewGroup) tab.getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("最新订单");
setStatusBg(3);
initViews();
getData();
initDatas();
}
private void initViews() {
userInfo = (UserInfo) ShareprefencesUtils.getBean(this, ShareprefencesUtils.USER_INFO);
tongYongPresent = new TongYongPresentImpl(this, new TongYongModelImpl());
tab = view.findViewById(R.id.tab);
vp = view.findViewById(R.id.vp);
}
@Override
protected void onResume() {
super.onResume();
Utils.showDialog(true, this, "加载中。。。");
getData();
}
/**
* 查看详细信息
*/
private void getData() {
builder = new FormBody.Builder();
builder.add("doctorId", userInfo.getUserid());
builder.add("ycgl_key", Utils.getMD5());
Log.e("详细信息", "getData: >>>>" + userInfo.getUserid() + "---" + Utils.getMD5() + "---" + userInfo.getAccess_token());
RequestBody requestBody = builder.build();
tongYongPresent.doTongYong(requestBody, userInfo.getAccess_token(), HDUrl.DOCTOR_INFO_DETAILS);
}
private void initDatas() {
tabTitle = new ArrayList<>();
tabMsg = new ArrayList<>();
orderAdapter = new TabOrderAdapter(getSupportFragmentManager(), tabTitle, tabMsg);
easyReFragment = new EasyReFragment();
reFragment = new RightReFragment();
doctoReFragment = new DoctoReFragment();
imReFragment = new ImReFragment();
tabTitle.add(easyReFragment);
tabTitle.add(reFragment);
tabTitle.add(doctoReFragment);
tabTitle.add(imReFragment);
tabMsg.add("one");
tabMsg.add("two");
tabMsg.add("three");
tabMsg.add("four");
//设置TabLayout的模式
tab.setTabMode(TabLayout.MODE_FIXED);
tab.setPadding(10, 10, 10, 10);
tab.addTab(tab.newTab().setText(tabMsg.get(0)));
tab.addTab(tab.newTab().setText(tabMsg.get(1)));
tab.addTab(tab.newTab().setText(tabMsg.get(2)));
tab.addTab(tab.newTab().setText(tabMsg.get(3)));
//viewpager加载adapter
vp.setAdapter(orderAdapter);
vp.setNoScroll(true);
//TabLayout加载viewpager
tab.setupWithViewPager(vp);
}
@Override
protected View addContentLayout() {
view = getLayoutInflater().inflate(R.layout.activity_order_latest, contentLayout, false);
return view;
}
@Override
public void tongYongSucess(Object value) {
if (value != null) {
Utils.showDialog(false, OrderLatestActivity.this, "");
doctorInfoBean = JSONObject.parseObject((String) value, DoctorInfoBean.class);
Message msg = Message.obtain();
msg.obj = doctorInfoBean;
msg.what = Constants.ONLINE_STATUS;
order_latest_handler.sendMessage(msg);
}
}
@Override
public void tongYongFail(int error, final String errorMsg) {
Utils.showDialog(false, OrderLatestActivity.this, "");
this.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.e("最新 >>>>", "run: " + errorMsg);
}
});
}
@Override
public void showProgress() {
}
@Override
public void hideProgress() {
}
}
<?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"
android:background="@color/white"
tools:context=".activity.OrderLatestActivity">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/one"
android:background="@color/home_view" />
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="@dimen/fifty"
android:layout_marginLeft="@dimen/ten"
android:layout_marginRight="@dimen/ten"
app:tabIndicator="@drawable/tab_indicator"
app:tabIndicatorColor="@color/main_color"
app:tabRippleColor="@color/transparent"
app:tabSelectedTextColor="@color/main_color" />
<com.yicheng.networkhospital.doctor.utils.NoScrollViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
总结
从OrderLatestActivity
请求接口成功后,hanlder回调中,可以看到通过status的不同状态值,来设置three
Tab标签的显示和隐藏,实测有效
//是否显示tab标签
String status = doctorInfoBean.getStatus();
Log.e("status >>>>", "handleMessage: " + status);
if (status .equals("3")) {
((ViewGroup) tab.getChildAt(0)).getChildAt(2).setVisibility(View.VISIBLE);
} else {
((ViewGroup) tab.getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
}