title: Android RecyclerView: Change layout file LIST to GRID onOptionItemSelected
body: <p>I am developing an Android Application for Online Shopping. I have created following view for List of Products using <code>RecyclerView</code>, in that i want to change view <strong>on selecting option menu item</strong>:</p>
  
  <p>I have created following <strong>adapter</strong> named <code>ProductAdapter</code>, in that I have implemented code for changing layout in <code>onCreateViewHolder</code> for selecting layout file based on boolean value.</p>
  
  <p><strong>Code of Adapter <code>ProductAdapter</code></strong>:</p>
  
  <pre><code>    /***
       * ADAPTER for Product to binding rows in List
       */
      private class ProductAdapter extends RecyclerView.Adapter&lt;ProductAdapter.ProductRowHolder&gt; {
  
          private List&lt;Product&gt; productList;
  
          private Context mContext;
  
          public ProductAdapter(Context context, List&lt;Product&gt; feedItemList) {
              this.productList = feedItemList;
              this.mContext = context;
          }
  
          @Override
          public ProductRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
              View v = LayoutInflater.from(viewGroup.getContext()).inflate(isProductViewAsList ? R.layout.product_row_layout_list : R.layout.product_row_layout_grid, null);
              ProductRowHolder mh = new ProductRowHolder(v);
              return mh;
          }
  
          @Override
          public void onBindViewHolder(ProductRowHolder  productRowHolder, int i) {
              Product prodItem = productList.get(i);
  
  //            Picasso.with(mContext).load(feedItem.getName())
  //                    .error(R.drawable.ic_launcher)
  //                    .placeholder(R.drawable.ic_launcher)
  //                    .into(productRowHolder.thumbnail);
  
  
              double price = prodItem.getPrice();
              double discount = prodItem.getDiscount();
              double discountedPrice = price - (price * discount / 100);
  
              String code = "";
              if(prodItem.getCode() != null)
                  code = "[" + prodItem.getCode() + "] ";
  
              productRowHolder.prodIsNewView.setVisibility(prodItem.getIsNew() == 1 ? View.VISIBLE : View.INVISIBLE);
              productRowHolder.prodNameView.setText(code + prodItem.getName());
              productRowHolder.prodOriginalRateView.setText("Rs." + new BigDecimal(price).setScale(2,RoundingMode.DOWN));
              productRowHolder.prodDiscView.setText("" + new BigDecimal(discount).setScale(2,RoundingMode.DOWN) + "% off");
              productRowHolder.prodDiscRateView.setText("Rs." + new BigDecimal(discountedPrice).setScale(2,RoundingMode.DOWN));
  
              productRowHolder.prodOriginalRateView.setPaintFlags(productRowHolder.prodOriginalRateView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
          }
  
          @Override
          public int getItemCount() {
              return (null != productList ? productList.size() : 0);
          }
  
          public class ProductRowHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
              //Declaration of Views
  
              public ProductRowHolder(View view) {
                  super(view);
  
                  view.setOnClickListener(this);
  
                  //Find Views
              }
  
              @Override
              public void onClick(View view) {
                 //Onclick of row
              }
          }
      }
  </code></pre>
  
  <p>After that i have done code for Changing <code>RecyclerView</code> layout from <code>List</code> to <code>Grid</code> and Vice Versa in <code>onOptionsItemSelected</code>, here i am calling <code>mAdapter.notifyDataSetChanged();</code> so it will call adapter again and change value.</p>
  
  <p><strong>onOptionsItemSelected</strong>:</p>
  
  <pre><code>    @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          switch (id) {
              case R.id.action_settings:
                  return true;
              case android.R.id.home:
                  finish();
                  break;
              case R.id.product_show_as_view:
                  isProductViewAsList = !isProductViewAsList;
                  supportInvalidateOptionsMenu();
                  mRecyclerView.setLayoutManager(isProductViewAsList ? new LinearLayoutManager(this) : new GridLayoutManager(this, 2));
                  mAdapter.notifyDataSetChanged();
                  break;
          }
  
          return super.onOptionsItemSelected(item);
      }
  </code></pre>
  
  <p>I got little bit success like:</p>
  
  <p>Image of <code>Grid</code> layout:</p>
  
  <p><img src="https://i.stack.imgur.com/5NHCf.png" alt="enter image description here"></p>
  
  <p>Image of <code>List</code> layout:</p>
  
  <p><img src="https://i.stack.imgur.com/g9LMH.png" alt="enter image description here"></p>
  
  <p><strong>BUT NOW WHEN I SCROLL and then CHANGING VIEW is Displaying</strong> like:</p>
  
  <p><code>Grid</code> layout:</p>
  
  <p><img src="https://i.stack.imgur.com/XkqvO.png" alt="enter image description here"></p>
  
  <p><code>List</code> layout:</p>
  
  <p><img src="https://i.stack.imgur.com/TIUWj.png" alt="enter image description here"></p>
  
  <p><strong>I dont know why it happens after scrolling. Is there any other way to change view like this.</strong></p>
  
  <blockquote>
    <p>Today i just saw that this problem is because of <code>ImageView</code>, without it  working perfectly.</p>
  </blockquote>
  
  <p>Help please, You help will be appreciated.</p>