如何在视图发生变化后使视图无效?

时间:2016-01-06 02:16:23

标签: android horizontal-scrolling horizontalscrollview

首先请参阅下面的示例img。 对不起,我的声誉还不够,请点击下方查看gif,

Many programming languages have built-in methods for doing this

正如您所见,我在底部区域使用horizo​​ntalscrollview。当它变大时我无法将其滚动到边界。我无法弄清楚为什么,希望有人能帮助我解决这个问题。

public class Rebound extends HorizontalScrollView implements View.OnClickListener {

    private LinearLayout container;

    private HorizontalScrollViewAdapter adapter;

    private int childWidth, childHeight;

    private CurrentImageChangeListener listener;

    private OnItemClickListener onClickListener;

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        container = (LinearLayout) getChildAt(0);
    }

    @Override
    public void invalidate() {
        super.invalidate();
    }

    public void initDatas(HorizontalScrollViewAdapter adapter) {
        this.adapter = adapter;
        container = (LinearLayout) getChildAt(0);

        final View view = adapter.getView(0, null, container);
        container.addView(view);

        calculateChildView(view);
    }

    private void calculateChildView(View view) {
        if (childWidth == 0 && childHeight == 0) {
            int w = View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

            view.measure(w, h);

            childHeight = view.getMeasuredHeight();
            childWidth = view.getMeasuredWidth();
        }
        initChild();
    }

    private void initChild() {
        container = (LinearLayout) getChildAt(0);
        container.removeAllViews();
        for (int i = 0; i < adapter.getCount(); i++) {
            View view = adapter.getView(i, null, container);
            view.setOnClickListener(this);
            container.addView(view);
        }
    }

    @Override
    public void onClick(View v) {

    }

    public interface CurrentImageChangeListener {
        void onCurrentImgChanged(int position, View viewIndicator);
    }

    public interface OnItemClickListener {
        void onClick(View view, int pos);
    }

    public void setOnItemClickListener(OnItemClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setCurrentImageChangeListener(CurrentImageChangeListener listener) {
        this.listener = listener;
    }
}

0 个答案:

没有答案