Butterknife绑定按钮OnclickListener无法正常工作

时间:2017-06-18 18:26:29

标签: android onclicklistener android-viewholder butterknife

我已在Android应用中的片段中的视图中设置了一个onclick侦听器。

package store.snapcart.snapshopper.ui.fragment.main;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.yoctopus.cac.util.LogUtil;
import store.snapcart.snapshopper.R;

public class DashboardFragment extends Fragment {
    private String TAG = LogUtil.makeTag(DashboardFragment.class);
    private OnInteractionListener listener;
    private ViewHolder holder;
    public DashboardFragment() {
    }

    public static DashboardFragment newInstance() {
        DashboardFragment fragment = new DashboardFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_dashboard, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //we have to initialize the views inside the viewholder
        //to use the this listener, we have to implement it in the dashboard acticity
        holder = new ViewHolder(view, listener);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnInteractionListener) {
            listener = (OnInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }

    public interface OnInteractionListener {
        void onShiftClicked(Button button);

        void onTestClicked(Button button);
    }

    static class ViewHolder {
        @BindView(R.id.shopper_image)
        ImageView shopperImage;
        @BindView(R.id.shopper_name)
        TextView shopperName;
        @BindView(R.id.shift)
        Button shift;
        @BindView(R.id.dashboard_list)
        ListView dashboardList;
        @BindView(R.id.test)
        Button test;

        ViewHolder(View view, final OnInteractionListener listener) {
            ButterKnife.bind(this, view);

            LogUtil.e("ViewHolder", "setting up viewholder");
            //we'll set the onclick listener here
            test.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtil.e("Holder", "on click test");
                    listener.onTestClicked((Button) v);
                }
            });
            shift.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtil.e("Holder", "on click shift");
                    listener.onShiftClicked((Button) v);
                }
            });

            LogUtil.e("ViewHolder", "set up viewholder");
        }
    }
}

执行带有ViewHolder的日志语句但如果单击按钮(其中任何一个),onclick侦听器中的日志状态将不起作用。

包含这是必要的,我的构建工具是25.0.3,我在运行android 7.1.1的测试设备上运行此代码 请帮助我解决这个问题。谢谢

0 个答案:

没有答案