首页 > 解决方案 > 如何重新排列recyclerview中的viewholders?

问题描述

我在 recyclerview 中有多个视图持有者(5 个视图持有者)。我从 API 获取前 4 个视图持有者的数据,对于第 5 个视图持有者,我正在提供静态数据。现在我想要的是我想将静态视图数据放入位置 2。

例如:

pos 0 = API 数据
pos 1 = API 数据
pos 2 =静态数据
pos 3 = API 数据
pos 4 = API 数据

到目前为止,我所做的如下所示。

@Override
public int getItemViewType(int position) {
    if (position < categoryAndVideoList.size() &&
            Constants.FEATURED_ARTIST.equalsIgnoreCase(categoryAndVideoList.get(position)
                    .getCategoryType())) {
        return TYPE_ARTIST;
    } else if (position == categoryAndVideoList.size()) {
        return TYPE_MOOD;
    } else {
        return TYPE_CATEGORY;
    }
}

@Override
public int getItemCount() {
    if (categoryAndVideoList.size() == 1)
        return categoryAndVideoList.size();
    else
        return categoryAndVideoList.size() + 1;

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int
        viewType) {
    View view;
    if (viewType == TYPE_ARTIST) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_artist,
                parent, false);
        return new ArtistSongsHolder(view);
    } else if (viewType == TYPE_MOOD) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mood,
                parent, false);
        return new MoodBannerHolder(view);
    } else {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout
                        .item_parent_home_sample,
                parent, false);
        return new RecentVideosHolder(view);
    }

}

    @Override
public void onBindViewHolder(final RecyclerView.ViewHolder recentVideosHolder, final int
        position) {

    data = new ArrayList<>();
    if (!(recentVideosHolder instanceof MoodBannerHolder)) {
        data.addAll(categoryAndVideoList.get(position).getData());
    }
    /**
     * TYPE ARTIST
     */

    if (position < categoryAndVideoList.size() && Constants.FEATURED_ARTIST
            .equalsIgnoreCase(categoryAndVideoList.get(position).getCategoryType())) {
        data = new ArrayList<>();
        data.addAll(categoryAndVideoList.get(position).getData());
        artistAdapter = new ArtistAdapter(context, data, typeApiValues.get(position),
                position);
        ((ArtistSongsHolder) recentVideosHolder).artistRecylerView.setAdapter(artistAdapter);
        ((ArtistSongsHolder) recentVideosHolder).artistRecylerView.setOverScrollEnabled(true);
        ((ArtistSongsHolder) recentVideosHolder).artistRecylerView.setItemTransformer
                (new ScaleTransformer.Builder().setMinScale(0.7f).build());
        double deviceWidth = Utils.getDisplayWidthValue(context);

        deviceWidthCalc(deviceWidth, (ArtistSongsHolder) recentVideosHolder);

        //To get artist name - categoryAndVideoList.get(position).getCategoryName()
        ((ArtistSongsHolder) recentVideosHolder).header.setText
                (categoryAndVideoList.get(position).getCategoryName());

        ((ArtistSongsHolder) recentVideosHolder).tvSeeAll.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(context, SeeAllActivity.class);
                        intent.putExtra("type", typeApiValues.get(position));
                        intent.putExtra("title",
                                categoryAndVideoList.get(position).getCategoryName());
                        context.startActivity(intent);

                    }
                });

    } else if (recentVideosHolder instanceof MoodBannerHolder) {
        moodAdapter = new MoodAdapter(context, moodBannerList, moodValue);
        RecyclerView.LayoutManager childHomeLayout = new LinearLayoutManager(context,
                LinearLayoutManager.HORIZONTAL, false);
        ((MoodBannerHolder) recentVideosHolder).moodRecycler.setLayoutManager(childHomeLayout);
        ((MoodBannerHolder) recentVideosHolder).moodRecycler.setAdapter(moodAdapter);
        ((MoodBannerHolder) recentVideosHolder).moodHeader.setText(context.getString(R.string.home_mood_banner));

        ((MoodBannerHolder) recentVideosHolder).moodRecycler.setHasFixedSize(true);
        ((MoodBannerHolder) recentVideosHolder).moodRecycler
                .setNestedScrollingEnabled(false);

        ((MoodBannerHolder) recentVideosHolder).moodRecycler.setFocusable(false);
        ((MoodBannerHolder) recentVideosHolder).moodRecycler.scrollTo(0, 0);

    }
    /**
     * TYPE CATEGORIES
     */
    else {
        if (!data.isEmpty()) {
            childRecyclerAdapter = new HomeVideosAdapter(context, data,
                    typeApiValues.get(position));

            /**
             * WEEKLY VIEW
             */
            if (typeApiValues.get(position).equalsIgnoreCase(Constants.WEEKLY)) {
                ((RecentVideosHolder) recentVideosHolder).header.setText
                        (categoryAndVideoList.get(position).getCategoryName());

            }

            /**
             * TRENDING NOW VIEW
             */
            else if (typeApiValues.get(position).equalsIgnoreCase(Constants.TRENDING_NOW)) {
                ((RecentVideosHolder) recentVideosHolder).header.setText(
                        context.getString(R.string.home_trending_now));

            }

            /**
             * TOP CHARTS VIEW
             */
            else if (typeApiValues.get(position).equalsIgnoreCase(Constants.TOP_CHARTS)) {
            }

            /**
             * TOP PICKS VIEW
             */
            else if (typeApiValues.get(position).equalsIgnoreCase(Constants.TOP_PICKS)) {
                ((RecentVideosHolder) recentVideosHolder).header.setText(
                        context.getString(R.string.home_top_picks_for_you));

            } else if (position == 0) {
                ((RecentVideosHolder) recentVideosHolder).header.setText
                        (categoryAndVideoList.get(position).getCategoryName());
            }

            RecyclerView.LayoutManager childHomeLayout = new LinearLayoutManager(context,
                    LinearLayoutManager.HORIZONTAL, false);
            ((RecentVideosHolder) recentVideosHolder).childRecyclerView.setLayoutManager
                    (childHomeLayout);
            ((RecentVideosHolder) recentVideosHolder).childRecyclerView.setAdapter
                    (childRecyclerAdapter);
            ((RecentVideosHolder) recentVideosHolder).childRecyclerView.setHasFixedSize(true);
            ((RecentVideosHolder) recentVideosHolder).childRecyclerView
                    .setNestedScrollingEnabled(false);

            ((RecentVideosHolder) recentVideosHolder).childRecyclerView.setFocusable(false);
            ((RecentVideosHolder) recentVideosHolder).childRecyclerView.scrollTo(0, 0);

            if (categoryAndVideoList.get(position).getLastPage() > 1) {
                ((RecentVideosHolder) recentVideosHolder).seeAll.setVisibility(View.VISIBLE);
            } else {
                ((RecentVideosHolder) recentVideosHolder).seeAll.setVisibility(View.GONE);
            }

            if (typeApiValues.get(position).equalsIgnoreCase(Constants.WEEKLY)) {
                ((RecentVideosHolder) recentVideosHolder).seeAll.setVisibility(View.GONE);
            }

            ((RecentVideosHolder) recentVideosHolder).seeAll.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            Intent intent = new Intent(context, SeeAllActivity.class);
                            intent.putExtra("type", typeApiValues.get(position));
                            intent.putExtra("title",
                                    categoryAndVideoList.get(position).getCategoryName());
                            context.startActivity(intent);

                        }
                    });

        } else {
            ((RecentVideosHolder) recentVideosHolder).parentLayout.setVisibility(View.GONE);
        }
    }

}

我的输出如下:

主页

我想要的是我想要“你的心情怎么样?” 列表放置在“现在趋势”列表下方。我已经搜索了很多,但我没有找到任何东西......我该如何解决这个问题?

标签: androidandroid-recyclerviewandroid-viewholder

解决方案


categoryAndVideoList您可以在该列表的构造函数或 setter 方法的所需位置插入“静态数据” 。

然后你可以修改getItemViewType()如下:

public int getItemViewType(int position) {
    if (position < categoryAndVideoList.size() &&
            Constants.FEATURED_ARTIST.equalsIgnoreCase(categoryAndVideoList.get(position)
                    .getCategoryType())) {
        return TYPE_ARTIST;
    } else if (position ==YOUR_DESIRED_POSITION) {
        return TYPE_MOOD;
    } else {
        return TYPE_CATEGORY;
    }
}

推荐阅读