错误支持v4 view.recycleview集适配器中的NestedScrollingChild2

时间:2019-02-08 19:26:19

标签: java android eclipse

我想创建一个水平回收视图,并编写了以下代码:

在主要活动xml中

 <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" >
    <android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

这是适配器类

public class AdapterNote extends ArrayAdapter<StructCategory> {

public AdapterNote(ArrayList<StructCategory> array) {
    super(G.context, R.layout.adapter_category, array);
}
private static class ViewHolder {

    public TextView txtTitle;


    public ViewHolder(View view) {
        txtTitle = (TextView) view.findViewById(R.id.cat_txt);

    }
    public void fill(final ArrayAdapter<StructCategory> adapter, final StructCategory item, final int position) {
        txtTitle.setText(item.title);
    }
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    StructCategory item = getItem(position);
    if (convertView == null) {
        convertView = G.inflater.inflate(R.layout.adapter_category, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.fill(this, item, position);
    return convertView;
}}

在MainClass setadapter中 setadapter函数有错误: 无法解析android.support.v4.view.NestedScrollingChild2类型。从所需的.class文件中间接引用了

我导入了支持v4 api 20和v7compat v20和v7 recycleview api 20 但是不要用我的代码

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

    RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view);
    myList.setLayoutManager(layoutManager);
    adapter = new AdapterNote(G.tasksCategory);
    myList.setAdapter(adapter);

然后我为适配器类创建xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:padding="8dip" android:gravity="right">
<TextView
    android:id="@+id/cat_txt"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@drawable/category_txt"
    android:gravity="center_vertical|center"
    android:text="TextView"
    android:textColor="#000" />
    </LinearLayout>

请帮助我解决这个问题

2 个答案:

答案 0 :(得分:1)

在使用RecyclerView时,通过RecyclerView.Adapter <>扩展适配器类。

请参考:https://www.google.com/amp/s/www.androidhive.info/2016/01/android-working-with-recycler-view/amp/

答案 1 :(得分:0)

我编辑我的代码,例如recycleview适配器示例: 请检查我的代码并修复它

关于类名和MyViewHolder的错误txt: AdapterCategory类型的层次结构不一致

在问题部分显示此错误: 由于构建路径不完整,因此未构建该项目。找不到android.support.v4.view.NestedScrollingChild2的类文件。修复构建路径,然后尝试构建该项目

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