2015-08-14 138 views
1

我已经尝试了很多示例,但没有人适合。请看看我的代码。在onCreateViewHolder我有错误如何通过支持库添加页脚到recyclerView支持库

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

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

我的进度条

<?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> 

回答

0

使用RecyclerView.ViewHolder作为适配器类的泛型参数。 如果您希望有一个支持多种视图类型的适配器,则需要将所有ViewHolders的超类指定为泛型参数。

看看enter link description here获取更多想法 - 它展示了如何通过提取独立类中的所有绑定/视图创建逻辑来使适配器更易于维护。在这种情况下,适配器的唯一责任是管理它配置为使用的ViewHolder委托。