gpt4 book ai didi

android - TabLayout+ViewPager+Fragment 第一个tab 没有显示数据?

转载 作者:行者123 更新时间:2023-11-29 02:35:38 25 4
gpt4 key购买 nike

我在Activity中使用TabLayout+ViewPager+Fragment,当我进入Activity时,第一个fragment没有显示数据,但是我滚动到第二个fragment然后滚动到第三个fragment,然后滚动到第一个再次 fragment ,第一个 fragment 显示数据。我不知道这是为什么。我尝试将 FragmentPagerAdapter 更改为 FragmentStatePagerAdapter,但没有用。以下是我的代码:

titles = new ArrayList<>();
titles.add("未使用");
titles.add("已使用");
titles.add("已失效");
fragments = new ArrayList<>();
fragments.add(new TicketUnuseFragment());
fragments.add(new TicketUseFragment());
fragments.add(new TicketInvalidFragment());

tabLayout = (TabLayout) findViewById(R.id.award_tablayout);
viewPager = (ViewPager) findViewById(R.id.award_view_pager);
viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager()));
tabLayout.setupWithViewPager(viewPager);

FragmentStatePagerAdapter

public class FragmentAdapter extends FragmentStatePagerAdapter {


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

@Override
public Fragment getItem(int position) {
return fragments.get(position);
}

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

@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}

}

TicketUnuseFragment 代码:

ublic class TicketUnuseFragment extends AbsFragment {

private RecyclerView recyclerView;
private AwardCenterRecyclerViewAdapter recyclerAdapter;

@Override
public void onServerMessage(Message msg) {
if (msg.obj != null) {
IResponseVO iResponseVO = (IResponseVO) msg.obj;
if (iResponseVO.getServerCode().equals(ServerCode.PatientCouponList)) {
if (HttpClient.RESULT_OK == iResponseVO.getStatus()) {
AwardCenterListResponse mpRespBean = (AwardCenterListResponse) iResponseVO;
recyclerAdapter.setData(mpRespBean.getList());
recyclerAdapter.notifyDataSetChanged();
}
}
}
}

@Override
protected View initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ticket_unused, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.unuse_ticket_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
recyclerAdapter = new AwardCenterRecyclerViewAdapter(mActivity);
recyclerView.setAdapter(recyclerAdapter);
return view;
}


@Override
protected void initVariables() {
}

@Override
protected void loadData() {
try {
RwardCenterRequest requestBean = new RwardCenterRequest();
requestBean.setTabType(1);
requestBean.setPage(0);
HttpClient.getInstance(mActivity).requestTextByPost(mActivity, requestHandler, ServerCode.PatientCouponList, requestBean);
} catch (Exception e) {
e.printStackTrace();
}
}

enter image description here

enter image description here谢谢,进阶!

最佳答案

要显示的选项卡的填充是通过 TabLayout.Tab 实例完成的。因此,在获取 TabLayout 的引用之后,在您的主代码中添加这些行。

 tabLayout.addTab(tabLayout.newTab().setText("未使用""));
tabLayout.addTab(tabLayout.newTab().setText("已使用"));
tabLayout.addTab(tabLayout.newTab().setText("已失效"));

关于android - TabLayout+ViewPager+Fragment 第一个tab 没有显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47283105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com