1
votes

I have this structure:

Activity -> RecyclerView Adapter -> Custom View

Here is my code (with some parts left out for clarity).

My activity, MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private final String TAG = "MainActivity";

    private EditText textField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }

        textField = (EditText) findViewById(R.id.textField);

        // Adapter code
    }
}

My adapter, MyAdapter.java:

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

    private static final String TAG = "MyAdapter";

    private Context context;

    public MyAdapter(Context context) {
        this.context = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View v) {
            super(v);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);

        CustomView customView = new CustomView(context);

        customView.setCustomViewListener(new CustomView.CustomViewListener() {
            @Override
            public void onEventComplete() {
                Log.d(TAG, "EVENT COMPLETE");
            }
        });

        ViewHolder viewHolder = new ViewHolder(customView);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        //
    }

    // other methods

}

And my custom view, CustomView.java:

public class CustomView extends RelativeLayout {

    private final String TAG = "CustomView";

    private CustomViewListener mListener = null;

    private RelativeLayout mLayout;
    private ImageView mPicture;

    public CustomView(Context context) {
        super(context);
        init();
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public interface CustomViewListener {
        void onEventComplete();
    }

    public void setCustomViewListener(CustomViewListener listener) {
        this.mListener = listener;
    }

    private void init() {
        inflate(getContext(), R.layout.item_layout, this);

        this.mLayout = (RelativeLayout) findViewById(R.id.layout);
        this.mPicture = (ImageView) findViewById(R.id.picture);

        mPicture.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onEventComplete();
                }
            }
        });
    }

}

But for some reason, the listener is always null, so the onEventComplete() is never actually called.

Why is it always null?

1
give another try of mine - Noorul

1 Answers

0
votes

I think your custom view should be like below, I am not sure. but you can try like below.

private void init() {
View view=inflate(R.layout.item_layout, null);
this.mLayout = (RelativeLayout)view. findViewById(R.id.layout);
this.mPicture = (ImageView)view. findViewById(R.id.picture);
this.mPicture.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        if (mListener != null) {
             mListener.onEventComplete();
        }
      }
    });
  addView(view);
}

UPDATE

create your custom view as xml file named customview and give try.

R.layout.customview

<com.yourpackage.customview
   layout_height="match_parent"
   layout_width="match_parent"/>

And do following in your adapter onCreateViewHolder(...) method

View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.customview, parent, false)
 return new MyViewHolder(view);