【问题标题】:RecyclerView in PageView not showing listPageView 中的 RecyclerView 不显示列表
【发布时间】:2017-02-06 10:25:36
【问题描述】:

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

问题:RecyclerView 未显示

另外,我已经准备好RecyclerView in PagerView Question,我知道我必须为 Recycler 准备另一个片段,而且我已经有了它

注意:

  • 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>

如果有人需要查看其他课程,我认为需要该代码,只需在评论中告诉我即可

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

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

非常感谢

【问题讨论】:

    标签: android android-viewpager android-recyclerview fragmentpageradapter


    【解决方案1】:

    问题在于,您调用 fetchExamples 时并不知道您的片段是否已膨胀。您可以使用 eventBus 将 dataSnapshot 对象发送到您的片段并在那里调用 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();
    

    【讨论】:

    • 非常感谢您的回答和解释,我认为它会起作用,但有一些小事情“无法解决”(粘性,ThreadMode,getDefault),我不知道 eventBus 所以会你帮我解决这个问题
    • 当然。 EventBus 只是一个用于松散耦合片段和活动的库。您通常会编写一个接口来将数据从活动或主片段发送到消费者片段。 EventBus 取代了它而无需声明类。粘性意味着在发布数据时消费者片段不需要存在。ThreadMode 只是告诉更新发生在 Main/UI 线程中。
    • greenrobot.org/eventbus 请参阅此以获取有关事件的更多信息
    • 非常感谢我会了解它,但我的意思是现在运行代码时无法解决 (sticky, ThreadMode, getDefault)
    • Guava 有一个事件总线,但我更喜欢绿色机器人。
    猜你喜欢
    • 2015-05-03
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2023-03-19
    • 2021-11-26
    • 2016-04-11
    相关资源
    最近更新 更多