android - Fragment 上的 RecyclerView 和 CardView,没有附加布局管理器;跳过布局

标签 android android-fragments android-recyclerview android-cardview

我正在尝试通过来自网络的 recyclerview 显示数据,我在 logcat 上发现错误,它说:没有附加布局管理器;跳过布局。

这是我的 recyclerview 代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_utama, container, false);
       recyclerView = (RecyclerView) view.findViewById(R.id.list_utra);
        dataku = new ArrayList<>();
        gridLayoutManager = new GridLayoutManager(getContext(), 4);
        adapter = new RecyclerAdapter(getContext(), dataku);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy){
                if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == dataku.size()-1){
                    Ambildatadariserver(dataku.get(dataku.size()-1).getId());
                }
            }
        });
        return view;
    }

这是我的 RecyclerAdapter :

public class RecyclerAdapter extends RecyclerView.Adapter <RecyclerAdapter.ViewHolder> {
    private Context context;
    private List<DataJson> datajson;

    public RecyclerAdapter(Context context, List<DataJson>datajson){
        this.context = context;
        this.datajson = datajson;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View Itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.daftart_list, parent, false);
        return new ViewHolder(Itemview);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tvnama.setText(datajson.get(position).getNama());
        holder.tvemail.setText(datajson.get(position).getEmail());
        holder.tvtanggal.setText(datajson.get(position).getTanggal());
    }

    @Override
    public int getItemCount() {
        return datajson.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView tvnama, tvemail, tvtanggal;
        public ViewHolder(View Itemview){
            super(Itemview);
            tvnama = (TextView) Itemview.findViewById(R.id.nama);
            tvemail = (TextView) Itemview.findViewById(R.id.email);
            tvtanggal = (TextView) Itemview.findViewById(R.id.tanggal);
        }
    }
}

我的 fragment 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/utamas"
    tools:context="ptguwatirtasejahtera.utraposmobile.FragmentUtama"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_horizontal_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin">

    <!-- TODO: Update blank fragment layout -->

    <android.support.v7.widget.RecyclerView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/list_utra"/>

</RelativeLayout>

这是 MyCardView 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_margin="10dp"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="5dp"
    android:layout_height="80dp">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="Nama: "
        tools:text="Nama: "
        android:id="@+id/nama"
        />
    <TextView
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/nama"
        android:textSize="15dp"
        android:text="Email: "
        tools:text="Email: "
        android:id="@+id/email"
        android:layout_alignParentLeft="true"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tanggal"
        android:layout_alignParentRight="true"
        android:text="Tgl Lahir: "
        android:textSize="15dp"
        android:layout_marginTop="10dp"
        android:layout_below="@id/nama"/>
</RelativeLayout>
</android.support.v7.widget.CardView>

感谢帮助

最佳答案

你错过了这行:

recyclerView.setLayoutManager(gridLayoutManager);

关于android - Fragment 上的 RecyclerView 和 CardView,没有附加布局管理器;跳过布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41790216/

相关文章:

java - 必需的 android.app.Fragment

android - fragment transaction - pop backstack 然后添加 fragment

java - 在运行时动态添加 recyclerView 部分 (Java)

android - 用于从 Android 上的 gmail 应用程序下载附件的 Intent 过滤器

android - 自定义方案的 Intent-filter pathPattern

android - 仅在使用自动链接时忽略 android 的 react native 库

android - 游标返回错误的联系人ID

java - Recyclerview旋转显示时丢失数据,按下按钮时加载数据Android

java - Android 使用 RecyclerView 滚动时列表可以容纳多少个项目

android - React-Native 更改 iOS 和 Android 文件夹的默认位置