在ViewPager中使用自定义视图类

时间:2013-08-16 17:38:33

标签: android android-viewpager horizontalscrollview

寻找有关如何使用自定义视图类从Horizo​​ntalScrollView迁移到ViewPager的任何建议。这是我的自定义视图类:

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);
    }
}

}

我的自定义适配器:

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;

}

...... }

我通过一个类似于扩展片段的类的简单方法调用来创建这个视图:

ProductChoiceView productsView;
        productsView = (ProductChoiceView) getView().findViewById(R.id.amazon_products);
        productsView.setVisibility(View.VISIBLE);
        productsView.setProductResults(productSearchResults);

其中“productSearchResults”是一个列表

我在布局中有这个:

  

..........

    <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>

我如何将自定义视图放在viewpager中?

1 个答案:

答案 0 :(得分:0)

您可以使用ViewPagerViewPagerAdapter,其基本上与ArrayAdapter的工作方式相同。可以查看here的一个示例,但是您需要根据自己的特定需求进行调整。