RecyclerView不滚动,也不显示大数字

时间:2019-03-25 18:17:22

标签: android android-recyclerview android-adapter

我的RecyclerView有点麻烦。如果我的适配器列表中有20多个项目,则recyclerview不显示任何内容。 <20都可以,并显示项目。另外,我无法在recyclerView上滚动。

在片段中创建RecyclerView:

recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
myAdapter = new MyAdapter();
myAdapter.setList(myList); //ArrayList<MyObject>
recyclerView.setAdapter(myAdapter);

我的适配器:

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

    private final ArrayList<MyObject> myList = new ArrayList<MyObject>();

    public MyAdapter() {}

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_item, parent, false);
        return new MyAdapter(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.bind(myList.get(position));
    }

    @Override
    public int getItemCount() {
        return myList.size();
    }

    public void setList(ArrayList<MyObject> newList) {
        myList.clear();
        myList.addAll(newList);
        notifyDataSetChanged();
    }
}

这是我的观察者:

public class CountryViewHolder extends RecyclerView.ViewHolder {

    private final TextView textView;
    private MyObject object;

    public CountryViewHolder(@NonNull View itemView) {
        super(itemView);
        textView = itemView.findViewById(R.id.my_holder_textview);
    }

    public void bind(@Nullable MyObject object){
        this.object = object;
        if(pbject == null)
            textView.setText("Error. Could not load name.");
        else
            textView.setText(object.getName());
    }
}

myitem.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/my_holder_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_marginBottom="4dp"
        android:textSize="24sp"
        android:gravity="center"/>
</LinearLayout>

我具有recyclerView的片段布局:

<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/myRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

有什么建议吗?

编辑:

将布局从ConstraintLayout更改为LinearLayout,recyclerview将正常运行。因此,罪魁祸首是约束布局。我想保留constraintlayout,但还不知道如何使它生效。

2 个答案:

答案 0 :(得分:0)

代替:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/myRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

尝试:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/myRecyclerView"
    android:layout_width="0dp"
    android:layout_height="0dp"

答案 1 :(得分:0)

我可以看到您在pszAlgId中有一个错误,它应该返回onCreateViewHolder而不是MyViewHolder

在这里MyAdapter可能也是个问题,因为如果必须为真(find more information on SO),您的孩子的大小可能不会改变。