我必须在我的一个片段中创建一个列表,但我无法使recyclerview工作,我整天都在网上搜索教程,但无法弄清楚为什么它不能正常工作
row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:name="com.streak.roadpoliceviolations.ViolationFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="com.streak.roadpoliceviolations.ViolationFragment"
tools:listitem="@layout/fragment_violation" />
适配器:
public class ViolationAdapter extends RecyclerView.Adapter<ViolationAdapter.MyViewHolder>{
private LayoutInflater inflater;
List<ViolationItem> data= Collections.emptyList();
public ViolationAdapter(List<ViolationItem> data) {
//inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.fragment_violation, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ViolationItem current = data.get(position);
holder.txt1.setText(current.txt1);
holder.txt2.setText(current.txt2);
}
@Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView txt1;
TextView txt2;
public MyViewHolder(View itemView) {
super(itemView);
txt1= (TextView) itemView.findViewById(R.id.txt1);
txt2= (TextView) itemView.findViewById(R.id.txt2);
}
}
}
片段类
public class ViolationFragment extends Fragment {
private ViolationAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_violation_list, container, false);
RecyclerView rv = (RecyclerView) view.findViewById(R.id.list);
adapter = new ViolationAdapter(getActivity(),getData());
rv.setAdapter(adapter);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
public static List<ViolationItem> getData() {
List<ViolationItem> data = new ArrayList<>();
String[] txts1 = {"AAA","BBB","CCC"};
String[] txts2 = {"111","222","333"};
for(int i=0; i<=txts1.length; i++) {
ViolationItem c = new ViolationItem();
c.txt1=txts1[i];
c.txt2=txts2[i];
data.add(c);
}
return data;
}
private List<ViolationItem> createList() {
List<ViolationItem> data = new ArrayList<>();
return data;
}
}
一切都没有一个警告但是我不能将它放入片段容器
在我的活动中,我用以下代码调用片段
//Initial fragment
ViolationFragment frag = new ViolationFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction() .replace(R.id.frame_container, frag).commit();
在构建过程中出现此错误
Error:(77, 75) error: incompatible types: ViolationFragment cannot be converted to Fragment
如果我更改frag
初始化Fragment frag = new ViolationFragment();
我得到了同样的错误。
如何最终修复此问题并使片段正常工作?!
答案 0 :(得分:0)
我记得你必须使用支持库片段才能使它工作。请检查并确认。