gpt4 book ai didi

java - Recycleview 没有出现在 Fragment 中

转载 作者:行者123 更新时间:2023-12-01 18:35:56 25 4
gpt4 key购买 nike

我正在尝试从我的 Firebase 获取一些数据,因此我在 Activity 中创建了 recycleview 来获取数据,并且它工作没有问题。

但是现在,我将回收添加到 fragment 中以获取数据,它成功获取了数据,但我看不到它,firenase(成功)告诉我它有效,但我看不到回收 View 。

我搜索了很多,没有找到任何解决方案,还有另一个有同样的问题,他没有找到其他解决方案,这里。

RecyclerView doesn't appear in Fragment

我尝试了这个问题的所有解决方案,但没有成功,现在我花了两个多月也没有找出问题所在。

FriendsFragment

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);
}
}
}

图片上传信息

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 中的回收工作没有问题,但它没有出现在我的 fragment 中。

我希望有人发现我的代码中的问题。

最佳答案

你能在 fragment 中显示任何 View 吗?我注意到您的 MyFragment 类正在初始化 fragment_people.xml 但您的 xml 类被命名为 Fragment.xml 除非这是一个拼写错误。在认为这是回收器 View 问题之前,请验证您可以在 fragment 中显示任何类型的 View 。

此外,在您的适配器中,您正在使用名为 recyclerview_items.xml 的项目布局,但您有名为 Recycle item 的类,除非这也是一个拼写错误。

关于java - Recycleview 没有出现在 Fragment 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60044951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com