android - 如何只实现一个圆形进度条,直到图像被滑动加载
问题描述
您好,我只想实现一个循环进度动画,直到从滑行加载图像,但是在占位符中实现动画之后,它为每个单独的图像提供动画,并且图像更改为我不想要的占位符图像大小
我希望布局的中心应该只有一个圆形进度动画,直到图像加载到回收器视图中
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) {
}
});
}
}
解决方案
由于您使用的是 aRecyclerView
并且因为您在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
,您需要将其添加到您的Fragment
或Activity
布局中,具体取决于您使用的是这样的
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:visibility="visible" />
将数据加载到其中后,您可以根据需要设置其大小并隐藏/显示其可见性RecyclerView
我知道您想显示ProgressBar
图像Glide
加载,但不建议这样做,因为在每个图像完成加载后您都需要某种回调。
推荐阅读
- c# - WinUI DataGrid(来自 CommunityToolkit):如何获取当前选定的单元格?
- javascript - forEach 循环中的 event.addEventListener()
- python - 我怎样才能用 if/elif 做一个理解,而不是别的?
- ms-access - 单击按钮以从 MS Access 表单中的字段填充 Outlook 电子邮件
- linux - 在一定数量的字母之后和特定字符之前删除文本
- python - 在屏幕之间传递值
- reactjs - 反应日历不传递日期变量
- c++ - C++中auto关键字如何扣除类型
- windows - 如何在 Windows 10 上的分组目录的任务栏上显示完整的目录名称
- powershell - 如何安装特定版本的 Microsoft.UI.Xaml