[英]RecyclerView in PageView not showing list
濃度:我正在嘗試在 PagerView 中查看 RecyclerView
問題:未顯示 RecyclerView
另外,我在 PagerView Question 中准備了RecyclerView知道我必須為Recycler准備另一個片段並且我擁有它
注意:
我所做的更改在更改后出現問題:
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"));
非常感謝
問題在於您在不知道您的片段是否已膨脹的情況下調用 fetchExamples。 您可以使用 eventBus 將 dataSnapshot 對象發送到您的片段並在那里調用 fetchExamples。 主類中的代碼是,
EventBus.getDefault().postSticky(dataSnapshot.child("Examples"));
在你的片段中做,
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(DataSnapshot examples) {
fetchExamples(examples);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.