Android 获取空的RecyclerView

Android 获取空的RecyclerView,android,android-fragments,android-recyclerview,navigation-drawer,Android,Android Fragments,Android Recyclerview,Navigation Drawer,这是我第一次与RecyclerView合作,我遇到了一个问题。我正试图从抽屉碎片上看出来。抽屉的菜单很好用,但我看不到那里的回收站。有人能帮我吗 这是我的片段的布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android

这是我第一次与RecyclerView合作,我遇到了一个问题。我正试图从抽屉碎片上看出来。抽屉的菜单很好用,但我看不到那里的回收站。有人能帮我吗

这是我的片段的布局:

<LinearLayout 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:background="#DADADA"
tools:context="com.example.entwicklung1.designtestapp.NavigationDrawerFragment"
android:orientation="horizontal">

<LinearLayout
    android:id="@+id/linid"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:background="#0064a7"
    android:paddingLeft="20dp">

    <ImageView
        android:layout_width="240dp"
        android:layout_height="120dp"
        android:elevation="20dp"
        android:gravity="center_vertical"
        android:src="@drawable/zanderlogo" />
</LinearLayout>

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

    ></android.support.v7.widget.RecyclerView>

</LinearLayout>

代码如下:

public class NavigationDrawerFragment extends Fragment {
private RecyclerView recyclerView;

public static final String PREF_FILE_NAME = "testpref";
public static final String KEY_USER_LEARNED_DRAWNER = "user_learned_drawer";
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private MyAdapter adapter;

private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;
private View containerView;

public NavigationDrawerFragment() {
    // Required empty public constructor
}

public  List<Information> getData(){
    List<Information> data = new ArrayList<>();
    int[] icons={R.drawable.ic_number1,R.drawable.ic_number2,R.drawable.ic_number3,R.drawable.ic_number4,R.drawable.ic_number5};
    String[] titles ={"News", "Produkte","Homepage","Kontakt","Anfahrt"};

    for(int i=0;i<titles.length; i++){
        Information current=new Information();
        current.iconId=icons[i];
        current.title=titles[i];
        data.add(current);
    }
    return data;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mUserLearnedDrawer = Boolean.valueOf(readFromPreferences(getActivity(), KEY_USER_LEARNED_DRAWNER, "false"));
    mFromSavedInstanceState = savedInstanceState != null ? true : false;

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View layout=inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
    recyclerView=(RecyclerView)layout.findViewById(R.id.drawerList);
    adapter = new MyAdapter(getActivity(),getData());
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(adapter);
    return layout;
}




public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
    containerView=getActivity().findViewById(fragmentId);
    mDrawerLayout = drawerLayout;
    mDrawerToggle = new ActionBarDrawerToggle(getActivity(),
            drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if(!mUserLearnedDrawer){
                mUserLearnedDrawer=true;
                saveToPreferences(getActivity(),KEY_USER_LEARNED_DRAWNER,mUserLearnedDrawer+"");
            }
            getActivity().supportInvalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            getActivity().supportInvalidateOptionsMenu();

        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
            if(!mUserLearnedDrawer && !mFromSavedInstanceState){
                mDrawerLayout.openDrawer(containerView);
            }
        }
    });
}

public static void saveToPreferences(Context context, String preferenceName, String preferenceValue) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putString(preferenceName, preferenceValue);
    editor.apply();
}

public static String readFromPreferences(Context context, String preferenceName, String defaultValue) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getString(preferenceName, defaultValue);
}
}
公共类NavigationDrawerFragment扩展片段{
私人回收站;
公共静态最终字符串PREF_FILE_NAME=“testpref”;
公共静态最终字符串键\u USER\u LEARNED\u DRAWNER=“USER\u LEARNED\u drawer”;
私有操作bardrawertoggle mDrawerToggle;
私人抽屉布局mDrawerLayout;
专用MyAdapter适配器;
私有布尔mUserLearnedDrawer;
私有布尔值mFromSavedInstanceState;
私有视图容器视图;
公共导航抽屉碎片(){
//必需的空公共构造函数
}
公共列表getData(){
列表数据=新的ArrayList();
int[]icons={R.drawable.ic_number1,R.drawable.ic_number2,R.drawable.ic_number3,R.drawable.ic_number4,R.drawable.ic_number5};
字符串[]标题={“新闻”、“产品”、“主页”、“康塔克”、“安法特”};

对于(inti=0;i您不在适配器构造函数中设置数据。因此,您的数据为空

    List<Information> data = Collections.emptyList();

    public MyAdapter(Context context,List<Information> data) {
       inflater=LayoutInflater.from(context);
       this.data = data;
       ^^^^^^^^^^^^^^^^ add this statement
    }
List data=Collections.emptyList();
公共MyAdapter(上下文、列表数据){
充气器=充气器。从(上下文);
这个数据=数据;
^^^^^^^^^^^^^^^^添加此语句
}
更新:

需要将顶部线性布局的方向更改为“垂直”

请记住:

  • 传递给适配器的列表不能为空
  • 如果为空,请记住在适配器上调用#notifyDataSetChanged()
  • 必须将LayoutManager设置为RecyclerView

谢谢。但这并不能解决问题。仍然是空白片段。尝试将recycler视图的布局高度设置为匹配父级。只是为了测试。您将顶部线性布局的方向设置为水平,将第一项宽度设置为匹配父级,所以,您的recycler视图不在布局中。您应该将顶部线性布局的方向设置为veRTICLI更新了我的答案。如果对你有帮助,请接受。
    List<Information> data = Collections.emptyList();

    public MyAdapter(Context context,List<Information> data) {
       inflater=LayoutInflater.from(context);
       this.data = data;
       ^^^^^^^^^^^^^^^^ add this statement
    }