0

三星进行了定制ListView,使其始终能够过度滚动,而忽略setOverScrollMode(). 这对我来说很好,直到现在我都有与之相关的布局问题。

我需要有一个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 的大小没有改变。这只发生在三星设备上。到底发生了什么?我该如何解决这个问题?我认为尝试禁用过度滚动不是一种选择,因为三星自己编写了ListView.

感谢您阅读这篇长文。

4

1 回答 1

0

您对三星Overscroll在他们的设备上进行定制是正确的。我在尝试编写OverScrollScrollView覆盖该onOverScrolled()方法的方法时遇到了类似的问题。

我不知道它是否会起作用,但是下载SOURCEListView并在您的应用程序中使用它很可能会解决您的问题。

祝你好运!

于 2013-01-04T04:27:29.807 回答