【发布时间】: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>
请帮我解决这个问题
【问题讨论】: