Я использую Pagination in Recyclerview. Я использовал два макета в RecyclerView.ViewHolder. Я хочу изменить макет при нажатии кнопки из другого класса.Как изменить компоновку в OncreateViewHolder в 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;
}
}
выше адаптер показывает несколько представлений макета и его состояние
ButtonClick
здесь код для нажатия кнопки
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();
}
});
Кнопка нажата в recyclerview или в отдельном представлении? –
Не в Recyclerview. В режиме Seperate –
создайте общедоступную переменную в вашем адаптере, чтобы сохранить тип вида как «VIEW_TYPE_LOADING» или «VIEW_TYPE_ITEM», и изменить его при нажатии кнопки, где бы вы ни находились. –