如何将 RecyclerView 实现到 BottomNavigationBar 的 Fragment 中?

huangapple 未分类评论44阅读模式
英文:

How to implement RecyclerView to Fragment of BottomNavigationBar?

问题

我不知道如何将 ***RecyclerView*** 实现到片段中首先我开始解释我的代码

public class HomeFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }
}

所以 `HomeFragment` 应该打开 `fragment_home.xml`,它看起来像这样...

        <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NoteActivity">
    
       <TextView
           android:id="@+id/textView4"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginTop="356dp"
           android:layout_marginEnd="152dp"
           android:text="主页片段"
           android:textColor="#000000"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintTop_toTopOf="parent" />
    
       <androidx.recyclerview.widget.RecyclerView
           android:id="@+id/recycler_view"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           tools:layout_editor_absoluteX="0dp"
           tools:layout_editor_absoluteY="16dp" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>

是的它正在打开这个布局 ***但是*** 没有 ***RecyclerView***TextView 是正常的)。我在思考为什么会发生这种情况我得出结论我必须将下面的 `this class`(下面的代码实现到我的 `HomeFragment`或者只需连接它同样我不知道如何

public class NoteActivity extends AppCompatActivity {
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference notebookRef = db.collection("Notebook");
    
    private NoteAdapter adapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home);
    
        setUpRecyclerView();
    }
    
    public void setUpRecyclerView() {
        Query query = notebookRef.orderBy("title", Query.Direction.DESCENDING);
    
        FirestoreRecyclerOptions<Note> options = new FirestoreRecyclerOptions.Builder<Note>()
                .setQuery(query, Note.class)
                .build();
    
        adapter = new NoteAdapter(options);
    
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

请帮忙谢谢 :)
英文:

I don't know how to implement RecyclerView to fragment. First I start to explain my code.

public class HomeFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }
} 

So HomeFragment should open fragment_home.xml which look like this...

    <androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NoteActivity">

   <TextView
       android:id="@+id/textView4"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="356dp"
       android:layout_marginEnd="152dp"
       android:text="Home fragment"
       android:textColor="#000000"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintTop_toTopOf="parent" />

   <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/recycler_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:layout_editor_absoluteX="0dp"
       tools:layout_editor_absoluteY="16dp" />


</androidx.constraintlayout.widget.ConstraintLayout>

and yes it is opening this layout BUT without RecyclerView (TextView is OK).
I was thinking why it's happening and I came to the conclusion that I have to implement this class (down below) to my HomeFragment (or just connect that, again I don't know how)

public class NoteActivity extends AppCompatActivity {
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference notebookRef = db.collection("Notebook");

    private NoteAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home);

        setUpRecyclerView();
    }

    public void setUpRecyclerView() {
        Query query = notebookRef.orderBy("title", Query.Direction.DESCENDING);

        FirestoreRecyclerOptions<Note> options = new FirestoreRecyclerOptions.Builder<Note>()
                .setQuery(query, Note.class)
                .build();

        adapter = new NoteAdapter(options);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

Please help, thanks 如何将 RecyclerView 实现到 BottomNavigationBar 的 Fragment 中?

huangapple
  • 本文由 发表于 2020年4月11日 04:04:50
  • 转载请务必保留本文链接:https://java.coder-hub.com/61147870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定