title: RecyclerView onClickListener and ViewType
body: <p>I have a RecyclerView which I want to attach an onClickListener too, however I get a null object error when clicking on the items. It would seem my onclicklistener isn't being attached correctly?</p>
  
  <p>There is also another problem, the correct viewholder doesn't seem to be returned?</p>
  
  <p>EDIT: Seems to be some sort of issue with the viewHolder and view variables, but not sure what....</p>
  
  <pre><code>        @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;
      }
  </code></pre>