【发布时间】:2020-05-19 13:07:38
【问题描述】:
我正在尝试从我的 firebase 中获取一些数据,因此我在活动中创建了 recycleview 来获取数据,并且它正常工作。
但是现在,我将我的回收添加到 Fragment 以获取数据,它设法获取数据但我看不到它,firenase(成功)告诉我它有效,但我看不到回收视图。
我越找越没找到解决办法,另外一个也有同样的问题,他也没找到解决办法,这里。
RecyclerView doesn't appear in Fragment
我尝试了这个问题的所有解决方案,但没有成功,现在我花了两个多月没有发现问题。
朋友片段
public class FriendsFragment extends Fragment {
DatabaseReference databaseReference;
RecyclerView recyclerView;
RecyclerView.Adapter adapter ;
ProgressDialog progressDialog;
List<ImageUploadInfo> list = new ArrayList<>();
private Context context;
public FriendsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(com.wanjy.dannie.rivchat.R.layout.fragment_people, container, false);
context = view.getContext();
recyclerView = (RecyclerView) view.findViewById(R.id.recycleListFriend);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading Images From Firebase.");
progressDialog.show();
databaseReference = FirebaseDatabase.getInstance().getReference("All_Image_Uploads_Database");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);
list.add(imageUploadInfo);
}
adapter = new RecyclerViewAdapter(context, list);
recyclerView.setAdapter(adapter);
progressDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Hiding the progress dialog.
progressDialog.dismiss();
}
});
return inflater.inflate(R.layout.fragment_people, container, false);
}
}
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<ImageUploadInfo> MainImageUploadInfoList;
public RecyclerViewAdapter(Context context, List<ImageUploadInfo> TempList) {
this.MainImageUploadInfoList = TempList;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ImageUploadInfo UploadInfo = MainImageUploadInfoList.get(position);
holder.imageNameTextView.setText(UploadInfo.getImageName());
//Loading image from Glide library.
Glide.with(context).load(UploadInfo.getImageURL()).into(holder.imageView);
}
@Override
public int getItemCount() {
return MainImageUploadInfoList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView imageNameTextView;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageNameTextView = (TextView) itemView.findViewById(R.id.ImageNameTextView);
}
}
}
ImageUploadInfo
public class ImageUploadInfo {
public String imageName;
public String imageURL;
public ImageUploadInfo() {
}
public ImageUploadInfo(String name, String url) {
this.imageName = name;
this.imageURL= url;
}
public String getImageName() {
return imageName;
}
public String getImageURL() {
return imageURL;
}
}
recyclerview_items
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="5dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="5dp"
card_view:cardMaxElevation="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#FFFFFF"
android:padding="1dp">
<ImageView
android:layout_height="50dp"
android:layout_width="50dp"
android:src="@android:drawable/ic_delete"
android:scaleType="fitXY"/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_weight="1.0"
android:padding="3dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#000000"
android:text="أخبار الدولة"
android:gravity="center"
android:id="@+id/myname"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="التاريخ"
android:id="@+id/date"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:src="@mipmap/ic_launcher"
android:background="#FFFFFF"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:layout_weight="1.0"
android:textSize="14sp"
android:textColor="#000000"
android:layout_gravity="right"
android:padding="4dp"
android:gravity="right"
android:id="@+id/ImageNameTextView"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#FFFFFF"
android:padding="2dp">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:background="#FFFFFF"
android:layout_weight="1.0"
android:gravity="center">
<ImageView
android:layout_height="30dp"
android:layout_width="30dp"
android:src="@android:drawable/ic_delete"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="0"
android:textColor="#000000"
android:padding="2dp"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:background="#FFFFFF"
android:layout_weight="1.0"
android:gravity="center">
<ImageView
android:layout_height="30dp"
android:layout_width="30dp"
android:src="@android:drawable/ic_delete"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="0"
android:textColor="#000000"
android:padding="2dp"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:background="#FFFFFF"
android:layout_weight="1.0"
android:gravity="center">
<ImageView
android:layout_height="30dp"
android:layout_width="30dp"
android:src="@android:drawable/ic_delete"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="0"
android:textColor="#000000"
android:padding="2dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
fragment_people
<RelativeLayout 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.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleListFriend"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
我的构建等级
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.google.firebase:firebase-core:9.8.0'
compile 'com.google.firebase:firebase-database:9.8.0'
compile 'com.google.firebase:firebase-auth:9.8.0'
testCompile 'junit:junit:4.12'
compile 'de.hdodenhof:circleimageview:2.1.0'
compile 'com.yarolegovich:lovely-dialog:1.0.4'
compile 'com.android.support:cardview-v7:25.0.1'
compile 'com.android.support:percent:25.0.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
- 注意 我在 Activity 中的回收工作没有问题,但它没有出现在我的片段中。
我希望有人在我的代码中发现问题。
【问题讨论】:
-
首先检查 if(snapShot.exists()) 是否有数据,然后在适配器中初始化你的数组,然后再分配一个数组。
-
@HaiderSaleem 它在活动中工作,但在片段中不起作用,应用程序给我列表中的帖子数量和值,但我看不到回收视图
-
请将您的数据库结构添加为 JSON 文件或至少是屏幕截图。
-
@AlexMamo 我解决了,看看我的回答,现在可以了。
标签: java android android-fragments firebase-realtime-database android-recyclerview