1
votes

I have tried lots of examples but no one suitable. Please take a look to my code. In onCreateViewHolder I have error

Error:(64, 17) error: name clash: onBindViewHolder(ViewHolder,int) in MyAdapter and onBindViewHolder(VH,int) in Adapter have the same erasure, yet neither overrides the other where VH is a type-variable: VH extends ViewHolder declared in class Adapter

and

Error:(51, 36) error: onCreateViewHolder(ViewGroup,int) in MyAdapter cannot override onCreateViewHolder(ViewGroup,int) in Adapter return type ViewHolder is not compatible with MyViewHolder where VH is a type-variable: VH extends ViewHolder declared in class Adapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>  {

ArrayList<String> dataset_;
public static final int MODE_DATA = 0;
public static final int MODE_LOADING = 1;

public static class MyViewHolder extends RecyclerView.ViewHolder{
    public TextView mTextView;
    public TextView mTextView2;

    public MyViewHolder(View v){
        super(v);
        mTextView  = (TextView) itemView.findViewById(R.id.textView);
        mTextView2 = (TextView) itemView.findViewById(R.id.textView2);
    }
}

public static class MyLoadingViewHolder extends RecyclerView.ViewHolder{
    public MyLoadingViewHolder(View v){
        super(v);
    }
}

@Override
public int getItemViewType(int position) {
    if (position == dataset_.size() - 1) {
        return MODE_LOADING;
    } else {
        return MODE_DATA;
    }
}

public MyAdapter (ArrayList<String> dataset){
    dataset_ = dataset;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    if (viewType == MODE_LOADING) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.progress_bar, parent, false);
        MyLoadingViewHolder loadingViewHolder = new MyLoadingViewHolder(v);
        return loadingViewHolder;
    } else {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(v);
        return myViewHolder;
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder,int position){
    if (position == MODE_LOADING){
                            // Do nothig just show progress bar
    }
    else {
        ((MyViewHolder)holder).mTextView.setText(dataset_.get(position));
        ((MyViewHolder)holder).mTextView2.setText(dataset_.get(position));
    }
}

@Override
public int getItemCount(){
    return dataset_.size();
}
}

my progress bar

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/progress_bar_panel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ProgressBar
        android:id="@+id/progress_bar"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:visibility="visible" />

</RelativeLayout>
1

1 Answers

0
votes

Use RecyclerView.ViewHolder as generic parameter for the Adapter class. If you'd like have an adapter supporting multiple view types, you need to specify the super class for all of the ViewHolders as generic parameter.

Take a look at enter link description here for some more ideas - it shows how to make the Adapter easier to maintain by extracting all the binding / view creating logic in separate classes. In this case the adapter's only responsibility is to manage the ViewHolder delegates it is configured to use.