【问题标题】:How to use RecyclerView inside widget.DrawerLayout如何在 widget.DrawerLayout 中使用 RecyclerView
【发布时间】:2016-05-26 16:57:42
【问题描述】:

我正在学习 Xamarin android。

我创建了一个基本活动,它将为每个活动生成菜单中的幻灯片。

我有一个 MyBills.axml 文件,其中包含以下代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
  <android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:paddingBottom="16dp"
    android:paddingTop="16dp"
    android:scrollbars="vertical" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/toolbar_layout">
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            app:layout_scrollFlags="scroll|enterAlways" />
    </android.support.design.widget.AppBarLayout>
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_below="@id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/nav_menu" />
</android.support.v4.widget.DrawerLayout>

这是我的 MyBillsActivity.cs 文件代码。

public class MyBillsActivity : BaseActivity
{
    DrawerLayout drawerLayout;
    protected override int LayoutResource
    {
        get
        {
            return Resource.Layout.MyBills;
        }
    }
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        drawerLayout = this.FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        drawerLayout.CloseDrawers();


        // Create your application here
    }
    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        switch (item.ItemId)
        {
            case Android.Resource.Id.Home:
                drawerLayout.OpenDrawer(Android.Support.V4.View.GravityCompat.Start);
                return true;
        }
        return base.OnOptionsItemSelected(item);
    }
}

现在当 MyBills.axml 页面加载时,它会引发以下错误。这是因为下面的代码返回空值

 drawerLayout = this.FindViewById<DrawerLayout>(Resource.Id.drawer_layout); 

System.NullReferenceException:对象引用未设置为对象的实例

有人可以帮我解释一下为什么会抛出错误吗?

注意:如果我从 mybills.axml 文件中删除以下代码,相同的代码也可以正常工作

  <android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:paddingBottom="16dp"
    android:paddingTop="16dp"
    android:scrollbars="vertical" />

【问题讨论】:

    标签: android xamarin.android android-recyclerview


    【解决方案1】:

    不要将 RecyclerView 直接放在您的布局中,而是将 Android.Support.V4.App.Fragment 放在您的 Activity 中,然后在您的片段代码中设置您的 RecycleView

    在我的主要活动中,我有一个带有标签的 ViewPager

    private void SetUpViewPager(ViewPager viewPager)
    {
        TabAdapter adapter = new TabAdapter(SupportFragmentManager);
        adapter.AddFragment(new Tab1MainFragment(), "Tab1");
        adapter.AddFragment(new Tab2MainFragment(), "Tab2");
    
        viewPager.Adapter = adapter;
    }
    

    在 OnCreateView 的片段类中,我设置了 RecicleView

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        recyclerView = inflater.Inflate(Resource.Layout.Tab1Layout, container, false) as RecyclerView;
    
        SetUpRecyclerView(recyclerView);
    
        return recyclerView;
    }
    

    这是我的片段 xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Tab1RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    回收这个布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="35dp"
        android:paddingRight="35dp"
        android:gravity="center_vertical">
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/text1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />
                <TextView
                    android:id="@+id/text2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?attr/textAppearanceListItem" />
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 2018-01-04
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多