具有API 20或21的RecyclerView适配器

时间:2019-02-09 19:08:25

标签: java android eclipse

我在Eclipse IDE中创建了RecyclerView代码,并且使用了API 20或21(最好使用20)。我添加了以下库:

支持v7 Recyclerview
支持v4
支持v7兼容

但显示错误NestedScrollingChild2

this document write : NestedScrollingChild2 added ver 26

https://developer.android.com/reference/android/support/v4/view/NestedScrollingChild2

但是我想在API 20或21中使用RecyclerView,并在RecyclerView中使用setAdapter设置ArrayList。如何在API 20或21和setAdapter中使用回收器视图?

我的代码:

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

private ArrayList<StructCategory> categoryList;
private ItemClickListener         mClickListener;

AdapterCategory(Context context, ArrayList<StructCategory> categoryList) {
    G.inflater = LayoutInflater.from(context);
    this.categoryList = categoryList;
}

// inflates the row layout from xml when needed
@Override
@NonNull
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = G.inflater.inflate(R.layout.adapter_category, parent, false);
    return new MyViewHolder(view);
}

// binds the data to the view and textview in each row
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    StructCategory hList = categoryList.get(position);
    holder.myTextView.setText(hList.title);
}

// total number of rows
@Override
public int getItemCount() {
    return categoryList.size();
}

// stores and recycles views as they are scrolled off screen
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView myTextView;

    MyViewHolder(View itemView) {
        super(itemView);
        myTextView = (TextView) itemView.findViewById(R.id.cat_txt);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (mClickListener != null)
            mClickListener.onItemClick(view, getAdapterPosition());
    }
}

// convenience method for getting data at click position
public StructCategory getItem(int id) {
    return categoryList.get(id);
}

// allows clicks events to be caught
public void setClickListener(ItemClickListener itemClickListener) {
    this.mClickListener = itemClickListener;
}

// parent activity will implement this method to respond to click events
public interface ItemClickListener {

    void onItemClick(View view, int position);
}}

0 个答案:

没有答案