我目前有一个回收器视图,它从我的firebase数据库中提取数据。回收器视图通过放入我不必填满列表的项目而过度填充。我已经尝试将layout_height从匹配父项更改为包装内容。这只是让我所有的数据都消失了。我只是试图用来自我的Firebase的准确数据准确地填充recyclerView。以下是XML的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#FCF9F9"
tools:context=".RecentReviews">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/_150sdp"
android:background="#62BEE5">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginBottom="0dp"
android:background="@drawable/backgroun_pattern_small_" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/back_button"
android:id="@+id/backButtonHistory"
android:layout_marginTop="@dimen/_15sdp"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginRight="@dimen/_5sdp"/>
<TextView
android:fontFamily="@font/radomir_tinkov_gilroy_regular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:textColor="#fff"
android:textSize="@dimen/_13ssp"
android:id="@+id/backButton"
android:textStyle="bold"
android:layout_toRightOf="@id/backButtonHistory"
android:layout_marginTop="@dimen/_15sdp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="History"
android:fontFamily="@font/radomir_tinkov_gilroy_regular"
android:textColor="#fff"
android:textStyle="bold"
android:textSize="@dimen/_15ssp"
android:layout_marginTop="@dimen/_15sdp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/image_uploadHistoy"
android:layout_width="@dimen/_120sdp"
android:layout_height="@dimen/_130sdp"
android:src="@drawable/user"
app:civ_border_color="#696969"
app:civ_border_width="3dp"
android:layout_marginTop="@dimen/_70sdp"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Cole Stradtmann"
android:fontFamily="@font/radomir_tinkov_gilroy_black"
android:textStyle="bold"
android:textColor="#000"
android:id="@+id/txtDriverNameHist"
android:textSize="@dimen/_15ssp"
android:layout_below="@id/image_uploadHistoy"
android:layout_marginTop="@dimen/_5sdp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/job_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/txtDriverNameHist"
android:layout_alignParentStart="true"
android:layout_margin="@dimen/_10ssp"
/>
</RelativeLayout>
下面是Java:
package com.example.usub;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.usub.Model.History;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
public class RecentReviews extends AppCompatActivity {
private RecyclerView jobList;
private DatabaseReference jobRef;
private FirebaseAuth mAuth;
private String currentUserId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recent_reviews);
mAuth = FirebaseAuth.getInstance();
currentUserId = mAuth.getCurrentUser().getUid();
jobRef = FirebaseDatabase.getInstance().getReference().child("RateDetails").child(currentUserId);
jobList = findViewById(R.id.job_list);
jobList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
jobList.setLayoutManager(linearLayoutManager);
DisplayAllJobs();
}
private void DisplayAllJobs() {
//Query query = jobRef.orderByChild("comments"); // haven't implemented a proper list sort yet.
FirebaseRecyclerOptions<History> options = new FirebaseRecyclerOptions.Builder<History>().setQuery(jobRef, History.class).build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<History, jobsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final jobsViewHolder friendsViewHolder, int position, @NonNull History friends) {
//friendsViewHolder.setDate(friends.getDate());
final String usersIDs = getRef(position).getKey();
jobRef.child(usersIDs).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
final String rateScore = dataSnapshot.child("rates").getValue().toString();
final String comment = dataSnapshot.child("comments").getValue().toString();
jobsViewHolder.setRate(rateScore);
jobsViewHolder.setComments(comment);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public jobsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.allratedisplay, parent ,false);
return new jobsViewHolder(view);
}
};
adapter.startListening();
jobList.setAdapter(adapter);
}
public static class jobsViewHolder extends RecyclerView.ViewHolder
{
static View mView;
public jobsViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
}
public static void setRate(String rateScore) {
TextView myName = (TextView) mView.findViewById(R.id.scoreRate);
myName.setText(rateScore);
}
public static void setComments(String comment) {
TextView myName = (TextView) mView.findViewById(R.id.scoreComment);
myName.setText(comment);
}
}
}
在RecyclerView
上尝试app:Layout_Behavior
,如下所示-
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/job_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_below="@+id/txtDriverNameHist"
android:layout_alignParentStart="true"
android:layout_margin="@dimen/_10ssp"/>
那么即使使用layout_height=“wrap_content”
,recycle视图也会显示。您还可以将NestedScrollView
作为RecycleView
的父级,但这不是一个好的代码实践。
附加小费
如果您打算将LinearLayout与RecycleView一起使用,那么将LayoutManager
放在xml本身中是一个不错的选择-
<androidx.recyclerview.widget.RecyclerView
............
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="vertical"/>
您可以设置LinearLayoutManager(default orientation=“Vertical”)
,GridLayoutManager
或StaggeredGridLayoutManager
。
快乐的编码!