在RecyclerView中更改视图的状态

时间:2017-08-07 04:01:52

标签: android android-recyclerview android-viewholder

目标:我想在RecyclerView中更改progressBar's可见性,以指示当用户使用addImage()添加图片时,图片正在加载。

问题: addImage应该会更改progressBar's种可见度。但是,调用addImage()时不会创建Viewholder,因此progressBar未定义。

MainActivity

    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
    recyclerView.setLayoutManager(gridLayoutManager);

    recyclerView.setAdapter(galleryAdapter);

    if (getArguments() != null) {
        if (getArguments().containsKey(PhotoFragment.IMAGE_URI)) {
            final Uri imageUri = Uri.parse(getArguments().getString(PhotoFragment.IMAGE_URI));
            galleryAdapter.addImage(imageUri);
            galleryAdapter.notifyDataSetChanged();
            getArguments().remove(PhotoFragment.IMAGE_URI);
        }
    }

RecyclerView(问题所在的位置)

public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.GalleryViewHolder> {


ArrayList<Uri> imageUriArrayList = new ArrayList<>();
File[] listFile;
ProgressBar progressBar;
private GalleryFragment mFragment;
private String airBnbDirectory = "/Airbnb";
SharedPreferences sharedPreferences;
SharedPreferences.Editor edit;

public GalleryAdapter(GalleryFragment fragment) {
    mFragment = fragment;
    loadSDCard();
}

public void addImage(final Uri imageUri) {
    imageUriArrayList.add(imageUri);

    //Save image in SD CARD(External Storage)
    new AsyncTask<Void, Void, Void>() {
        //
        String imageUriForLoadingSP;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.d("adapter", "OnPreExecute");
            progressBar.setVisibility(View.VISIBLE);
            //Cannot get progressBar because it's not created by the ViewHolder yet.
        }

        @Override
        protected Void doInBackground(Void... params) {
            imageUriForLoadingSP = saveInSDCard(imageUri);
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            Log.d("mattSaving", "onPostExecute");
            progressBar.setVisibility(View.GONE);

        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);


}




}

@Override
public GalleryAdapter.GalleryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.gallery_adapter_item, parent, false);
    return new GalleryAdapter.GalleryViewHolder(view);
}

@Override
public void onBindViewHolder(GalleryAdapter.GalleryViewHolder holder, int position) {
    holder.bindView(position);
}

@Override
public int getItemCount() {
    return imageUriArrayList.size();
}

public class GalleryViewHolder extends RecyclerView.ViewHolder {
    ImageView ivPhoto;

    public GalleryViewHolder(View itemView) {
        super(itemView);
        Log.d("adapter", "ViewHolder");
        ivPhoto = (ImageView) itemView.findViewById(R.id.ivPhoto);
        progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);

    }

    public void bindView(final int position) {
        //load images called by addImage()
        Glide.with(mFragment)
                .load(imageUriArrayList.get(position)) // Uri of the picture
                .into(ivPhoto);

    }

}

   }

1 个答案:

答案 0 :(得分:0)

您必须调用notifyItemChanged(position)来更新视图项。

public void addImage(final Uri imageUri) {
    imageUriArrayList.add(imageUri);

//Save image in SD CARD(External Storage)
new AsyncTask<Void, Void, Void>() {
    //
    String imageUriForLoadingSP;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.d("adapter", "OnPreExecute");
        progressBar.setVisibility(View.VISIBLE);
        //Cannot get progressBar because it's not created by the ViewHolder yet.
    }

    @Override
    protected Void doInBackground(Void... params) {
        imageUriForLoadingSP = saveInSDCard(imageUri);
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        Log.d("mattSaving", "onPostExecute");
        progressBar.setVisibility(View.GONE);

    }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);


}

你可以把这个notifyItemChanged(位置);在imageUriArrayList.add(imageUri)下;但你必须提供一个位置,或者你可以尝试在那里使用notifyDataSetChanged。不确定这将如何运作。