作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我试图在 fragment 中显示 recyclerview 及其内容。我的 recyclerview 显示正常,但我想在 recyclerview 内显示的 User 对象却显示不正常。
我从数据库中获取了用户名,并希望将其设置为 TextView 来显示。该名称正在从 Firebase 中完美检索,因此它应该显示在回收器 View 上。我已将“custom_lists_users.xml”(我的自定义 View 来显示每个用户)膨胀到 UserRecyclerAdap (我的适配器)类中。
但是,我假设它没有正确膨胀,因为如果它正确膨胀,那么我会在我膨胀的 fragment 的回收器 View 中看到我的用户名。
我附加了两个我认为有问题的类(class),如果我遗漏了什么,请告诉我。谢谢!
UserRecyclerAdap 类:
public class UserRecyclerAdap extends RecyclerView.Adapter<UserRecyclerAdap.ViewHolder> {
private Context context;
private List<User> users;
public UserRecyclerAdap(Context context, List<User> users) {
this.context = context;
this.users = users;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_list_users, parent, false);
Log.d("layoutInflater", "inflated!");
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
String userName = users.get(i).getName();
holder.name.setText(userName);
Log.d("bindViewHolder", holder.name.toString());
}
@Override
public int getItemCount() {
return users.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
ImageView profilePic;
public ViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
}
}
}
UserFragment 类
public class UsersFragment extends Fragment {
private RecyclerView recyclerView;
private List<User> users;
private UserRecyclerAdap userRecyclerAdap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_users, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
users = new ArrayList<>();
readUsers();
return view;
}
public void readUsers() {
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("USERS");
//goes thru everythig stored in firebase database
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
users.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
//goes thru every object in db and makes a user obj out of data
User user = snapshot.getValue(User.class);
assert user != null;
assert firebaseUser != null;
if(user.getID().equals(firebaseUser.getUid())) {
users.add(user);
}
}
userRecyclerAdap = new UserRecyclerAdap(getContext(), users);
recyclerView.setAdapter(userRecyclerAdap);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
custom_list_users.xml 文件
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp">
<ImageView
android:id="@+id/profilePic"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginStart="-1dp"
android:layout_marginLeft="-1dp"
android:layout_marginTop="3dp"
app:srcCompat="@drawable/ic_user2_foreground"
tools:srcCompat="@drawable/ic_user2_foreground" />
<TextView
android:id="@+id/name"
android:layout_width="192dp"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="36sp" />
</LinearLayout>
</RelativeLayout>
最佳答案
请将您的custom_list_users.xml 文件替换为以下内容
<LinearLayout 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:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/profilePic"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/ic_user2_foreground"
tools:srcCompat="@drawable/ic_user2_foreground" />
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="36sp" />
</LinearLayout>
关于java - 无法在 UserRecyclerAdapter 类中使用 "custom_list_users.xml"来膨胀 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61814588/
所以我试图在 fragment 中显示 recyclerview 及其内容。我的 recyclerview 显示正常,但我想在 recyclerview 内显示的 User 对象却显示不正常。 我从数
我是一名优秀的程序员,十分优秀!