繁体   English   中英

'RecyclerView:没有连接适配器; 在父布局中将 RecyclerView 与 EditText 一起嵌套时跳过布局错误

[英]'RecyclerView: No adapter attached; skipping layout' error when nesting RecyclerView alongside EditText inside parent layout

我有一个 RecyclerView,当它是 XML 文件中唯一的 object 时,它可以正确显示从 RSS 提要获取的数据,即它没有嵌套在父元素中,也没有在另一个元素中共享父元素。 相反,当我尝试这样做时,数据不显示,而是收到错误消息“RecyclerView:未连接适配器; 跳过 LogCat 中的布局。 我应该提一下,这个 RecyclerView 是作为 ListFragment 的一部分生成的。

Fragment 替换了 ActivityMain.xml 中的一个 FrameLayout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="@color/design_default_color_primary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:titleTextColor="#ffff"/>
        <!-- This one -->
        <FrameLayout
            android:id="@+id/main_fragment_target"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id= "@+id/nav_view"
        android:layout_width= "wrap_content"
        android:layout_height= "match_parent"
        android:layout_gravity= "start"
        android:fitsSystemWindows= "true"
        app:menu= "@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

这是由 MainActivity.java 中的以下代码执行的

            MainActivity.this.runOnUiThread(() -> {
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ComponentListFragment componentListFragment = new ComponentListFragment(componentsList);
                transaction.replace(R.id.main_fragment_target, componentListFragment);
                transaction.commit();
            });

如前所述,当我没有弄乱 RecycleView XML 文件时,数据加载得很好,所以我怀疑插入片段是问题所在。 我只是为后代张贴这个。

因此,MainActivity.java 将片段替换为 ComponentListFragment.java 的实例:

package com.example.mclean_ross_s2030507;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

/**
 * A fragment representing a list of Items.
 */
public class ComponentListFragment extends Fragment {

    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    // TODO: Customize parameters
    private int mColumnCount = 1;
    private ArrayList<ListComponent> components;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public ComponentListFragment() {
    }

    public ComponentListFragment(ArrayList<ListComponent> components) {
        this.components = components;
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static ComponentListFragment newInstance(int columnCount) {
        ComponentListFragment fragment = new ComponentListFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_component_list_list, container, false);
        // Set the adapter
        if (view instanceof RecyclerView) {
            Context context = view.getContext();
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
            RecyclerView recyclerView = (RecyclerView) view;
            recyclerView.setHasFixedSize(true);
            recyclerView.addItemDecoration(
                    new DividerItemDecoration(recyclerView.getContext(),
                    linearLayoutManager.getOrientation())
            );
            if (mColumnCount <= 1) {
                recyclerView.setLayoutManager(linearLayoutManager);
            } else {
                recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
            }
            recyclerView.setAdapter(new MyItemRecyclerViewAdapter(components));
        }
        return view;
    }
}

它构建了一个 RecyclerView 并将适配器设置为 MyItemRecyclerViewAdapter.java:

package com.example.mclean_ross_s2030507;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.mclean_ross_s2030507.databinding.FragmentComponentListBinding;
import com.example.mclean_ross_s2030507.placeholder.PlaceholderContent.PlaceholderItem;

import java.util.ArrayList;

/**
 * {@link RecyclerView.Adapter} that can display a {@link PlaceholderItem}.
 * TODO: Replace the implementation with code for your data type.
 */
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {

    private final ArrayList<ListComponent> mValues;

    public MyItemRecyclerViewAdapter(ArrayList<ListComponent> items) {
        mValues = items;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(FragmentComponentListBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Log.e("MyTag", String.valueOf(holder));
        holder.mItem = mValues.get(position);
//        holder.mIdView.setText(mValues.get(position).id);
        holder.mContentView.setText(mValues.get(position).getTitle());

        holder.itemView.setOnClickListener(view -> {

        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
//        public final TextView mIdView;
        public final TextView mContentView;
        public ListComponent mItem;

        public ViewHolder(FragmentComponentListBinding binding) {
            super(binding.getRoot());
//            mIdView = binding.itemNumber;
            mContentView = binding.content;
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mContentView.getText() + "'";
        }
    }
}

最后,这是我试图实现的视图 (fragment_component_list_list.xml),但它导致了错误:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <androidx.recyclerview.widget.RecyclerView
        tools:viewBindingType="androidx.recyclerview.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.example.mclean_ross_s2030507.ComponentListFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:context=".ComponentListFragment"
        tools:listitem="@layout/fragment_component_list"
        android:scrollbars="vertical"/>

</LinearLayout>

但是,具有以下设置的同一文件工作得很好:

<androidx.recyclerview.widget.RecyclerView
    tools:viewBindingType="androidx.recyclerview.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.example.mclean_ross_s2030507.ComponentListFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:context=".ComponentListFragment"
    tools:listitem="@layout/fragment_component_list"
    android:scrollbars="vertical"/>

嵌套: RecycleView获取和显示数据图片

不嵌套: RecycleView获取不到数据的图片

我知道这个错误的各种解决方案都是如此,但我还没有看到任何具体回答我关于嵌套 RecyclerView 的问题。 一如既往,非常感谢任何帮助。 干杯。

根据您提供的R.layout.fragment_component_list_list的内容,在ComponentListFragmentonCreateView中膨胀的视图不是RecyclerView而是LinearLayout 父 View/ViewGroup 总是从inflate返回。 因此,您需要在该布局中引用实际的 RecyclerView:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_component_list_list, container, false);
        RecyclerView rv = (RecyclerView) view.findViewById(R.id.list)
        // No need for if statement, just use `rv` variable...
        ...

此外,您的 XML 有一些错误和需要改进的地方:

  • 删除 RecyclerView 元素中的冗余命名空间 ( xmlns ) 属性并将xmlns:app移至根元素。
  • android:name属性仅在AndroidManifest.xml<application>元素中使用
  • 您不需要tools:viewBindingType属性。 只需要在使用视图绑定类型时消除生成的视图的歧义
  • tools:context仅适用于根视图。 查看工具属性
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/fragment_component_list"
        android:scrollbars="vertical"/>

</LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM