Android RecyclerAdapter

时间:2016-07-06 08:32:51

标签: java android android-recyclerview classcastexception recycler-adapter

我很难理解为什么我在RecyclerAdapter类中遇到ClassCastException。我已将其设置为接受两种不同类型的视图,一种用于常规Feed项,另一种用于本机广告。出了什么问题?

在我的主要活动中,我按如下方式调用我的构造函数:

feedItems = new ArrayList<>();
List<Feed> adItems = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
marketFeedRecyclerAdapter = new MarketFeedRecyclerAdapter(this, feedItems, new ImageLoader(new FeedItemFileCache(this)), adItems);

我在RecyclerAdapter课程的这一行收到投诉,其中包含以下堆栈跟踪:

bindAdItemView((AdViewHolder) viewHolder);

我的堆栈跟踪:

  

java.lang.ClassCastException:   com.elgami.utility.LoadingRowRecyclerAdapter $ LoadingViewHolder不能   被投射到com.elgami.market.MarketFeedRecyclerAdapter $ AdViewHolder        在com.elgami.market.MarketFeedRecyclerAdapter.onBindViewHolder(MarketFeedRecyclerAdapter.java:80)        在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:5768)        在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:5801)        在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5037)        在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4913)        在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2029)        在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)        在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)        在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)        在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)        在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)        在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)        在android.view.View.layout(View.java:16636)        在android.view.ViewGroup.layout(ViewGroup.java:5437)        在android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:598)        在android.view.View.layout(View.java:16636)        在android.view.ViewGroup.layout(ViewGroup.java:5437)        在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)        在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)        在android.widget.LinearLayout.onLayout(LinearLayout.java:1495)        在android.view.View.layout(View.java:16636)        在android.view.ViewGroup.layout(ViewGroup.java:5437)        在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)        在android.view.View.layout(View.java:16636)        在android.view.ViewGroup.layout(ViewGroup.java:5437)        在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)        在android.widget.FrameLayout.onLayout(FrameLayout.java:273)        在android.view.View.layout(View.java:16636)        在android.view.ViewGroup.layout(ViewGroup.java:5437)        在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)        在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)        在android.widget.LinearLayout.onLayout(LinearLayout.java:1495)        在android.view.View.layout(View.java:16636)        在android.view.ViewGroup.layout(ViewGroup.java:5437)        在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)        在android.widget.FrameLayout.onLayout(FrameLayout.java:273)        在android.view.View.layout(View.java:16636)        在android.view.ViewGroup.layout(ViewGroup.java:5437)        在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)        在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)        在android.widget.LinearLayout.onLayout(LinearLayout.java:1495)        在android.view.View.layout(View.java:16636)        在android.view.ViewGroup.layout(ViewGroup.java:5437)        在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)        在android.widget.FrameLayout.onLayout(FrameLayout.java:273)        在com.android.internal.policy.PhoneWindow $ DecorView.onLayout(PhoneWindow.java:2678)        在android.view.View.layout(View.java:16636)        在android.view.ViewGroup.layout(ViewGroup.java:5437)        在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)        在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)        在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)        在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6013)        在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858)        在android.view.Choreographer.doCallbacks(Choreographer.java:670)        在android.view.Choreographer.doFrame(Choreographer.java:606)        在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844)     在

以下是我的RecyclerAdapter类:

public class MarketFeedRecyclerAdapter extends LoadingRowRecyclerAdapter {

    private static final int VIEW_TYPE_MARKET_FEED = 0;
    private static final int VIEW_TYPE_AD = 1;

    private final Context context;
    private final List<Feed> feedItems;
    private final ImageLoader feedItemImageLoader;
    private FeedItemClickListener feedItemClickListener;

    private boolean isLongPressed = false;
    public MarketFeedRecyclerAdapter(Context context, List<Feed> feedItems, ImageLoader feedItemImageLoader, List<Feed> adItems) {
        this.context = context;
        this.feedItems = feedItems;
        this.feedItemImageLoader = feedItemImageLoader;
        this.feedItems.addAll(adItems);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_MARKET_FEED:
                return new MarketFeedViewHolder(new FeedItemView(context));
            case VIEW_TYPE_AD:
                return new AdViewHolder(new MarketFeedAdItemView(context));
        }
        return super.onCreateViewHolder(parent, viewType);
    }

    // Differentiate between feedItem views and nativeAds
    @Override
    public int getViewType(int position) {
        int viewType = VIEW_TYPE_MARKET_FEED;
        if ((position % 25 == 0)) {
            viewType = VIEW_TYPE_AD;
        }
        return viewType;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (getViewType(position) == VIEW_TYPE_MARKET_FEED) {
                bindMarketFeedItemView((MarketFeedViewHolder) viewHolder, position - position / 6);
        }  else {
            bindAdItemView((AdViewHolder) viewHolder);
        }
    }

    // For Ad Objects
    private void bindAdItemView(AdViewHolder viewHolder) {

        showNativeAd(viewHolder);

    }

    private void bindMarketFeedItemView(MarketFeedViewHolder viewHolder, int position) {
        final FeedItemView feedItemView = viewHolder.feedItemView;
        final Feed feedDesign = feedItems.get(position);

        // TODO we can probably conditionally show or hide these based on the type of feed item, same as in FeedActivitySingle
        feedItemView.showOrHideEditButton(false);
        feedItemView.showOrHideBuyButton(true);
        feedItemView.showOrHideFlipButton(feedDesign.getDesign().getCompressedBackImage() != null);
        feedItemView.showOrHidePriceText(true);

        // Set the results into TextViews
        feedItemView.setProductPriceText(String.valueOf(feedDesign.getDesign().getPrice()));
        feedItemView.setDownloadsText(String.valueOf(feedDesign.getDesign().getDownloadCount()));
        feedItemView.setLikesText(String.valueOf(feedDesign.getDesign().getLikesCount()));
        feedItemView.setUsernameText(feedDesign.getDesign().getAuthor().getUsername());
        feedItemView.setTimestampText(feedDesign.getTimestampText());
        feedItemView.getSaveImage().setImageResource(feedDesign.isInPersonalGallery() ? R.drawable.ic_action_saved : R.drawable.ic_not_saved);
        feedItemView.getLikeImage().setImageResource(feedDesign.isLiked() ? R.drawable.ic_action_like_feed_full : R.drawable.ic_action_like_feed);

        feedItemView.getTrashImage().setVisibility(ParseHelper.isCurrentUser(feedDesign.getDesign().getAuthor().getObjectId()) ? View.VISIBLE : View.GONE);
        feedItemView.getFeedSocialShareImage().setVisibility(View.VISIBLE);

        switch(feedDesign.getDisplayedSide()) {
            case FRONT:
                feedItemImageLoader.DisplayImage(feedDesign.getDesign().getCompressedImage().getUrl(), feedItemView.getImage(), feedItemView.getProgressBar());
                break;
            case BACK:
                feedItemImageLoader.DisplayImage(feedDesign.getDesign().getCompressedBackImage().getUrl(), feedItemView.getImage(), feedItemView.getProgressBar());
                break;
        }

        if(feedDesign.getDesign().getAuthor().getProfilePicture() != null) {
            feedItemImageLoader.DisplayImage(feedDesign.getDesign().getAuthor().getProfilePicture().getUrl(), feedItemView.getProfilePicture(), null); // TODO should this use profilePictureFileCache?
        } else {
            viewHolder.feedItemView.getProfilePicture().setImageResource(R.drawable.ic_anonymous);
        }

        SetCommentViews(feedItemView, feedDesign.getComments());
        SetClickListeners(feedItemView, feedDesign, position);
    }

    @Override
    protected int getContentDataSize() {
        return feedItems.size();
    }

class MarketFeedViewHolder extends RecyclerView.ViewHolder {

    FeedItemView feedItemView;

    public MarketFeedViewHolder(FeedItemView view) {
        super(view);
        this.feedItemView = view;
    }
}

class AdViewHolder extends RecyclerView.ViewHolder {

    MarketFeedAdItemView adItemView;

    public AdViewHolder(MarketFeedAdItemView view) {
        super(view);
        this.adItemView = view;
    }

}

    private NativeAd nativeAd;
    private AdChoicesView adChoicesView;

    private void showNativeAd(AdViewHolder viewHolder){
        AdSettings.addTestDevice("a6ffb7bec7af13f768f033dbfea042df");
        nativeAd = new NativeAd(context, "846223392142435_1025413774223395");
        nativeAd.setAdListener(new AdListener() {

            @Override
            public void onError(Ad ad, AdError adError) {

            }

            @Override
            public void onAdLoaded(Ad ad) {

                final MarketFeedAdItemView adItemView = viewHolder.adItemView;

                // Setting the Text
                adItemView.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
                adItemView.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
                adItemView.nativeAdTitle.setText(nativeAd.getAdTitle());
                adItemView.nativeAdBody.setText(nativeAd.getAdBody());

                // Downloading and setting the ad icon
                NativeAd.Image adIcon = nativeAd.getAdIcon();
                NativeAd.downloadAndDisplayImage(adIcon, adItemView.nativeAdIcon);

                // Download and setting the cover image
                /*NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();*/
                adItemView.nativeAdMedia.setNativeAd(nativeAd);

                // Add adChoices icon
                if (adChoicesView == null) {
                    adChoicesView = new AdChoicesView(context, nativeAd, true);
                    adItemView.addView(adChoicesView, 0);
                }

                nativeAd.registerViewForInteraction(adItemView);

            }

            @Override
            public void onAdClicked(Ad ad) {

            }
        });

        nativeAd.loadAd();
    }
}

我的RecyclerAdapter扩展了以下类,这有助于加载更多的feedItems:

public abstract class LoadingRowRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int ROW_VIEW_TYPE_LOADING = 72398; // obscure number
    private boolean mContainsLoadingRow;

    protected abstract int getContentDataSize();

    protected abstract int getViewType(int position);

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ROW_VIEW_TYPE_LOADING:
                return new LoadingViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_row, parent, false));
        }

        throw new IllegalArgumentException("viewType is not ROW_VIEW_TYPE_LOADING. You must handle all other values of viewType (defined by getViewType) before calling super.");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        // empty
    }

    @Override
    public final int getItemCount() {
        return mContainsLoadingRow ? (getContentDataSize() + 1) : getContentDataSize();
    }

    @Override
    public int getItemViewType(int position) {
        return (position == getContentDataSize()) ? ROW_VIEW_TYPE_LOADING : getViewType(position);
    }

    /**
     * Sets a boolean which is used by getItemCount and in turn getItemViewType to determine which view type the row should be (loading view vs. other view).
     * Should only be called when there is more results to load in an upcoming api request (determined by calling fragment).
     */
    public void toggleLoadingRowOn() {
        mContainsLoadingRow = true;
    }

    /**
     * Checks to see if a loading row exists by checking an instance boolean and removes the row / clears the boolean.
     * This helps to 'replace' a loading row with a different row.
     */
    public void toggleLoadingRowOff() {
        if (mContainsLoadingRow) {
            mContainsLoadingRow = false;

            // removes the loading row explicitly instead of allowing it to be 'pushed' down when new user suggestion rows are added.
            // this is only required to maintain consistency with the rest of the app.
            int position = getContentDataSize();
            if (position >= 0) {
                notifyItemRemoved(position);
            }
        }
    }

    protected class LoadingViewHolder extends RecyclerView.ViewHolder {

        public LoadingViewHolder(View v) {
            super(v);
        }
    }
}

最后,确定加载更多项目的阈值的类:

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {

    private static final int VISIBLE_THRESHOLD = 5; // The minimum amount of items to have below your current scroll position before loading more
    private LinearLayoutManager mLinearLayoutManager;

    public abstract void onLoadMore();

    public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
        this.mLinearLayoutManager = linearLayoutManager;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        int totalItemCount = mLinearLayoutManager.getItemCount();
        int firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
        int visibleItemCount = recyclerView.getChildCount();

        int lastItemVisible = firstVisibleItem + visibleItemCount;

        // once the last visible item is within VISIBLE_THRESHOLD from the bottom, we want to load more
        if ((totalItemCount - lastItemVisible) <= VISIBLE_THRESHOLD) {
            onLoadMore();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您的代码中存在问题:

@Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (getViewType(position) == VIEW_TYPE_MARKET_FEED) {
                bindMarketFeedItemView((MarketFeedViewHolder) viewHolder, position - position / 6);
        }  else {
            bindAdItemView((AdViewHolder) viewHolder);
        }
    }

您正在检查当前行viewType(这很好),但您实际上可以有3种可能的viewType: market ad loading

所以当这个

@Override
    public int getItemViewType(int position) {
        return (position == getContentDataSize()) ? ROW_VIEW_TYPE_LOADING : getViewType(position);
    }

返回 ROW_VIEW_TYPE_LOADING 您将LoadingViewHolder投射到AdViewHolder

这导致ClassCastException

要解决此问题,只需将onBindViewHolder方法更改为包含所有三种可能的视图类型,或使用else if代替else

希望这有帮助