简体   繁体   中英

How to change Layout in OncreateViewHolder in Recyclerview.ViewHolder

I am using Pagination in Recyclerview. I have used two layouts in RecyclerView.ViewHolder. I want to change the layout on button click from another class.

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;
    }

}

The above adapter shows the Multiple layout views and its condition

ButtonClick
here is the code for button click

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();
            }
        });

If I understood correctly you showing loading if List element is null. So to change element just change your List at that position and update recyclerView adapter.

Create public method inside Adapter class

public void changeLoading(int position, PollList item){

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

Call this method inside 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);
        }
    });

Inside your adapter class create a variable

public int VIEW_TYPE = VIEW_TYPE_LOADING;

Make the two view type variables in your adapter public static.

public static final int VIEW_TYPE_ITEM = 0; public static final int VIEW_TYPE_LOADING = 1;

Create a method in your adapter

public void changeLayout(int viewType)
{
    this.VIEW_TYPE = viewType;
    notifyDatasetChanged();
}

And your OnClickListener() will be

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();

    //change layout here
    pollListAdapter.changeLayout(PollListAdapter.VIEW_TYPE_ITEM)
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM