如何实现只有一个圆形进度条,直到图像加载了滑动



你好,我只想实现一个循环进度动画,直到从幻灯片加载图像,但在占位符中实现动画后,它会为每个单独的图像提供动画,并且图像会更改为占位符图像大小,我不希望

我希望在布局的中心只有一个循环进度动画,直到图像加载到回收器视图中

Post_Item_Container.xml

<com.google.android.material.imageview.ShapeableImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="6dp"
android:contentDescription="@string/todo"
app:shapeAppearanceOverlay="@style/RoundedCorner" />

Fragment_Search.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/search_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">
<androidx.appcompat.widget.SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="15dp"
android:background="@color/grey"
android:overScrollMode="never" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/search_view"
android:overScrollMode="never">

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/listText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:paddingStart="40dp"
android:paddingEnd="0dp"
android:text="@string/today_s_most_liked_posts"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="italic" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawable="@drawable/hu3fv"
android:layout_marginTop="240dp"
android:gravity="center_vertical|center_horizontal"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/postRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:overScrollMode="never"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>

Search_Fragment.java

public class Search_Fragment extends Fragment {
private final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("uploads");
public List<Upload> mUploads;
PostAdapter_Search postsAdapter;
RecyclerView postRecyclerView;
ProgressBar progressBar;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
postRecyclerView = view.findViewById(R.id.postRecyclerView);
progressBar = view.findViewById(R.id.progressBar);
postRecyclerView.setLayoutManager(
new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
);
getData();
progressBar.setVisibility(View.VISIBLE);
mUploads = new ArrayList<>();
postsAdapter = new PostAdapter_Search(getContext(), mUploads);
postRecyclerView.setAdapter(postsAdapter);
return view;
}

private void getData() {
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
mUploads.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
progressBar.setVisibility(View.GONE);
postRecyclerView.setVisibility(View.VISIBLE);
Upload upload = dataSnapshot.getValue(Upload.class);
upload.setmKey(dataSnapshot.getKey());
mUploads.add(upload);

}
}
//notify the adapter
postsAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}

由于您正在使用RecyclerView,并且由于您在onBindViewHolder方法中执行此操作

@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
Glide.with(mcontext)
.load(uploadCurrent.getmImageUrl())
.diskCacheStrategy(DiskCacheStrategy.DATA)
.placeholder(R.drawable.hu3fv)
.dontAnimate()
.into(holder.imageView);

}

在某种程度上,你将其应用于所有RecyclerView项目,因此它是重复的,因为这就是RecyclerView的目的

如果你想要一个单独的CircularProgressBar,你需要将其添加到你的FragmentActivity布局中,这取决于你使用的是什么,比如这个

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:visibility="visible" />

一旦数据加载到RecyclerView中,您可以根据需要设置其大小并隐藏/显示其可见性

我知道你想为Glide图像加载显示ProgressBar,但不建议这样做,因为在每个图像加载完成后,你需要某种回调。

最新更新