如何在 recyclerview 中重新排列 viewholders?
How to rearrange the viewholders in recyclerview?
我在 recyclerview 中有多个视图持有者(5 个视图持有者)。我从 API 获取前 4 个视图持有者的数据,并为第 5 个视图持有者提供静态数据。现在我想要的是将静态取景器数据放入位置 2。
例如:
pos 0 = data from API,
pos 1 = data from API,
pos 2 = Static data,
pos 3 = data from API,
pos 4 = data from 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);
}
}
}
我的输出如下:
我想要的是 “你心情怎么样?” 列表放在 “当前流行” 列表下方。我已经搜索了很多,但我没有找到任何东西......
我该如何解决这个问题?
您可以在 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;
}
}
我在 recyclerview 中有多个视图持有者(5 个视图持有者)。我从 API 获取前 4 个视图持有者的数据,并为第 5 个视图持有者提供静态数据。现在我想要的是将静态取景器数据放入位置 2。
例如:
pos 0 = data from API,
pos 1 = data from API,
pos 2 = Static data,
pos 3 = data from API,
pos 4 = data from 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);
}
}
}
我的输出如下:
我想要的是 “你心情怎么样?” 列表放在 “当前流行” 列表下方。我已经搜索了很多,但我没有找到任何东西...... 我该如何解决这个问题?
您可以在 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;
}
}