如何在等待加载数据时让进度条填充cardview

时间:2017-01-26 16:02:43

标签: android android-recyclerview progress-bar android-progressbar android-cardview

由于我从数据库中获取数据并使用毕加索填充我的cardview中的项目,因此我想在每个cardview中显示一个加载程序。在我的适配器中,我有这个代码:

public class CardViewAdapter extends RecyclerView.Adapter<CardViewAdapter.ViewHolder> {

    Context mContext;
    private static List<AnimalType> mAnimalData = new ArrayList<>();

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView petInfo;
        public ImageView imgURL;
        public ProgressBar progressBar;

        private Context itemContext;
        public ViewHolder(View v){
            super(v);
            imgURL = (ImageView) v.findViewById(R.id.pet_image);
            petInfo = (TextView) v.findViewById(R.id.pet_description);
            progressBar = (ProgressBar) v.findViewById(R.id.progressbar);

            itemContext = v.getContext();

            v.setOnClickListener(this);
        }

        @Override
        public void onClick(View v){
            Intent intent= new Intent(itemContext, DetailCardLayout.class);
            Integer position = getAdapterPosition();
            intent.putExtra("CARDVIEW_POSITION", position);

            //animation
            String transitionName = v.getResources().getString(R.string.transition_string);
            View viewStart = v.findViewById(R.id.entire_card);
            ActivityOptionsCompat options =
                    ActivityOptionsCompat.makeSceneTransitionAnimation((Activity)mContext, viewStart, transitionName);


            v.getContext().startActivity(intent, options.toBundle());
        }
    }

    //constructor
    public CardViewAdapter(Context mContext, List<AnimalType> mAnimalData){
        this.mAnimalData = mAnimalData;
    }

    //create new views
    @Override
    public CardViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_card_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);

        mContext = parent.getContext();

        return viewHolder;
    }

    //replace contents of view
    @Override
    public void onBindViewHolder(ViewHolder holder, int position){

        holder.petInfo.setText(mAnimalData.get(position).getPetInfo());
        PicassoClient.downloadImage(mContext,mAnimalData.get(position).getImgURL(), holder.imgURL);

        holder.progressBar.setVisibility(View.VISIBLE);
        holder.petInfo.setVisibility(View.VISIBLE);
        holder.imgURL.setVisibility(View.VISIBLE);
    }

    public void updateAnimals(List<AnimalType> animalType){
        this.mAnimalData = animalType;
        notifyDataSetChanged();
    }

    public static List<AnimalType> getAnimalList(){
        return mAnimalData;
    }

    //return size of dataset
    public int getItemCount(){
        return mAnimalData.size();
    }
}

但它没有显示加载图标。相反,它将显示一个空白屏幕,直到所有卡都被加载。

编辑:布局xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/entire_card"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_margin="8dp"
        android:transitionName="@string/transition_string"
        app:cardBackgroundColor="@color/colorPrimaryDark"
        app:cardUseCompatPadding="true">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ProgressBar
                android:id="@+id/progressbar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_centerHorizontal="true"
                android:paddingTop="120dp"/>


            <ImageView
                android:id="@+id/pet_image"
                android:layout_width="match_parent"
                android:layout_height="210dp"
                android:layout_alignParentTop="true"
                android:scaleType="centerCrop"
                android:src="@drawable/placeholder"
                android:visibility="gone" />

            <TextView
                android:id="@+id/pet_description"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/pet_image"
                android:ellipsize="end"
                android:maxLines="4"
                android:padding="8dp"
                android:textColor="@color/textColorPrimary"
                android:visibility="gone"/>

        </RelativeLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

0 个答案:

没有答案