gpt4 book ai didi

android - PageView 中的 RecyclerView 未显示列表

转载 作者:行者123 更新时间:2023-11-30 00:44:16 26 4
gpt4 key购买 nike

Conc.: 我正在尝试在 PagerView 中查看 RecyclerView

问题:RecyclerView 没有显示

此外,我已经准备好了RecyclerView in PagerView Question知道我必须为 Recycler 准备另一个 fragment ,我已经准备好了

注意:

  • PagerView 运行良好,因为它显示单个 TextView
  • RecyclerView 在 PageView 之前正常显示

我所做的更改后出现问题的更改:

  • 将 RecyclerView 移动到新的 Fragment 类

ExampleRecyclerViewFragment

public class ExampleRecyclerViewFragment extends Fragment {
public statlic ExampleAdapter adapter;
List<MainExampleObject> exampleList = new ArrayList<>();
public Example example = new Example();

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_two, container, false);

RecyclerView exampleRecyclerView = (RecyclerView) rootView.findViewById(R.id.ExampleRecyclerView);

exampleList = new ArrayList<>();

adapter = new ExampleAdapter(exampleList);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setSmoothScrollbarEnabled(true);
exampleRecyclerView.setScrollbarFadingEnabled(true);
exampleRecyclerView.setLayoutManager(llm);
exampleRecyclerView.setAdapter(adapter);

return rootView;
}

public void fetchExamples(final DataSnapshot Examples) {
//noinspection StatementWithEmptyBody
if (Examples.hasChild("Method 2")) {

} else {

Examples.getRef().child("Method 1").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
example.setStepName(String.valueOf(dataSnapshot.getKey()));

for (DataSnapshot childSnapshot : dataSnapshot.child("Code").getChildren()) {
example.addCode(String.valueOf(childSnapshot.getValue()));
}

for (DataSnapshot childSnapshot : dataSnapshot.child("Explaination").getChildren()) {
example.addExplanation(String.valueOf(childSnapshot.getValue()));
}
example.addExample();
exampleList.add(example.getExampleObject());
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});
}
}

public ExampleRecyclerViewFragment() {
// Required empty public constructor
}
}

ViewPagerAdapter

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

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

@Override
public Fragment getItem(int position) {
return new ExampleRecyclerViewFragment();
}

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

void addFragment(String title) {
//mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

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

fragment_two.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ExampleRecyclerViewFragment"
>

<android.support.v7.widget.RecyclerView
android:id="@+id/ExampleRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
/>

</LinearLayout>

我认为如果有人需要查看其他类(class),我认为代码将是必需的,请通过评论告诉我

很抱歉忘记提及 fetchExamples() 是在主类中调用 onCreate 的方法中调用的

new ExampleRecyclerViewFragment().fetchExamples(dataSnapshot.child("Examples"));

非常感谢

最佳答案

问题在于您在调用 fetchExamples 时不知道您的 fragment 是否已膨胀。您可以使用 eventBus 将 dataSnapshot 对象发送到您的 fragment 并在那里调用 fetchExamples。你的主类中的代码是,

EventBus.getDefault().postSticky(dataSnapshot.child("Examples"));

在你的 Fragment 中做,

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(DataSnapshot examples) {
fetchExamples(examples);
}

@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();

关于android - PageView 中的 RecyclerView 未显示列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42065424/

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