【问题标题】:Error support v4 view.NestedScrollingChild2 in recycleview set adapter错误支持 v4 view.NestedScrollingChild2 在回收视图集适配器
【发布时间】:2019-07-03 02:01:33
【问题描述】:

我想创建一个水平回收视图,我写了这段代码:

在主要活动 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>

请帮我解决这个问题

【问题讨论】:

    标签: java android eclipse


    【解决方案1】:

    当您使用 RecyclerView 时,通过 RecyclerView.Adapter 扩展您的适配器类。

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

    【讨论】:

    • 我使用了这个方法,但是在适配器类名上仍然显示新的错误:AdapterCategory 类型的层次结构不一致
    • 我使用 sdk ver 21 并导入了 v7 支持和 v4 支持
    【解决方案2】:

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

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

    【讨论】:

    • NestedScrollingChild2 添加到 26.1.0 版!!如何在 21 版中使用 recycleview 适配器?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多