gpt4 book ai didi

android - 当我移动到相邻页面之外时,带有 TabLayout 的 ViewPager 不显示来自 recyclerview 的列表

转载 作者:太空狗 更新时间:2023-10-29 14:00:44 24 4
gpt4 key购买 nike

这就是我的应用正在做的事情。我在 FragmentPagerAdapter 中使用 TabLayout 有四个选项卡:A、B、C 和 D。我只在 B 处显示数据。该应用程序在选项卡 B 启动。我可以从选项卡 B 向左翻页到 A,然后返回到选项卡 B,数据仍然显示正常。然后我可以从选项卡 B 向右翻页到选项卡 C,然后返回到选项卡 B,数据也可以正常显示。但是一旦我翻页到选项卡 D,然后翻页回到选项卡 B,页面就完全变空了。我显示的 RecyclerView 列表不再显示任何内容。我刚开始使用 FragmentPagerAdapterTabLayout(以及一般的 Android 编程),所以如果我的代码有问题或其他问题,我深表歉意。

顺便说一句,我的 ViewPager 位于 Activity 内部(尽管各个页面都是 Fragment,希望不会造成混淆)所以我无法使用 getChildFragmentManager() 很多答案都在暗示(除非我做错了)。我在这里阅读了 viewpager 上的许多答案,但似乎他们遇到的问题与我不同。这是我的代码:

主要 Activity :

public class MainActivity extends AppCompatActivity
{
private Toolbar mToolbar;
private ViewPager mViewPager;
private TabLayout mTabLayout;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mToolbar = (Toolbar) findViewById(R.id.toolbar);
mViewPager = (ViewPager) findViewById(R.id.main_activity_view_pager);
mTabLayout = (TabLayout) findViewById(R.id.main_activity_tab_layout);

setSupportActionBar(mToolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayShowTitleEnabled(false);

NavDrawerFragment navDrawerFrag = (NavDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_nav_drawer);
navDrawerFrag.setUp((DrawerLayout)findViewById(R.id.nav_drawer), mToolbar);

setupViewPagerAdapter(mViewPager);
mTabLayout.setupWithViewPager(mViewPager);

mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
@Override
public void onTabSelected(TabLayout.Tab tab)
{
mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab)
{

}

@Override
public void onTabReselected(TabLayout.Tab tab)
{

}
});

private void setupViewPagerAdapter(ViewPager viewPager)
{
MainPagerAdapter pagerAdapter = new MainPagerAdapter(getSupportFragmentManager());

//order is very important here; they correspond to the tab position from left to right
pagerAdapter.addFragment(MyGamesFragment.newInstance(), "My Games");
pagerAdapter.addFragment(AllGamesFragment.newInstance(), "All Games");
pagerAdapter.addFragment(AllPlacesFragment.newInstance(), "All Places");
pagerAdapter.addFragment(MyPlacesFragment.newInstance(), "My Places");

viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(1);
}

MainPagerAdapter(FragmentPagerAdapter 类):

public class MainPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

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

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

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

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

public void addFragment(Fragment fragment, String title)
{
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
}

我的 fragment (这代表我上面解释中的“选项卡 B”):

public class AllGamesFragment extends Fragment
{
private static final String ARG_ALLGAMES_FRAG_ID = "all_games_frag_id";
RecyclerView mAllGamesRecyclerView;
GameAdapter mGameAdapter;

public static AllGamesFragment newInstance()
{
return new AllGamesFragment();
}

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.all_games_recycler_view, container, false);
mAllGamesRecyclerView = (RecyclerView) view.findViewById(R.id.all_games_recycler_view);
mAllGamesRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateList();

return view;
}

@Override
public void onResume()
{
super.onResume();
updateList();
}

private void updateList()
{
if(mGameAdapter == null)
{
mGameAdapter = new GameAdapter();
mAllGamesRecyclerView.setAdapter(mGameAdapter);
}
}

private class GameHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private TextView mTeamOne, mTeamTwo, mGameStartTime;
private ImageView mTeamOneLogo, mTeamTwoLogo;

public GameHolder(View itemView)
{
super(itemView);
itemView.setOnClickListener(this);

mTeamOne = (TextView) itemView.findViewById(R.id.team_one_name);
mTeamTwo = (TextView) itemView.findViewById(R.id.team_two_name);
mTeamOneLogo = (ImageView) itemView.findViewById(R.id.team_one_logo);
mTeamTwoLogo = (ImageView) itemView.findViewById(R.id.team_two_logo);
mGameStartTime = (TextView) itemView.findViewById(R.id.game_time);
}

@Override
public void onClick(View v)
{

}
}

private class GameAdapter extends RecyclerView.Adapter<GameHolder>
{
@Override
public GameHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.list_game_item, parent, false);
return new GameHolder(view);
}

@Override
public void onBindViewHolder(GameHolder holder, int position)
{

}

@Override
public int getItemCount()
{
return 10;
}
}

如您所见,我还没有使用实际数据,我只是想让 UI 正常工作。

最佳答案

更改代码

if(mGameAdapter == null)
{
mGameAdapter = new GameAdapter();
mAllGamesRecyclerView.setAdapter(mGameAdapter);
}

    mGameAdapter = new GameAdapter();
mAllGamesRecyclerView.setAdapter(mGameAdapter);

关于android - 当我移动到相邻页面之外时,带有 TabLayout 的 ViewPager 不显示来自 recyclerview 的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35743762/

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