非常感谢您阅读我的代码。我已经创建了TabLayout并为其分配了View Pager。我能够滑动以查看不同的碎片。我有三个片段,"所有","科学"和"世界"。
重要的是,我可以根据需要在回收站视图中加载数据,并且一切都可以正常使用MainActivity.java。但是,我为AllFragment尝试了同样的事情,但它没有用。有人可以帮我这个吗?谢谢!
这是我的 AllFragment.java
public class AllFragment extends Fragment
{
/*UI*/
private RecyclerView mPostList;
/*Firebase*/
private DatabaseReference mDatabasePost;
private DatabaseReference mDatabaseUser;
private Firebase firebaseRef;
private FirebaseAuth mAuth;
public AllFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_all, container, false);
/*UI*/
mPostList = (RecyclerView)view.findViewById(R.id.fragment_all_recycler_view);
/*Firebase*/
Firebase.setAndroidContext(getActivity());
firebaseRef = new Firebase(FIREBASE_URL);
mAuth = FirebaseAuth.getInstance();
mDatabaseUser = FirebaseDatabase.getInstance().getReference().child("User");
mDatabaseUser.keepSynced(true);
mDatabasePost = FirebaseDatabase.getInstance().getReference().child("Post");
mDatabasePost.keepSynced(true);
/*Others*/
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
mPostList.setHasFixedSize(true);
mPostList.setLayoutManager(layoutManager);
FirebaseRecyclerAdapter<Post, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(
Post.class,
R.layout.post_row,
PostViewHolder.class,
mDatabasePost
)
{
@Override
protected void populateViewHolder(PostViewHolder viewHolder, Post model, int position)
{
final String post_key = getRef(position).getKey();
viewHolder.setDebate(model.getDebate());
viewHolder.setDisplayName("by " + model.getDisplayName());
final String room_name = model.getDebate();
viewHolder.mView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i = new Intent(getActivity(), SingleDebateActivity.class);
i.putExtra("room_name", room_name);
i.putExtra("post_key", post_key);
startActivity(i);
}
});
}
};
mPostList.setAdapter(firebaseRecyclerAdapter);
return view;
}
public static class PostViewHolder extends RecyclerView.ViewHolder
{
View mView;
FirebaseAuth mAuth;
public PostViewHolder(View itemView)
{
super(itemView);
mView = itemView;
mAuth = FirebaseAuth.getInstance();
}
public void setDebate(String debate)
{
TextView postDebate = (TextView)mView.findViewById(R.id.post_row_display_debate);
postDebate.setText(debate);
}
public void setDisplayName(String displayName)
{
TextView postDisplayName = (TextView)mView.findViewById(R.id.post_row_display_displayName);
postDisplayName.setText(displayName);
}
}
}
这是我的 fragment_all.xml
<FrameLayout
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">
<LinearLayout
android:id="@+id/fragment_all_linear_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_all_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</FrameLayout>
这是我的 ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter
{
ArrayList<Fragment> fragments = new ArrayList<>();
ArrayList<String> tabTitles = new ArrayList<>();
public void addFragments(Fragment fragments, String titles)
{
this.fragments.add(fragments);
this.tabTitles.add(titles);
}
public ViewPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position)
{
return fragments.get(position);
}
@Override
public int getCount()
{
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position)
{
return tabTitles.get(position);
}
}