2013-08-16 34 views
2

尋找關於如何使用自定義視圖類從Horizo​​ntalScrollView遷移到ViewPager的任何建議。這裏是我的自定義視圖類:使用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中使用我的自定義視圖嗎?

回答

0

您可以使用ViewPagerViewPagerAdapter,基本上與ArrayAdapter的工作方式基本相同。其中一個例子可以查看here,但是您需要根據您的具體需求進行調整。

+0

我想保留我的自定義視圖,所以我可以在我的應用程序的其他部分重用它。一直在尋找,看看我是否可以在viewpager中有我的自定義視圖...沒有答案的運氣,猜我會嘗試一下並玩。 – taraloca

+0

我最終使用了一個擴展了FixedFragmentStatePagerAdapter的適配器,然後將我的視圖填充到擴展片段的類中(然後實例化適配器) – taraloca

相關問題