android – 使用Pull来刷新ViewGager中的gridview在ActivityGroup中的Activity中导致ClassCastException

android – 使用Pull来刷新ViewGager中的gridview在ActivityGroup中的Activity中导致ClassCastException,第1张

概述我试图用ViewPager使用PullToRefresh GridView,这会导致ClassCastException的崩溃.这是例外. FATAL EXCEPTION: main java.lang.ClassCastException: android.support.v4.view.ViewPager$LayoutParams at android.widget.LinearLayout 我试图用VIEwPager使用PullToRefresh GrIDVIEw,这会导致ClassCastException的崩溃.这是例外.
FATAL EXCEPTION: main java.lang.classCastException: androID.support.v4.vIEw.VIEwPager$LayoutParams at androID.Widget.linearLayout.measureVertical(linearLayout.java:360) at androID.Widget.linearLayout.onMeasure(linearLayout.java:309) at androID.vIEw.VIEw.measure(VIEw.java:8313) at androID.Widget.@R_403_4614@Layout.measureChild(@R_403_4614@Layout.java:566) at androID.Widget.@R_403_4614@Layout.onMeasure(@R_403_4614@Layout.java:381) at androID.vIEw.VIEw.measure(VIEw.java:8313) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:3138) at androID.Widget.FrameLayout.onMeasure(FrameLayout.java:250) at androID.vIEw.VIEw.measure(VIEw.java:8313) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:3138) at androID.Widget.FrameLayout.onMeasure(FrameLayout.java:250) at androID.vIEw.VIEw.measure(VIEw.java:8313) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:3138) at androID.Widget.FrameLayout.onMeasure(FrameLayout.java:250) at androID.vIEw.VIEw.measure(VIEw.java:8313) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:3138) at androID.Widget.FrameLayout.onMeasure(FrameLayout.java:250) at androID.vIEw.VIEw.measure(VIEw.java:8313) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:3138) at androID.Widget.FrameLayout.onMeasure(FrameLayout.java:250) at androID.vIEw.VIEw.measure(VIEw.java:8313) at androID.Widget.@R_403_4614@Layout.measureChildHorizontal(@R_403_4614@Layout.java:581) at androID.Widget.@R_403_4614@Layout.onMeasure(@R_403_4614@Layout.java:365) at androID.vIEw.VIEw.measure(VIEw.java:8313) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:3138) at androID.Widget.FrameLayout.onMeasure(FrameLayout.java:250) at androID.vIEw.VIEw.measure(VIEw.java:8313) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:3138) at androID.Widget.linearLayout.measureChildBeforeLayout(linearLayout.java:1017) at androID.Widget.linearLayout.measureVertical(linearLayout.java:386) at androID.Widget.linearLayout.onMeasure(linearLayout.java:309) at androID.vIEw.VIEw.measure(VIEw.java:8313) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:3138) at androID.Widget.FrameLayout.onMeasure(FrameLayout.java:250) at androID.vIEw.VIEw.measure(VIEw.java:8313) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:3138) at androID.Widget.FrameLayout.onMeasure(FrameLayout.java:250) at androID.vIEw.VIEw.measure(VIEw.java:8313) at androID.vIEw.VIEwRoot.performTraversals(VIEwRoot.java:845) at androID.vIEw.VIEwRoot.handleMessage(VIEwRoot.java:1865) at androID.os.Handler.dispatchMessage(Handler.java:99) at androID.os.Looper.loop(Looper.java:130) at androID.app.ActivityThread.main(ActivityThread.java:3687) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:625) at dalvik.system.NativeStart.main(Native Method)

这是它被实现的代码段.

PulL_TO_REFRESH = (PullToRefreshGrIDVIEw)findVIEwByID(R.ID.groupList);mGroupList = PulL_TO_REFRESH.getAdapterVIEw();Vector<VIEw> pages = new Vector<VIEw>();        pages.add(mGroupList);        VIEwPager vp = (VIEwPager)findVIEwByID(R.ID.myfivepanelpager);        CustomPagerAdapter adapter = new CustomPagerAdapter(this,pages);        vp.setAdapter(adapter);        // Set a Listener to be invoked when the List should be refreshed.        PulL_TO_REFRESH.setonRefreshListener(new OnRefreshListener() {            @OverrIDe            public voID onRefresh() {                // Todo auto-generated method stub            }        });

这是来自PullToRefresh库的代码片段,可能是原因.

package com.syncusup.pulltorefresh.library.my;import androID.content.Context;import androID.os.Handler;import androID.util.AttributeSet;import androID.vIEw.LayoutInflater;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OntouchListener;import androID.vIEw.VIEwGroup;import androID.vIEw.animation.AccelerateDecelerateInterpolator;import androID.vIEw.animation.Animation;import androID.vIEw.animation.Interpolator;import androID.vIEw.animation.linearInterpolator;import androID.vIEw.animation.RotateAnimation;import androID.Widget.AdapterVIEw;import androID.Widget.ImageVIEw;import androID.Widget.linearLayout;import androID.Widget.listadapter;import androID.Widget.Progressbar;import androID.Widget.TextVIEw;import com.syncusup.R;public abstract class PullToRefreshBase<T extends AdapterVIEw<listadapter>> extends linearLayout implements        OntouchListener {    private final class SmoothScrollRunnable implements Runnable {        static final int ANIMATION_DURATION_MS = 190;        static final int ANIMATION_FPS = 1000 / 60;        private final Interpolator interpolator;        private final int scrollToY;        private final int scrollFromY;        private final Handler handler;        private boolean continueRunning = true;        private long startTime = -1;        private int currentY = -1;        public SmoothScrollRunnable(Handler handler,int fromY,int toY) {            this.handler = handler;            this.scrollFromY = fromY;            this.scrollToY = toY;            this.interpolator = new AccelerateDecelerateInterpolator();        }        @OverrIDe        public voID run() {            /**             * Only set startTime if this is the first time we're starting,else             * actually calculate the Y delta             */            if (startTime == -1) {                startTime = System.currentTimeMillis();            } else {                /**                 * We do do all calculations in long to reduce software float                 * calculations. We use 1000 as it gives us good accuracy and                 * small rounding errors                 */                long normalizedTime = (1000 * (System.currentTimeMillis() - startTime)) / ANIMATION_DURATION_MS;                normalizedTime = Math.max(Math.min(normalizedTime,1000),0);                final int deltaY = Math.round((scrollFromY - scrollToY)                        * interpolator.getInterpolation(normalizedTime / 1000f));                this.currentY = scrollFromY - deltaY;                setheaderScroll(currentY);            }            // If we're not at the target Y,keep going...            if (continueRunning && scrollToY != currentY) {                handler.postDelayed(this,ANIMATION_FPS);            }        }        public voID stop() {            this.continueRunning = false;            this.handler.removeCallbacks(this);        }    };    // ===========================================================    // Constants    // ===========================================================    static final int PulL_TO_REFRESH = 0;    static final int RELEASE_TO_REFRESH = PulL_TO_REFRESH + 1;    static final int REFRESHING = RELEASE_TO_REFRESH + 1;    static final int EVENT_COUNT = 3;    // ===========================================================    // FIElds    // ===========================================================    private int state = PulL_TO_REFRESH;    private T adapterVIEw;    private boolean isPullToRefreshEnabled = true;    private Progressbar headerProgress;    private TextVIEw headerText;    private ImageVIEw headerImage;    private Animation flipAnimation,reverseAnimation;    private int headerHeight;    private final Handler handler = new Handler();    private OntouchListener ontouchListener;    private OnRefreshListener onRefreshListener;    private SmoothScrollRunnable currentSmoothScrollRunnable;    private float startY = -1;    private final float[] lastYs = new float[EVENT_COUNT];    private String releaseLabel;    private String pullLabel;    private String refreshingLabel;    // ===========================================================    // Constructors    // ===========================================================    public PullToRefreshBase(Context context) {        this(context,null);    }    public PullToRefreshBase(Context context,AttributeSet attrs) {        super(context,attrs);        init(context,attrs);    }    // ===========================================================    // Getter & Setter    // ===========================================================    /**     * Get the Wrapped AdapterVIEw. Anything returned here has already been     * added to the content vIEw.     *      * @return The AdapterVIEw which is currently wrapped     */    public final T getAdapterVIEw() {        return adapterVIEw;    }    /**     * Whether Pull-to-Refresh is enabled     *      * @return enabled     */    public boolean isPullToRefreshEnabled() {        return isPullToRefreshEnabled;    }    /**     * Mark the current Refresh as complete. Will reset the UI and hIDe the     * Refreshing VIEw     */    public voID onRefreshComplete() {        resetheader();    }    public voID setonRefreshListener(OnRefreshListener Listener) {        onRefreshListener = Listener;    }    /**     * A mutator to enable/disable Pull-to-Refresh for the current AdapterVIEw     *      * @param enable     *            Whether Pull-To-Refresh should be used     */    public voID setPullToRefreshEnabled(boolean enabled) {        this.isPullToRefreshEnabled = enabled;    }    public voID setReleaseLabel(String releaseLabel) {        this.releaseLabel = releaseLabel;    }    public voID setPullLabel(String pullLabel) {        this.pullLabel = pullLabel;    }    public voID setRefreshingLabel(String refreshingLabel) {        this.refreshingLabel = refreshingLabel;    }    public voID setheaderProgress(Progressbar headerProgress) {        this.headerProgress = headerProgress;    }    // ===========================================================    // Methods for/from SuperClass/Interfaces    // ===========================================================    @OverrIDe    public voID setontouchListener(OntouchListener Listener) {        ontouchListener = Listener;    }    @OverrIDe    public boolean ontouch(VIEw vIEw,MotionEvent ev) {        if (isPullToRefreshEnabled) {            // Returning true here stops the ListVIEw being scrollable while we            // refresh            if (state == REFRESHING) {                return true;            } else {                return onAdapterVIEwtouch(vIEw,ev);            }        }        return false;    }    /**     * This is implemented by derived classes to return the created AdapterVIEw.     * If you need to use a custom AdapterVIEw (such as a custom ListVIEw),* overrIDe this method and return an instance of your custom class.     *      * Be sure to set the ID of the vIEw in this method,especially if you're     * using a ListActivity or ListFragment.     *      * @param context     * @param attrs     *            AttributeSet from wrapped class. Means that anything you     *            include in the XML layout declaration will be routed to the     *            AdapterVIEw     * @return New instance of the AdapterVIEw     */    protected abstract T createAdapterVIEw(Context context,AttributeSet attrs);    // ===========================================================    // Methods    // ===========================================================    protected final voID resetheader() {        state = PulL_TO_REFRESH;        initializeYsHistory();        startY = -1;        headerImage.setVisibility(VIEw.VISIBLE);        headerProgress.setVisibility(VIEw.GONE);        smoothScrollTo(0);    }    private voID init(Context context,AttributeSet attrs) {        setorIEntation(linearLayout.VERTICAL);        // header        VIEwGroup header = (VIEwGroup) LayoutInflater.from(context).inflate(R.layout.pull_to_refresh_header,this,false);        headerText = (TextVIEw) header.findVIEwByID(R.ID.pull_to_refresh_text);        pullLabel = context.getString(R.string.pull_to_refresh_pull_label);        refreshingLabel = context.getString(R.string.pull_to_refresh_refreshing_label);        releaseLabel = context.getString(R.string.pull_to_refresh_release_label);        headerImage = (ImageVIEw) header.findVIEwByID(R.ID.pull_to_refresh_image);        headerProgress = (Progressbar) header.findVIEwByID(R.ID.pull_to_refresh_progress);        addVIEw(header,VIEwGroup.LayoutParams.FILL_PARENT,VIEwGroup.LayoutParams.WRAP_CONTENT);        measureVIEw(header);        headerHeight = header.getMeasuredHeight();        // AdapterVIEw        // By passing the attrs,we can add ListVIEw/GrIDVIEw params via XML        adapterVIEw = this.createAdapterVIEw(context,attrs);        adapterVIEw.setontouchListener(this);        addVIEw(adapterVIEw,VIEwGroup.LayoutParams.FILL_PARENT);        // Animations        flipAnimation = new RotateAnimation(0,-180,Animation.@R_403_4614@_TO_SELF,0.5f,0.5f);        flipAnimation.setInterpolator(new linearInterpolator());        flipAnimation.setDuration(250);        flipAnimation.setFillAfter(true);        reverseAnimation = new RotateAnimation(-180,0.5f);        reverseAnimation.setInterpolator(new linearInterpolator());        reverseAnimation.setDuration(250);        reverseAnimation.setFillAfter(true);        // HIDe header VIEw        setpadding(getpaddingleft(),-headerHeight,getpaddingRight(),getpaddingBottom());    }    private voID measureVIEw(VIEw child) {        VIEwGroup.LayoutParams p = child.getLayoutParams();        if (p == null) {            p = new VIEwGroup.LayoutParams(VIEwGroup.LayoutParams.FILL_PARENT,VIEwGroup.LayoutParams.WRAP_CONTENT);        }        int chilDWIDthSpec = VIEwGroup.getChildMeasureSpec(0,0 + 0,p.wIDth);        int lpHeight = p.height;        int childHeightSpec;        if (lpHeight > 0) {            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,MeasureSpec.EXACTLY);        } else {            childHeightSpec = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UnspecIFIED);        }        child.measure(chilDWIDthSpec,childHeightSpec);    }    private boolean onAdapterVIEwtouch(VIEw vIEw,MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_MOVE:                updateEventStates(event);                if (isPullingDownToRefresh() && startY == -1) {                    if (startY == -1) {                        startY = event.getY();                    }                    return false;                }                if (startY != -1 && !adapterVIEw.ispressed()) {                    pullDown(event,startY);                    return true;                }                break;            case MotionEvent.ACTION_UP:                initializeYsHistory();                startY = -1;                if (state == RELEASE_TO_REFRESH) {                    setRefreshing();                    if (onRefreshListener != null) {                        onRefreshListener.onRefresh();                    }                } else {                    smoothScrollTo(0);                }                break;        }        if (null != ontouchListener) {            return ontouchListener.ontouch(vIEw,event);        }        return false;    }    private voID pullDown(MotionEvent event,float firstY) {        float averageY = average(lastYs);        int height = (int) (Math.max(averageY - firstY,0));        setheaderScroll(height);        if (state == PulL_TO_REFRESH && headerHeight < height) {            state = RELEASE_TO_REFRESH;            headerText.setText(releaseLabel);            headerImage.clearanimation();            headerImage.startAnimation(flipAnimation);        }        if (state == RELEASE_TO_REFRESH && headerHeight >= height) {            state = PulL_TO_REFRESH;            headerText.setText(pullLabel);            headerImage.clearanimation();            headerImage.startAnimation(reverseAnimation);        }    }    private voID setheaderScroll(int y) {        scrollTo(0,-y);    }    private int getheaderScroll() {        return -getScrollY();    }    private voID setRefreshing() {        state = REFRESHING;        headerText.setText(refreshingLabel);        headerImage.clearanimation();        headerImage.setVisibility(VIEw.INVISIBLE);        headerProgress.setVisibility(VIEw.VISIBLE);        smoothScrollTo(headerHeight);    }    private float average(float[] ysArray) {        float avg = 0;        for (int i = 0; i < EVENT_COUNT; i++) {            avg += ysArray[i];        }        return avg / EVENT_COUNT;    }    private voID initializeYsHistory() {        for (int i = 0; i < EVENT_COUNT; i++) {            lastYs[i] = 0;        }    }    private voID updateEventStates(MotionEvent event) {        for (int i = 0; i < EVENT_COUNT - 1; i++) {            lastYs[i] = lastYs[i + 1];        }        float y = event.getY();        int top = adapterVIEw.gettop();        lastYs[EVENT_COUNT - 1] = y + top;    }    private boolean isPullingDownToRefresh() {        return isPullToRefreshEnabled && state != REFRESHING && isUserDraggingDownwards() && isFirstVisible();    }    private boolean isFirstVisible() {        if (this.adapterVIEw.getCount() == 0) {            return true;        } else if (adapterVIEw.getFirstVisibleposition() == 0) {            return adapterVIEw.getChildAt(0).gettop() >= adapterVIEw.gettop();        } else {            return false;        }    }    private boolean isUserDraggingDownwards() {        return this.isUserDraggingDownwards(0,EVENT_COUNT - 1);    }    private boolean isUserDraggingDownwards(int from,int to) {        return lastYs[from] != 0 && lastYs[to] != 0 && Math.abs(lastYs[from] - lastYs[to]) > 10                && lastYs[from] < lastYs[to];    }    private voID smoothScrollTo(int y) {        if (null != currentSmoothScrollRunnable) {            currentSmoothScrollRunnable.stop();        }        this.currentSmoothScrollRunnable = new SmoothScrollRunnable(handler,getheaderScroll(),y);        handler.post(currentSmoothScrollRunnable);    }    // ===========================================================    // Inner and Anonymous Classes    // ===========================================================    public static interface OnRefreshListener {        public voID onRefresh();    }}
解决方法 为了获得更好的答案,我必须看到您的CustomPagerAdapter的实现.

但是,由于CustomPagerAdapter的构造函数将一个数组/集合/向量的视图作为参数,假设CustomPagerAdapter的instantiateItem方法(或者如果您的CustomPagerAdapter是FragmentPagerAdapter的getItem方法)使用这些视图构建VIEwPager的页面是否正确?

如果是这样,那可能是你的问题.

矢量<视图>页面变量包含已经是布局的一部分的视图.在这种情况下,(唯一)VIEw中有mGroupList. mGroupList已经是布局的一部分.你不能做一个视图(在这种情况下是mGroupList)是两个不同父母的孩子.

而不是“找到”mGroupList,而是在CustomPagerAdapter的instantiateItem方法中创建一个全新的PullToRefreshGrIDVIEw(通过代码或通过扩充相应的layout-xml).全新的PullToRefreshGrIDVIEw实例将不再具有父权限,但是当VIEwPager认为有必要时,它将成为VIEwPager的子代.

为了使事情变得更简单,我建议使用FragmentPagerAdapter或FragmentStatePagerAdapter,它将在其getItem方法中返回一个适当的Fragment.然后,Fragment将在其onCreateVIEw方法中返回正确的视图,其中包含您想要的PullToRefreshGrIDVIEw.
https://developer.android.com/reference/android/support/v13/app/FragmentPagerAdapter.html

总结

以上是内存溢出为你收集整理的android – 使用Pull来刷新ViewGager中的gridview在ActivityGroup中的Activity中导致ClassCastException全部内容,希望文章能够帮你解决android – 使用Pull来刷新ViewGager中的gridview在ActivityGroup中的Activity中导致ClassCastException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1132437.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存