监听器在自定义视图类中始终为null

时间:2017-02-28 05:27:11

标签: java android android-activity android-recyclerview android-view

我有这个结构:

  

活动 - > RecyclerView适配器 - >自定义视图

这是我的代码(为清楚起见,省略了一些部分)。

我的活动,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
    }
}

我的适配器,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

}

我的自定义视图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();
                }
            }
        });
    }

}

但由于某种原因,听众始终是null,因此onEventComplete()实际上从未被调用过。

为什么它总是为空?

1 个答案:

答案 0 :(得分:0)

我认为您的自定义视图应如下所示,我不确定。但你可以尝试如下。

AutoroutePart

<强>更新

将您的自定义视图创建为名为customview的xml文件,然后尝试。

R.layout.customview

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);
}

在适配器onCreateViewHolder(...)方法中执行以下操作

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