2

寻找有关如何使用自定义视图类从 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 中?

4

1 回答 1

0

您可以使用 aViewPager和 a ViewPagerAdapter,它们的工作方式基本上与 a 相同ArrayAdapter。可以在此处查看其中的一个示例,但是您需要对其进行调整以适应您的特定需求。

于 2013-08-16T17:59:54.980 回答