gpt4 book ai didi

java - Fragment 白页上的 RecyclerView

转载 作者:行者123 更新时间:2023-12-02 02:05:50 25 4
gpt4 key购买 nike

我已经为选项卡创建了一个 fragment ,并按照代码在该 fragment 中放置了一个 RecyclerView:

fragment 1 Java:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

mView = inflater.inflate(R.layout.fragment_two, container, false);
mRecyclerView = (RecyclerView) mView.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyRecyclerViewAdapter(getDataSet());
mRecyclerView.setAdapter(mAdapter);

return inflater.inflate(R.layout.fragment_two, container, false);
}

fragment 1 XML:

    <android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>

Activity 调用 fragment 选项卡:

 private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "Cliente");
adapter.addFragment(new TwoFragment(), "Prodotti");
viewPager.setAdapter(adapter);
}

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

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

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

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

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

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

我没有任何错误,但调试显示:

E/RecyclerView: No adapter attached; skipping layout

我在谷歌上搜索过,但没有解决我的问题,你有什么建议吗?如果您需要更多代码,请询问,无论如何,谢谢..

最佳答案

问题出在线条

    mView = inflater.inflate(R.layout.fragment_two, container, false);
........

return inflater.inflate(R.layout.fragment_two, container, false);

当您编写return inflater.inflate(R.layout.fragment_two,container, false);时,您正在重新膨胀 View 并返回没有任何初始化的新 View 。

<小时/>

你只需要返回之前膨胀的 View (即mView)。

所以你的代码必须像

mView = inflater.inflate(R.layout.fragment_two, container, false);
mRecyclerView = (RecyclerView) mView.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyRecyclerViewAdapter(getDataSet());
mRecyclerView.setAdapter(mAdapter);

return mView;

关于java - Fragment 白页上的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50815409/

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