title: Using Custom view class inside ViewPager
body: <p>Looking for any suggestions on how I would migrate from a HorizontalScrollView to a ViewPager using a custom view class.  Here is my custom view class:</p>
  
  <pre><code>public class ProductChoiceView extends LinearLayout {
  private AmazonProductListAdapter customListAdapter;
  private Context context;
  private List&lt;Product&gt; mItems = new ArrayList&lt;Product&gt;();
  
  public ProductChoiceView(Context context, AttributeSet attrs) {
      super(context, attrs);
      this.context = context;
  
  }
  
  public void setProductResults(List&lt;Product&gt; productSearchResults) {
      this.mItems = productSearchResults;
      init();
  }
  
  private void init() {
      customListAdapter = new AmazonProductListAdapter(context, R.layout.amazon_list_item, mItems);
      this.setAdapter(getContext(), customListAdapter);
  }
  
  public void setAdapter(Context context, AmazonProductListAdapter adapter) {
      populateViewWithAdapter(adapter);
  }
  
  private void populateViewWithAdapter(AmazonProductListAdapter customListAdapter) {
      if (customListAdapter == null) {
          return;
      }
  
      ViewGroup parent = this;
  
      parent.removeAllViews();
  
      for (int i = 0; i &lt; customListAdapter.getCount(); i++) {         
          View view = customListAdapter.getView(i, null, this);
          parent.addView(view);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And my custom adapter:</p>
  
  <pre><code>public class AmazonProductListAdapter extends BaseAdapter implements ShoppingListener {
  
  
  Context context;
  int layoutId;
  Holder holder;
  public View view;
  private List&lt;Product&gt; products;
  public int currPosition = 0;
  
  public AmazonProductListAdapter(Context context, int resource, List&lt;Product&gt; products) {
      super();
      this.context = context;
      this.products = products;
      layoutId = resource;
  
  }
  
  public int getCurrentPosition() {
      return currPosition;
  }
  
  @Override
  public long getItemId(int position) {
      // TODO Auto-generated method stub
      return 0;
  }
  
  @Override
  public int getCount() {
      return products.size();
  }
  
  @Override
  public String getItem(int position) {
      return products.get(position).getProductId();
  }
  
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
  
      if (convertView == null) {
  
          convertView = (LinearLayout) View.inflate(context, layoutId, null);
  
          holder = new Holder();
  
          holder.product = (ImageView) convertView.findViewById(R.id.product_img);
          holder.description = (TextView) convertView.findViewById(R.id.description);
          holder.price = (TextView) convertView.findViewById(R.id.price);
  
      } else {
          view = convertView;
          holder = (Holder) convertView.getTag();
      }
      ImageLoader.getInstance().displayImage(products.get(position).getImage().getUrl(), holder.product);
      holder.description.setText(products.get(position).getDescription());
      holder.price.setText(products.get(position).getPrice().toString());
      convertView.setTag(Integer.valueOf(position));
      convertView.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              Integer realPosition = (Integer) v.getTag();
              SharedPreferences prefs = context.getSharedPreferences(Constantsias.Preferences.SEARCH_RESPONSE,
                                                                     Context.MODE_PRIVATE);
              String value = prefs.getString(Constantsias.Preferences.SEARCH_RESPONSE, null);
              Gson gson = new Gson();
              List&lt;Product&gt; productSearchResults = gson.fromJson(value, new TypeToken&lt;List&lt;Product&gt;&gt;() {
              }.getType());
              final PurchaseRequest purchaseRequest = new PurchaseRequest(productSearchResults.get(realPosition)
                                                                                              .getProductId(),
                                                                          v,
                                                                          true);
              purchaseRequest.setPurchaseExperience(PurchaseExperience.IN_APP);
              purchaseProduct(purchaseRequest);
  
          }
      });
      return convertView;
  }
  
  private class Holder {
      public ImageView product;
      public TextView description;
      public TextView price;
  
  }
  </code></pre>
  
  <p>......
  }</p>
  
  <p>And I create this view with a simple method call from a class that extends fragment like so:</p>
  
  <pre><code>ProductChoiceView productsView;
          productsView = (ProductChoiceView) getView().findViewById(R.id.amazon_products);
          productsView.setVisibility(View.VISIBLE);
          productsView.setProductResults(productSearchResults);
  </code></pre>
  
  <p>where "productSearchResults" is a list</p>
  
  <p>I have this within a layout:</p>
  
  <blockquote>
    <p></p>
  </blockquote>
  
  <p>..........</p>
  
  <pre><code>    &lt;TextView
          android:id="@+id/update_time"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="right"
          android:layout_marginRight="18dp"
          android:layout_marginTop="8dp"
          android:textColor="@color/primary_text"
          android:textSize="12dp" /&gt;
  
      &lt;HorizontalScrollView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
  
          &lt;com.accuweather.android.ias.ProductChoiceView
              android:id="@+id/amazon_products"
              android:layout_width="80dp"
              android:layout_height="match_parent"
              android:visibility="gone" /&gt;
      &lt;/HorizontalScrollView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>How would I  place my custom view inside a viewpager please?</p>