简体   繁体   中英

Unable to access Inner Interface of class

I have an adapter class for recyclerview:

public abstract class RecyclerViewArrayAdapter<T, VH extends RecyclerView.ViewHolder>
        extends RecyclerView.Adapter<VH> {

        ...
  }

I am creating an object like this :

  new RecyclerViewArrayAdapter<String, ListImageItemViewHolder>(emailAddresses) {
            @Override
            public ListImageItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
                return new ListImageItemViewHolder(v);
            }

            @Override
            public void onBindViewHolder(ListImageItemViewHolder holder, int position) {


                holder.setClickListener(new ListImageItemViewHolder.ClickListener() {
                    @Override
                    public void onClick(View v, int position, boolean isLongClick) {


                    }
                });
            }
        };

The ListImageItemViewHolder definition looks like this:

public class ListImageItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

    private ClickListener clickListener;


    public ListImageItemViewHolder(View itemView) {
        super(itemView);

        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
    }

    public interface ClickListener {
        void onClick(View v, int position, boolean isLongClick);
    }


   public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }
  }

Everything looks fine, no red mark, however as soon as I compile the following line gives me an error;

Error:(147, 68) error: cannot find symbol class ClickListener

Like 147 is :

holder.setClickListener(new ListImageItemViewHolder.ClickListener() {
                    @Override
                    public void onClick(View v, int position, boolean isLongClick) {


                    }
                });

Question: Why it is not able access ClickListener .. .

It driving me crazy ?

What wrong am I doing here...

UPDATES:

I found that the issue is with minify.

buildTypes { release { minifyEnabled true } }

The RecyclerViewArrayAdapter and ListImageItemViewHolder is the part of a module called "Common". I refer this module in my project.

Now when I have a common gradle set to "minify" to false it works. The moment I change it to minify true, it comes back.

It looks like a compilation issue. But I cannot live with this error nor I can set minify to false for the production build.

Can someone please help me with the work around ?

在此输入图像描述

Try this,

new ListImageItemViewHolder().clickListener = new ClickListener(){
                @Override
                public void onClick(View v, int position, boolean isLongClick) {


                }
            });

This line new ListImageItemViewHolder().clickListener refers to ClickListener clickListener;

Then you instantiate it..

Declare clickListener using access specifier public

Ok there is no solution as such but a work around. To me, it is nothing wrong with the code. There is no red line whatsoever but it cannot find when I compile. However, when I set minify to false then it works (even when the minify setting is set for release version and I am running as debug).

The solution is to separate out ClickListener interface away from ListImageItemViewHolder class; thus no to nested interface.

 public interface ClickListener {
        void onClick(View v, int position, boolean isLongClick);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM