三星已定製ListView
,以便它一直能夠過卷,忽略像setOverScrollMode()
這樣的功能。對我來說這很好,直到現在我有與之相關的佈局問題。三星的ListView導致佈局問題,同時存在WebViews時滾動
我需要有一個ListView
顯示正常的項目,並在某些預先定義的行號插入一些行內的廣告,使用WebView
。爲了能夠重新使用代碼,我創建了一個基礎適配器,所有其他適配器擴展它。
InRowAdAdapter.java
public class InRowAdAdapter extends BaseAdapter {
private final Context mContext;
private SparseArray<WebView> webViews;
private OnInRowAdItemClickListener mListener = null;
public void setOnInRowAdItemClickListener(
OnInRowAdItemClickListener listener) {
mListener = listener;
}
public interface OnInRowAdItemClickListener {
public void onInRowAdItemClick(View v);
}
public InRowAdAdapter(Context context) {
mContext = context;
webViews = new SparseArray<WebView>();
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int adCount = webViews.size();
for (int i = 0; i < adCount; i++) {
((ViewGroup) convertView)
.removeView(webViews.get(webViews.keyAt(i)));
}
if (AdManager.isAdRow(position)) { // If this row is pre-defined to show ads
if (webViews.get(position) == null) {
webViews.put(position, AdManager.addAd(mContext,
(ViewGroup) convertView, MyApp.URL_ROW_AD));
} else {
try {
AdManager.addAd((ViewGroup) convertView,
webViews.get(position));
} catch (Exception e) {
e.printStackTrace();
}
}
webViews.get(position).setVisibility(View.VISIBLE);
convertView.setClickable(true);
// To fix that onItemClick() is not working
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null)
mListener.onInRowAdItemClick(v);
}
});
}
return convertView;
}
protected Context getContext() {
return mContext;
}
}
AdManager.java
public class AdManager {
/**
* Create a new WebView and add it to the supplied ViewGroup.
*
* @return The created WebView
**/
public static WebView addAd(final Context context,
final ViewGroup container, final String adUrl) {
final WebView webView = new WebView(context);
webView.setVisibility(View.GONE);
webView.loadUrl(adUrl);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
// webView.setInitialScale((Math.round(adHeight/BOTTOM_AD_MIN_HEIGHT
// * 100)) + 2);
webView.setClickable(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent urlIntent = new Intent(
android.content.Intent.ACTION_VIEW, Uri.parse(url));
urlIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(urlIntent);
return true;
}
});
addAd(container, webView);
return webView;
}
/**
* Add the webView to the bottom of the supplied ViewGroup. If a
* LinearLayout is passed to this function, please make sure that its
* orientation is vertical.
**/
public static void addAd(final ViewGroup container, final WebView webView) {
if (container instanceof RelativeLayout) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.BELOW,
container.getChildAt(container.getChildCount() - 1).getId());
container.addView(webView, params);
} else if (container instanceof LinearLayout) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
container.addView(webView, params);
}
}
}
這是我如何使用它: MyAdapter.java
public class MyAdapter extends InRowAdAdapter {
private final List<Product> products;
public static class ViewHolder {
public TextView vBrand;
public TextView vProduct;
public SmartImageView vImage;
public TextView vDescription;
public String id;
}
public MyAdapter(Context context, List<Product> products) {
super(context);
this.products = products;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(
R.layout.adapter_product_new_arrival_list_item, null);
holder = new ViewHolder();
holder.vBrand = (TextView) convertView
.findViewById(R.id.brand_name);
holder.vProduct = (TextView) convertView
.findViewById(R.id.product_name);
holder.vImage = (SmartImageView) convertView
.findViewById(R.id.product_image);
holder.vDescription = (TextView) convertView
.findViewById(R.id.description);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.vBrand.setText(products.get(position).getBrandName());
holder.vProduct.setText(products.get(position).getProductName());
holder.vImage.setImageUrl(products.get(position).getImageUrl());
holder.vDescription.setText(products.get(position).getDescription());
holder.id = products.get(position).getProductId();
// Let InRowAdAdapter handle the ads
return super.getView(position, convertView, parent);
}
@Override
public int getCount() {
return products.size();
}
@Override
public Object getItem(int position) {
return products.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
現在的問題是,通常它看起來像這樣:
但是,當過滾動:
所有行正常移動,但對於一些奇怪的原因,我的WebView的內容是不正在滾動。從截圖中可以看出,WebView的大小沒有改變。這隻發生在三星設備上。 究竟發生了什麼?我該如何解決這個問題?我認爲試圖禁用overscroll不是一種選擇,因爲三星寫了他們自己的ListView
。
感謝您閱讀這篇較長的文章。