RecyclerView onClickListener和ViewType

时间:2016-11-12 04:06:47

标签: android

我有一个RecyclerView,我想附加一个onClickListener,但是当点击这些项目时我得到一个空对象错误。看来我的onclicklistener没有正确连接?

还有另一个问题,正确的观看者似乎没有被退回?

编辑:看来是viewHolder和视图变量的某种问题,但不确定是什么......

        @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        // Variable to hold the view and view holder.
        View view = null;
        RecyclerView.ViewHolder viewHolder = null;

        // Choose which field type to display.
        switch (viewType) {
            case TEMPLATE_DETAILS_FIELD:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.templates_add_template_details_field, viewGroup, false);
                viewHolder = new TemplateDetailsField(view);
            case SECTION_FIELD:
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.templates_add_section_field, viewGroup, false);
                viewHolder = new SectionField(view);
            case HEADING_FIELD:
                // Create the new view.
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.templates_add_heading_field, viewGroup, false);

                // Create a new heading field.
                HeadingField headingField = new HeadingField(view);

                // Attach the edittext listener to it.
                headingField.getHeading().addTextChangedListener(new TitleTextWatcher(headingField.getTitle(), headingField.getHeading()));
                viewHolder = headingField;
            default:
                // Do nothing here.
                break;
        }

        //Create a temp view holder for the onclick listener.
        final RecyclerView.ViewHolder tempViewHolder = viewHolder;

        // Set the onclick listener to expand and contract the view.
        view.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                FrameLayout expandedView = (FrameLayout) view.findViewById(R.id.expanded_view);
                // Check if the child view is visible or not.
                if (expandedView.getVisibility() == View.VISIBLE) {

                    // View is visible, hide the view.
                    expandedView.setVisibility(View.GONE);

                    // Add to expanded state array list.
                    mRecyclerViewExpandedState.set(tempViewHolder.getAdapterPosition(), false);
                } else {

                    // View is not visible, show the view.
                    expandedView.setVisibility(View.VISIBLE);

                    // Add to expanded state array list.
                    mRecyclerViewExpandedState.set(tempViewHolder.getAdapterPosition(), true);
                }
            }
        });

        return viewHolder;
    }

1 个答案:

答案 0 :(得分:0)

将此用作点击侦听器,

import android.content.Context;  
import android.support.v7.widget.RecyclerView;  
import android.view.GestureDetector;  
import android.view.MotionEvent;  
import android.view.View;

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {  
private OnItemClickListener mListener;

public interface OnItemClickListener {
    public void onItemClick(View view, int position);
}

GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
    mListener = listener;
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
  }
}

在代码中使用它非常简单。只需将此类的项目触摸侦听器添加到RecyclerView

即可
recyclerView.addOnItemTouchListener(  
  new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
  @Override public void onItemClick(View view, int position) {
    // TODO Handle item click
  }
})
);