首页 > 解决方案 > 如何只实现一个圆形进度条,直到图像被滑动加载

问题描述

您好,我只想实现一个循环进度动画,直到从滑行加载图像,但是在占位符中实现动画之后,它为每个单独的图像提供动画,并且图像更改为我不想要的占位符图像大小

我希望布局的中心应该只有一个圆形进度动画,直到图像加载到回收器视图中

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

标签: androidandroid-glideplaceholderandroid-progressbar

解决方案


由于您使用的是 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,您需要将其添加到您的FragmentActivity布局中,具体取决于您使用的是这样的

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

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

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


推荐阅读