0

I have a recyclerview with each row containing an icon. When the image is clicked, I want the image to be gone, and a progress bar to appear. The way I'm doing it, the problem is, the image for all rows disappears, and a progressbar for all rows appear. Here's the relevant code:

@Override
    public void onBindViewHolder(SubmissionViewHolder holder, int position) {
        switch(value){
            case 0:
                holder.smoothProgressBar.setVisibility(View.VISIBLE);
                holder.downloadIcon.setVisibility(View.GONE);
                break;
            case 4:
                holder.smoothProgressBar.setVisibility(View.INVISIBLE);
                holder.downloadIcon.setVisibility(View.VISIBLE);
                break;
            case 8:
                holder.smoothProgressBar.setVisibility(View.GONE);
                holder.downloadIcon.setVisibility(View.VISIBLE);
                break;
            default:
                holder.smoothProgressBar.setVisibility(View.GONE);
                holder.downloadIcon.setVisibility(View.VISIBLE);
        }
    }


    public void setDownloadBar(int value, int position){
        this.value = value;
        notifyItemChanged(position);
    }
  • What's "value" in your code? – Mike Nov 26 '15 at 20:11
  • It's the value for visibility. That is; GONE is 8, INVISIBLE is 4, and VISIBLE is 0. –  Nov 26 '15 at 20:23
  • There is absolutely no need for that. I think you don't understand the the way the adapter lifecycle methods work. Did you research the ViewHolder pattern, ListView, it's adapter's functionality? – Mike Nov 26 '15 at 20:28
  • No need for what? Could you be more specific about what I'm doing wrong? Those values are arguments to `setVisibility()` They've got nothing to do with the adapter or recyclerview itself. They modify the row layout. –  Nov 26 '15 at 20:31

1 Answers1

1

Based on the following links you can build up your solution

https://gist.github.com/grantland/cd70814fe4ac369e3e92

Get clicked item and its position in RecyclerView

http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/

So basically you need to check if the certain item of the recyclerview is onclicked. If yes just flip the visibility of the progressbar and the image.

You have a recyclerview similar to this one, let's assume on the activity_main layout:

<android.support.v7.widget.RecyclerView
    android:id="@+id/myRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

</android.support.v7.widget.RecyclerView>

in MainActivity's onCreate add:

    MainAdapter mainAdapter = new MainAdapter(new String[] {"apple", "banana", "coconut"});
    RecyclerView myRecyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    myRecyclerView.setLayoutManager(layoutManager);
    myRecyclerView.setAdapter(mainAdapter);

create a row_recycler_list under the layout:

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

    <ProgressBar
        android:id="@+id/myProgressBar"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:visibility="gone"
        />

    <ImageView
        android:id="@+id/myImage"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/myText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

your viewholder implements the View.OnClickListener like this one:

package com.example.jana.recyclerimage;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

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

    private String[] dataSet;

    public MainAdapter(String[] dataSet) {
        this.dataSet = dataSet;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater
            .from(parent.getContext())
            .inflate(R.layout.row_recycler_list, parent, false);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.textView.setText(dataSet[position]);
    }

    @Override
    public int getItemCount() {
        return dataSet.length;
    }

    public class ViewHolder
            extends RecyclerView.ViewHolder
            implements View.OnClickListener {

        public TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            this.textView = (TextView)itemView.findViewById(R.id.myText);
        }

        @Override
        public void onClick(View v) {
            ImageView imageView = (ImageView) v.findViewById(R.id.myImage);
            ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.myProgressBar);

            imageView.setVisibility(View.GONE);
            progressBar.setVisibility(View.VISIBLE);
        }
    }
}

and basically you're done. Of course you can extend and/or polish it according to your needs.

Community
  • 1
  • 1