Android未附加适配器的跳过布局

时间:2019-11-28 05:03:27

标签: android android-recyclerview

我正在使用Android中的recyclerview从Firebase提取数据。当我尝试以其他布局在当前recyclerview中显示activity_main时,显示错误No Adapter Attached Skipping Layout,但是当我仅在空白活动中附加recyclerview时,相同的recyclerview仍然有效。在activity_main中,我有一个Google地图当前显示在FrameLayout和导航菜单的底部,并且一个顶部栏都显示在相对布局中。我正在尝试在Google地图窗口中显示recyclerview。主要活动有什么问题

这是我的activity_main.xml文件,我试图在其中显示recyclerview

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

android:id="@+id/activity_main"
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"
android:background="@color/white"
tools:context="com.trioangle.goferdriver.MainActivity"
android:keepScreenOn="true">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorblock"
    android:id="@+id/relativeLayout2">
    <ImageView
        android:visibility="gone"
        android:id="@+id/homelist"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:layout_centerVertical="true"
        android:src="@drawable/list"
        android:tint="@color/white"/>
    <View
        android:visibility="gone"
        android:id="@+id/view"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/homelist"
        android:background="@color/button_material_light"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"/>
    <com.trioangle.goferdriver.custompalette.FontTextView
        android:id="@+id/txt_driverstatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/offline"
        android:paddingLeft="20dp"
        android:layout_centerVertical="true"
        android:textSize="18sp"
        android:textColor="@color/white"
        app:font="@string/font_UBERMedium"
        android:layout_toRightOf="@+id/homelist"
        />

    <android.support.v7.widget.SwitchCompat
        style="@style/SwitchCompatStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/switch_driverstatus"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:checked="false"
        android:layout_centerVertical="true"/>
    <com.trioangle.goferdriver.custompalette.FontTextView
        android:visibility="gone"
        android:background="@color/ub__red"
        android:id="@+id/txt_checkdriverstatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/checkstatus"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:layout_centerVertical="true"
        android:textSize="18sp"
        android:padding="5dp"
        android:textColor="@color/white"
        app:font="@string/font_UBERMedium"
        />

</RelativeLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/relativeLayout2">

    <TextView
        android:id="@+id/iv_line"
        android:layout_width="50dp"
        android:layout_height="5dp"
        android:background="#ffffff"/>

</RelativeLayout>

<FrameLayout
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/navigation"
    android:layout_below="@+id/relativeLayout2">

   <!-- below is the recycleview not working-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--RecyclerView-->
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview_books"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="52dp"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_editor_absoluteX="8dp"></android.support.v7.widget.RecyclerView>
    </LinearLayout>

</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:layout_centerVertical="true"
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/colorblock"
    app:itemIconTint="@color/nav_item_state_list"
    app:itemTextColor="@color/nav_item_state_list"
    app:menu="@menu/bottom_nav_items"
    android:animateLayoutChanges="false"
    android:splitMotionEvents="false"/>
</RelativeLayout>

这是我的main_activity.java on_Create方法

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.activity_main);

    /*****books view**************************************************************************/

    mRecyclerView=(RecyclerView) findViewById(R.id.recyclerview_books);

    new BookFirebaseHelper().readBooks(new BookFirebaseHelper.DataStatus() {
        @Override
        public void DataIsLoaded(List<Book> books, List<String> keys) {
            new BookViewConfig().setConfig(mRecyclerView,MainActivity.this,books,keys);
        }

        @Override
        public void DataIsInserted() {

        }

        @Override
        public void DataIsUpdated() {

        }

        @Override
        public void DataIsDeleted() {

        }
    });

    /*****books view***********************************************************************/

这是我的bookview_config

public class BookViewConfig {

private Context mContext;
private BooksAdapter mBooksAdapter;

public void setConfig(RecyclerView recyclerView,Context context,List<Book>books,List<String>keys){

    mContext=context;
    mBooksAdapter=new BooksAdapter(books,keys);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(mBooksAdapter);

}

class BookItemView extends RecyclerView.ViewHolder{
    private TextView tvPickup;
    private TextView tvDrop;
    private TextView tvUser;

    private String key;

    public BookItemView(ViewGroup parent){
        super(LayoutInflater.from(mContext).inflate(R.layout.book_list_item,parent,false));
        tvPickup=(TextView) itemView.findViewById(R.id.tvPickup);
        tvDrop=(TextView) itemView.findViewById(R.id.tvDrop);
        tvUser=(TextView) itemView.findViewById(R.id.tvUserId);
    }

    public void bind(Book book, String key){
        tvPickup.setText(book.getPickup_location());
        tvDrop.setText(book.getDrop_location());
        tvUser.setText(book.getUser_id());
        this.key=key;
    }
}

class  BooksAdapter extends RecyclerView.Adapter<BookItemView>{

    private List<Book> mBookList;
    private List<String> mkeys;

    public BooksAdapter(List<Book> mBookList, List<String> mkeys) {
        this.mBookList = mBookList;
        this.mkeys = mkeys;
    }

    public BooksAdapter() {
        super();
    }

    @Override
    public BookItemView onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BookItemView(parent);
    }

    @Override
    public void onBindViewHolder(BookItemView holder, int position) {
        holder.bind(mBookList.get(position),mkeys.get(position));

    }

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

2 个答案:

答案 0 :(得分:0)

您尚未为您的recyclerview创建适配器。创建一个,然后使用recyclerview.setadapter(your_adapter);将recyclerview设置为该适配器;

答案 1 :(得分:0)

LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
list.setLayoutManager(llm);
list.setAdapter(adapter);