我有一个以下设计的片段 需要帮助来实现这一点。
尝试将其作为Header放入适配器但没有运气。
我的适配器
public class FeedListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements GetJSONListener {
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
private final int VIEW_TYPE_HEADER = 2;
private List<FeedItem> feedItems;
private Context mContext;
private CallWSTask callWSTask;
private View VaultsHeader;
private ArrayList<String> userVaults;
private MyVaultsAdapter vaultsAdapter;
private List<VaultsPojo> vaultsPojo;
// Provide a suitable constructor (depends on the kind of dataset)
public FeedListAdapter(Context context, List<FeedItem> feedItems, View header) {
this.feedItems = feedItems;
mContext = context;
if(!header.equals(null)){
this.VaultsHeader = header;
}
}
public boolean isHeader(int position) {
return position == 0;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_note, null);
if (viewType == VIEW_TYPE_ITEM) {
View view = LayoutInflater.from(mContext).inflate(R.layout.feed_note, parent, false);
return new FeedListViewHolder(view);
} else if (viewType == VIEW_TYPE_LOADING) {
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_loading_item, parent, false);
return new LoadingViewHolder(view);
}else if(viewType == VIEW_TYPE_HEADER){
View view = LayoutInflater.from(mContext).inflate(R.layout.vaults_header_view, parent, false);
return new HeaderViewHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof FeedListViewHolder) {
final FeedItem feedItem = feedItems.get(position);
FeedListViewHolder feedListViewHolder = (FeedListViewHolder) holder;
feedListViewHolder.display_name_tv.setText(feedItem.getUserDisp().getFirstName() + " " + feedItem.getUserDisp().getLastName());
feedListViewHolder.display_name_tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mContext, UserProfileActivity.class);
i.putExtra("userid", feedItem.getUserId());
mContext.startActivity(i);
}
});
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
Long.parseLong(feedItem.getCreateDate()),
System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
feedListViewHolder.timestamp_tv.setText(timeAgo);
feedListViewHolder.abbreviationtxt.setText("Ma");
Picasso.with(mContext).load(mContext.getString(R.string.ENVIRONMENT1) + feedItem.getUserDisp().getProfilepicName())
.into(feedListViewHolder.user_iv);
Picasso.with(mContext).load(SharedPreferenceUtils.getProfilePic(mContext)).into(((FeedListViewHolder) holder).comment_user_iv);
feedListViewHolder.like_count.setText(feedItem.getFavoriteCount() + "");
feedListViewHolder.comment_count.setText(feedItem.getCommentCount() + "");
feedListViewHolder.share_count.setText(feedItem.getShareCount() + "");
if (feedItem.isLike()) {
feedListViewHolder.like_empty_iv.setImageDrawable(mContext.getDrawable(R.drawable.ic_action_helpful_selected));
} else {
feedListViewHolder.like_empty_iv.setImageDrawable(mContext.getDrawable(R.drawable.ic_action_helpful));
}
feedListViewHolder.like_empty_iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "Liked!", Toast.LENGTH_LONG).show();
}
});
feedListViewHolder.comment_iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "coment!", Toast.LENGTH_LONG).show();
}
});
feedListViewHolder.share_iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "shared!", Toast.LENGTH_LONG).show();
}
});
if (feedItem.getFeedType().equals("Note")) {
feedListViewHolder.FeedNote.setVisibility(View.VISIBLE);
feedListViewHolder.FeedAskQuestion.setVisibility(View.GONE);
feedListViewHolder.FedMcq.setVisibility(View.GONE);
feedListViewHolder.FeedEmbedYouTube.setVisibility(View.GONE);
feedListViewHolder.FeedEmbedWeb.setVisibility(View.GONE);
feedListViewHolder.embed_disp_tv.setText(Html.fromHtml(feedItem.getNotedisp().getNoteContent()));
if (feedItem.getNotedisp().getAttachment().toString() != "[]") {
if (feedItem.getNoteDisp().getAttachment().size() == 1) {
if (feedItem.getNoteDisp().getAttachment().get(0).isImage()) {
feedListViewHolder.note_post_iv01.setVisibility(View.VISIBLE);
feedListViewHolder.gridView.setVisibility(View.GONE);
Picasso.with(mContext)
.load(mContext.getString(R.string.ENVIRONMENT1) + "/MediaImages/Medium/" + feedItem.getNoteDisp().getAttachment().get(0).getFileName())
.into(feedListViewHolder.note_post_iv01);
Log.v("one image",mContext.getString(R.string.ENVIRONMENT1) + "/MediaImages/Medium/" + feedItem.getNoteDisp().getAttachment().get(0).getFileName());
} else if (feedItem.getNoteDisp().getAttachment().get(0).isVideo()) {
}
}else if(feedItem.getNoteDisp().getAttachment().size() > 1){
feedListViewHolder.gridView.setVisibility(View.VISIBLE);
feedListViewHolder.note_post_iv01.setVisibility(View.GONE);
FeedNoteAttachmentAdapter attachmentAdapter = new FeedNoteAttachmentAdapter(mContext, feedItem.getNoteDisp().getAttachment());
feedListViewHolder.gridView.setAdapter(attachmentAdapter);
feedListViewHolder.gridView.autoresize();
}
}else{
feedListViewHolder.gridView.setVisibility(View.GONE);
feedListViewHolder.note_post_iv01.setVisibility(View.GONE);
}
} else if (feedItem.getFeedType().equals("Embed")) {
if (feedItem.getEmbed().getEmbedType().equals("youtube")) {
feedListViewHolder.FeedNote.setVisibility(View.GONE);
feedListViewHolder.FeedAskQuestion.setVisibility(View.GONE);
feedListViewHolder.FedMcq.setVisibility(View.GONE);
feedListViewHolder.FeedEmbedWeb.setVisibility(View.GONE);
feedListViewHolder.FeedEmbedYouTube.setVisibility(View.VISIBLE);
feedListViewHolder.btnplay.setVisibility(View.VISIBLE);
((FeedListViewHolder) holder).youtube_title.setText(Html.fromHtml(feedItem.getEmbed().getEmbedTitle()));
((FeedListViewHolder) holder).youtube_disp.setText(Html.fromHtml(feedItem.getEmbed().getEmbedDescription()));
Picasso.with(mContext).load(feedItem.getEmbed().getEmbedImageUrl())
.into(feedListViewHolder.youtube_post_iv);
} else {
feedListViewHolder.FeedNote.setVisibility(View.GONE);
feedListViewHolder.FeedAskQuestion.setVisibility(View.GONE);
feedListViewHolder.FedMcq.setVisibility(View.GONE);
feedListViewHolder.FeedEmbedYouTube.setVisibility(View.GONE);
feedListViewHolder.FeedEmbedWeb.setVisibility(View.VISIBLE);
((FeedListViewHolder) holder).embed_title.setText(Html.fromHtml(feedItem.getEmbed().getEmbedTitle()));
((FeedListViewHolder) holder).embed_disp.setText(Html.fromHtml(feedItem.getEmbed().getEmbedDescription()));
if ((feedItem.getEmbed().getEmbedImageUrl() != null) && (feedItem.getEmbed().getEmbedImageUrl().length() > 0) && (!feedItem.getEmbed().getEmbedImageUrl().equals("null"))) {
feedListViewHolder.embed_image.setVisibility(View.VISIBLE);
Picasso.with(mContext).load(feedItem.getEmbed().getEmbedImageUrl())
.into(feedListViewHolder.embed_image);
} else {
feedListViewHolder.embed_image.setVisibility(View.GONE);
}
} //youTubeView.setVisibility(View.GONE);
} else if (feedItem.getFeedType().equals("AskQuestion")) {
feedListViewHolder.FeedNote.setVisibility(View.GONE);
feedListViewHolder.FeedAskQuestion.setVisibility(View.VISIBLE);
feedListViewHolder.FedMcq.setVisibility(View.GONE);
feedListViewHolder.FeedEmbedYouTube.setVisibility(View.GONE);
feedListViewHolder.FeedEmbedWeb.setVisibility(View.GONE);
feedListViewHolder.question_txt.setText(Html.fromHtml(feedItem.getAskQuestions().getQuestionText()));
} else if (feedItem.getFeedType().equals("MCQ")) {
feedListViewHolder.FeedNote.setVisibility(View.GONE);
feedListViewHolder.FeedAskQuestion.setVisibility(View.GONE);
feedListViewHolder.FedMcq.setVisibility(View.VISIBLE);
feedListViewHolder.FeedEmbedYouTube.setVisibility(View.GONE);
feedListViewHolder.FeedEmbedWeb.setVisibility(View.GONE);
feedListViewHolder.mcq_question.setText(Html.fromHtml(feedItem.getMCQ().getQuestion()));
Typeface ubantulight = Typeface.createFromAsset(mContext.getAssets(), "fonts/Ubuntu-L.ttf");
feedListViewHolder.radioButton01.setTypeface(ubantulight);
feedListViewHolder.radioButton02.setTypeface(ubantulight);
feedListViewHolder.radioButton03.setTypeface(ubantulight);
feedListViewHolder.radioButton04.setTypeface(ubantulight);
if (feedItem.getMCQ().getAnswer().size() == 4) {
feedListViewHolder.radioButton01.setText(feedItems.get(position).getMCQ().getAnswer().get(0).getAnswerText());
feedListViewHolder.radioButton02.setText(feedItems.get(position).getMCQ().getAnswer().get(1).getAnswerText());
feedListViewHolder.radioButton03.setText(feedItems.get(position).getMCQ().getAnswer().get(2).getAnswerText());
feedListViewHolder.radioButton04.setText(feedItems.get(position).getMCQ().getAnswer().get(3).getAnswerText());
// viewHolder = (ViewHolder) convertView.getTag();
} else if (feedItem.getMCQ().getAnswer().size() == 3) {
feedListViewHolder.radioButton04.setVisibility(View.GONE);
feedListViewHolder.radioButton03.setText(feedItems.get(position).getMCQ().getAnswer().get(2).getAnswerText());
feedListViewHolder.radioButton01.setText(feedItems.get(position).getMCQ().getAnswer().get(0).getAnswerText());
feedListViewHolder.radioButton02.setText(feedItems.get(position).getMCQ().getAnswer().get(1).getAnswerText());
// viewHolder = (ViewHolder) convertView.getTag();
} else if (feedItem.getMCQ().getAnswer().size() == 2) {
feedListViewHolder.radioButton01.setText(feedItems.get(position).getMCQ().getAnswer().get(0).getAnswerText());
feedListViewHolder.radioButton02.setText(feedItems.get(position).getMCQ().getAnswer().get(1).getAnswerText());
feedListViewHolder.radioButton03.setVisibility(View.GONE);
feedListViewHolder.radioButton04.setVisibility(View.GONE);
// viewHolder = (ViewHolder) convertView.getTag();
} else {
feedListViewHolder.radioButton01.setVisibility(View.GONE);
feedListViewHolder.radioButton02.setVisibility(View.GONE);
feedListViewHolder.radioButton03.setVisibility(View.GONE);
feedListViewHolder.radioButton04.setVisibility(View.GONE);
//viewHolder = (ViewHolder) convertView.getTag();
}
}
//this line is important
feedListViewHolder.itemView.setTag(feedItems.get(position));
//setAnimation(feedListViewHolder.mainlinear_feed,position);
} else if (holder instanceof LoadingViewHolder) {
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
if (!Utilities.Isconnetcted(mContext) && position >= 30) {
loadingViewHolder.errormsg.setVisibility(View.VISIBLE);
} else {
loadingViewHolder.progressBar.setVisibility(View.VISIBLE);
loadingViewHolder.progressBar.setIndeterminate(true);
}
}else if (holder instanceof HeaderViewHolder){
HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
userVaults = SharedPreferenceUtils.getUserVaults(mContext);
//String vaultsjsonstring = new Gson().toJson(userVaults);
vaultsPojo = ResponseParser.parsevaultsJson(userVaults.toString());
vaultsAdapter = new MyVaultsAdapter(mContext,vaultsPojo);
headerViewHolder.vaultsRecyclerView.setLayoutManager(new GridLayoutManager(mContext,2));
headerViewHolder.vaultsRecyclerView.setAdapter(vaultsAdapter);
}
}
@Override
public int getItemCount() {
return feedItems == null ? 0 : feedItems.size();
}
@Override
public void onRemoteCallComplete(String jsonFromWSCall) {
}
public void addFeedItem(List<FeedItem> feedItems) {
//this.feedItems.add(position,feedItems);
this.feedItems.addAll(feedItems);
//notifyDataSetChanged();
}
public String getLastFeedId() {
if (!this.feedItems.isEmpty()) {
return this.feedItems.get(this.feedItems.size() - 2).getFeedId();
} else {
return null;
}
}
@Override
public int getItemViewType(int position) {
if(isHeader(position)){
return VIEW_TYPE_HEADER;
}else {
return feedItems.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}
}
static class LoadingViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public TextView errormsg;
public LoadingViewHolder(View itemView) {
super(itemView);
progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar1);
errormsg = (TextView) itemView.findViewById(R.id.errormsg);
}
}
static class HeaderViewHolder extends RecyclerView.ViewHolder {
public RecyclerView vaultsRecyclerView;
public HeaderViewHolder(View itemView) {
super(itemView);
vaultsRecyclerView = (RecyclerView) itemView.findViewById(R.id.my_Vaults_recycler_view);
}
}}
试过图书馆 RvJoiner
我试图在同一个xml中一个接一个地实现两个回收站视图,但只有一个出现,另一个没有出现。如果你能把我与一些教程联系起来会很棒。
答案 0 :(得分:7)
您可以只使用两种不同的视图类型定义一个RecyclerView
(一个适配器),而使用RecyclerView
定义播放,而不是将两个GridLayoutManager
合并为一个,因此元素可以根据其类型填充不同数量的列。
Here我的实现完全符合您的要求。
我在下面为您提供有关代码和主要内容的一些细节:
在模型中定义两种不同的项类型,一种用于网格部件,另一种用于列表部件。它们都应该扩展相同的抽象类。当然,您可以添加渲染项目所需的所有参数。在这个简单的例子中,我只是在每个元素中使用一个标签。
public abstract class AbstractItem {
public static final int GRID_TYPE = 0;
public static final int LIST_TYPE = 1;
private String label;
public AbstractItem(String label) {
this.label = label;
}
// getters and setters here
abstract public int getType();
}
public class GridItem extends AbstractItem {
public GridItem(String label) {
super(label);
}
@Override
public int getType() {
return GRID_TYPE;
}
}
public class ListItem extends AbstractItem {
public ListItem(String label) {
super(label);
}
@Override
public int getType() {
return LIST_TYPE;
}
}
在Activity
中,使用您需要的RecyclerView
实施行为初始化GridLayoutManager
。特别是,您的网格将有2列。 GridItem
元素只占一列,而ListItem
元素则占2列(因此它们将使用colspan等于2)。您可以使用方法setSpanSizeLookup
获得此结果,如下所示:
GridLayoutManager manager = new GridLayoutManager(this, 2);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
AbstractItem item = mItems.get(position);
switch (item.getType()) {
case AbstractItem.GRID_TYPE:
// grid items to take 1 column
return 1;
default:
// list items to take 2 columns
return 2;
}
}
});
这是我得到的最终结果:
希望这可以提供帮助。