在scrollview的原始项目中动态添加视图,在scrollview中不能正常工作

时间:2016-06-24 10:39:53

标签: android listview scrollview

我有一个强制需要它的scrollview。在滚动视图中我有一个Listview。 listview的行项目有一个framlayout。我正在准备广告时将nativeexpreesview添加到fram布局。

现在的问题是,如果列表有10个项目,当广告没有加载时它的工作正常但是当加载广告然后它占用一些高度因为我将它添加到框架布局然后滚动视图仅滚动列表的高度在广告加载之前

但我希望滚动整个项目以及广告

我的活动' slayout低于

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/svStrategy"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_bg"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        android:orientation="vertical">


        <LinearLayout
            android:id="@+id/lytBanner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/list_selector"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:layout_gravity="center"

                android:text="@string/str_use_spells_best"
                android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
                android:textColor="@android:color/white" />

            <ImageView
                android:id="@+id/ivBanner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true" />
        </LinearLayout>



        <com.example.SummonersWar.classes.NestedListView
            android:id="@+id/lvStrategy"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:cacheColorHint="#00000000"
            android:divider="@android:color/transparent"
            android:dividerHeight="10dp"
            android:fadingEdge="none"
            android:focusable="false"
            android:scrollbars="none" />

    </LinearLayout>

</ScrollView>

Nestedlistview的Row_layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/lytStrategyRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/list_selector"
        android:orientation="horizontal"

        android:weightSum="2">

        <ImageView
            android:id="@+id/ivStrategyIcon"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.4"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/tvStrategyTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="5dp"
            android:layout_weight="0.6"
            android:ellipsize="end"
            android:maxLines="4"
            android:minLines="4"


            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
            android:textColor="@android:color/white" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/rlads"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lytStrategyRow"
        android:orientation="vertical"
        android:visibility="gone">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:background="@android:color/transparent" />

        <FrameLayout
            android:id="@+id/fr_adview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></FrameLayout>
    </LinearLayout>
</RelativeLayout>

我在加载广告时以编程方式将Nativeexpressview添加到framlayout。

我在下面的嵌套列表视图

public class NestedListView extends ListView implements OnScrollListener // OnTouchListener
{

    private int listViewTouchAction;
    private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 100;

    public NestedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        listViewTouchAction = -1;
        setOnScrollListener(this);
        // setOnTouchListener(this);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        if (getAdapter() != null
                && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, -1);
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int newHeight = 0;
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            ListAdapter listAdapter = getAdapter();
            if (listAdapter != null && !listAdapter.isEmpty()) {
                int listPosition = 0;
                for (listPosition = 0; listPosition < listAdapter.getCount()
                        && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                    View listItem = listAdapter.getView(listPosition, null,
                            this);
                    // now it will not throw a NPE if listItem is a ViewGroup
                    // instance
                    if (listItem instanceof ViewGroup) {
                        listItem.setLayoutParams(new LayoutParams(
                                LayoutParams.WRAP_CONTENT,
                                LayoutParams.WRAP_CONTENT));
                    }
                    listItem.measure(widthMeasureSpec, heightMeasureSpec);
                    newHeight += listItem.getMeasuredHeight();

                }
                newHeight += getDividerHeight() * listPosition;
                newHeight += getPaddingBottom() + getPaddingTop();
            }
            if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
                if (newHeight > heightSize) {
                    newHeight = heightSize;
                }
            }
        } else {
            newHeight = getMeasuredHeight();
        }
        setMeasuredDimension(getMeasuredWidth(), newHeight);

    }}

所以我的问题是scrollview只在加载广告时滚动列表的高度。所以请帮助我现在应该如何滚动整个广告列表?

1 个答案:

答案 0 :(得分:0)

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.widget.ScrollView;

public class VerticalScrollview extends ScrollView {

public VerticalScrollview(Context context) {
    super(context);
}

public VerticalScrollview(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public VerticalScrollview(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            super.onTouchEvent(ev);
            break;

        case MotionEvent.ACTION_MOVE:
            return false; // redirect MotionEvents to ourself

        case MotionEvent.ACTION_CANCEL:
            super.onTouchEvent(ev);
            break;

        case MotionEvent.ACTION_UP:
            return false;

        default:
            break;
    }

    return false;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    super.onTouchEvent(ev);
    return true;
}

}