简体   繁体   中英

Error support v4 view.NestedScrollingChild2 in recycleview set adapter

I want create a horizontal recycle view and i writed this code:

in main activity 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>

And this is adapter class

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

In the MainClass setadapter setadapter function has error : The type android.support.v4.view.NestedScrollingChild2 cannot be resolved. It is indirectly referenced from required .class files

I imported support v4 api 20 and v7compat v20 and v7 recycleview api 20 but dont work my code

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

and i create xml for adapter class:

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

please help me for this problem

As you are using RecyclerView, extends your adapter class by RecyclerView.Adapter<>.

Please refer this : https://www.google.com/amp/s/www.androidhive.info/2016/01/android-working-with-recycler-view/amp/

I edit my code Like recycleview adapter example: please check my code and fix it

Error txt on Classname and MyViewHolder : The hierarchy of the type AdapterCategory is inconsistent

in the problem section show this error: The project was not built since its build path is incomplete. Cannot find the class file for android.support.v4.view.NestedScrollingChild2. Fix the build path then try building this project

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM