gpt4 book ai didi

android - 如何在 Recyclerview.ViewHolder 的 OncreateViewHolder 中更改布局

转载 作者:行者123 更新时间:2023-11-29 19:22:41 25 4
gpt4 key购买 nike

我在 Recyclerview 中使用分页。我在 RecyclerView.ViewHolder 中使用了两种布局。我想更改从另一个类单击按钮时的布局。

public class PollListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<PollList> pollLists;
RecyclerView mrecyclerView;
private int VIEW_TYPE_ITEM = 0;
private int VIEW_TYPE_LOADING = 1;

public class MyViewHolder extends RecyclerView.ViewHolder {
TextView question, description, createdTime, closedTime;

public MyViewHolder(View view) {
super(view);
question = (TextView) view.findViewById(R.id.questionPoll);
description = (TextView) view.findViewById(R.id.descriptionPoll);
}
}

public static class LoadingViewHolder extends RecyclerView.ViewHolder {
public static ProgressBar progressBar;

public LoadingViewHolder(View itemView) {
super(itemView);
progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar1);
}
}

public PollListAdapter(List<PollList> pollLists, RecyclerView recyclerView, Context con){
this.pollLists = pollLists;
this.mrecyclerView = recyclerView;
this.con = con;
mrecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
if (NewsFragment.nextPageURL != null) {
if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if (mOnLoadMoreListener != null) {
mOnLoadMoreListener.onLoadMore();
}
isLoading = true;
}
}
}
});

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.poll_row, parent, false);
return new MyViewHolder(itemView);
} else if (viewType == VIEW_TYPE_LOADING) {
View view = LayoutInflater.from(con).inflate(R.layout.layout_loading_item, parent, false);
return new LoadingViewHolder(view);
}
return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder) {
PollList poll_list = pollLists.get(position);
MyViewHolder viewHolder = (MyViewHolder) holder;
viewHolder.question.setText(poll_list.getTitle());
viewHolder.description.setText(poll_list.getDescription());
viewHolder.createdTime.setText(poll_list.getCreatedAt());
} else if (holder instanceof LoadingViewHolder){
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setIndeterminate(true);
}
}

@Override
public int getItemViewType(int position) {
return pollLists.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}

@Override
public int getItemCount() {
return pollLists == null ? 0 : pollLists.size();
}

public void setLoaded() {
isLoading = false;
}

public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
this.mOnLoadMoreListener = mOnLoadMoreListener;
}

}

上面的适配器显示了多个布局 View 及其条件

按钮点击
这是按钮点击的代码

createGroupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
groupName.setText("");
groupStatus.clearCheck();
groupVisibility.clearCheck();
finish();
GroupFragment.groupvalues.clear();
GroupFragment.GroupData groupDataRefresh = new GroupFragment.GroupData();
groupDataRefresh.execute();
}
});

最佳答案

如果我没理解错的话,如果 List 元素为 null,您会显示加载。因此,要更改元素,只需更改该位置的 List 并更新 recyclerView 适配器。

在 Adapter 类中创建公共(public)方法

public void changeLoading(int position, PollList item){

pollLists.set(index, item);
notifyItemChanged(index);
}

OnClick 中调用此方法

createGroupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
groupName.setText("");
groupStatus.clearCheck();
groupVisibility.clearCheck();
finish();
GroupFragment.groupvalues.clear();
GroupFragment.GroupData groupDataRefresh = new GroupFragment.GroupData();
groupDataRefresh.execute();

adapter.changeLoading(itemPosition, item);
}
});

关于android - 如何在 Recyclerview.ViewHolder 的 OncreateViewHolder 中更改布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42061546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com