我想创建一个水平回收 View ,我编写了以下代码:
在主要 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>
这是适配器类
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 函数有错误: 无法解析类型 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>
请帮我解决这个问题
最佳答案
当您使用 RecyclerView 时,请通过 RecyclerView.Adapter<> 扩展您的适配器类。
请引用:https://www.google.com/amp/s/www.androidhive.info/2016/01/android-working-with-recycler-view/amp/
关于java - recycleview 设置适配器中支持 v4 view.NestedScrollingChild2 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54598997/