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:
public class ProductChoiceView extends LinearLayout {
private AmazonProductListAdapter customListAdapter;
private Context context;
private List<Product> mItems = new ArrayList<Product>();
public ProductChoiceView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public void setProductResults(List<Product> 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 < customListAdapter.getCount(); i++) {
View view = customListAdapter.getView(i, null, this);
parent.addView(view);
}
}
}
And my custom adapter:
public class AmazonProductListAdapter extends BaseAdapter implements ShoppingListener {
Context context;
int layoutId;
Holder holder;
public View view;
private List<Product> products;
public int currPosition = 0;
public AmazonProductListAdapter(Context context, int resource, List<Product> 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<Product> productSearchResults = gson.fromJson(value, new TypeToken<List<Product>>() {
}.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;
}
...... }
And I create this view with a simple method call from a class that extends fragment like so:
ProductChoiceView productsView;
productsView = (ProductChoiceView) getView().findViewById(R.id.amazon_products);
productsView.setVisibility(View.VISIBLE);
productsView.setProductResults(productSearchResults);
where "productSearchResults" is a list
I have this within a layout:
..........
<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" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.accuweather.android.ias.ProductChoiceView
android:id="@+id/amazon_products"
android:layout_width="80dp"
android:layout_height="match_parent"
android:visibility="gone" />
</HorizontalScrollView>
</LinearLayout>
How would I place my custom view inside a viewpager please?
You can use a ViewPager
and a ViewPagerAdapter
, which basically works the same way as an ArrayAdapter
. One example of this can be viewed here, however you will need to adapt it to your specific needs.