我一直在做一个在导航抽屉中使用回收器视图的应用程序。为什么回收器视图的内容没有显示出来。视图肯定在那里,因为我可以看到滚动阴影。我不知道我做错了什么,因为应用程序在运行时没有崩溃。
导航抽屉片段:
public class NavigationDrawerFragment extends android.support.v4.app.Fragment {
public static final String PREF_FILE_NAME = "testPref";
public static final String KEY_USER_LEARNED_DRAWER = "user_learned_drawer";
private RecyclerView mRecyclerView;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private View containerView;
private Boolean mUserLearnedDrawer;
private Boolean mFromSavedInstanceState;
private DrawerAdapter adapter;
public NavigationDrawerFragment() {
mFromSavedInstanceState = false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserLearnedDrawer = Boolean.valueOf(readFromPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, "false"));
if (savedInstanceState != null){
mFromSavedInstanceState = true;
}
}
@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);
mRecyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
adapter = new DrawerAdapter(getActivity(), getData());
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<DrawerRow> getData(){
List<DrawerRow> data = new ArrayList<>();
int icons[] = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
String titles[] = {"Link 1","Link 2","Link 3","Link 4"};
for (int i = 0 ; i < titles.length && i < icons.length; i++){
DrawerRow current = new DrawerRow();
current.iconId = icons[i];
current.title = titles[i];
data.add(current);
}
return data;
}
public void setUp(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
containerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClosed){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if(!mUserLearnedDrawer){
mUserLearnedDrawer = true;
saveToPreferences(getActivity(),KEY_USER_LEARNED_DRAWER, mUserLearnedDrawer+"");
}
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};
if(!mUserLearnedDrawer && !mFromSavedInstanceState){
mDrawerLayout.openDrawer(containerView);
}
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
}
public void saveToPreferences(Context context, String prefName, String prefValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor mEditor = sharedPreferences.edit();
mEditor.putString(prefName, prefValue);
mEditor.apply();
}
public static String readFromPreferences(Context context, String prefName, String defaultValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(prefName,defaultValue);
}
}
适配器:
public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.MyViewHolder>{
private LayoutInflater inflator;
List<DrawerRow> data = Collections.EMPTY_LIST;
public DrawerAdapter(FragmentActivity activity, List<DrawerRow> data) {
inflator = LayoutInflater.from(activity);
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = inflator.inflate(R.layout.nav_drawer_row , viewGroup,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder viewHolder, int i) {
DrawerRow current = data.get(i);
viewHolder.title.setText(current.title);
viewHolder.icon.setImageResource(current.iconId);
}
@Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
ImageView icon;
TextView title;
public MyViewHolder(View itemView) {
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.list_icon);
title = (TextView) itemView.findViewById(R.id.list_text);
}
}
}
抽屉行:
public class DrawerRow {
int iconId;
String title;
}
版面配置:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout">
<!--Main Content -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text" />
</LinearLayout>
<!--Nav Drawer -->
<fragment
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="uk.co.clickcomputing.hhscweatherstation.NavigationDrawerFragment"
android:id="@+id/fragment_navigation_drawer"/>
</android.support.v4.widget.DrawerLayout>
抽屉行:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:padding="@dimen/list_padding"
android:id="@+id/list_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_launcher"/>
<TextView
android:padding="@dimen/list_padding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="New Text"
android:id="@+id/list_text" />
</LinearLayout>
导航抽屉片段布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_marginBottom="8dp"
android:contentDescription="@string/nav_drawer_img_desciption"
android:scaleType="centerCrop"
android:src="@drawable/nav_drawer" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/drawerList">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
9条答案
按热度按时间1qczuiv01#
问题是您返回的项计数为0,这表示没有要显示的行。您应该返回如下所示的List的大小。
lh80um4z2#
替换以下行
与
zvms9eto3#
我同意你需要返回正确的项目编号,但我认为你可能会有一个问题,设置回收视图的高度为wrap_content以及。我发现this链接很有帮助。希望它能有所帮助。
yquaqz184#
请确保RecyclerView父容器宽度未设置为WrapContent
egmofgnx5#
行中布局高度为
android:layout_height="match_parent"
,但必须为android:layout_height="wrap_content"
lpwwtiir6#
请检查RecyclerView宽度和高度属性,如果它们设置为内容换行,请将其更改为与父视图匹配或在某些特定区域中约束视图还要检查父视图宽度属性,如果它设置为内容换行,请将其更改为与父视图匹配
soat7uwm7#
我已经给出了静态高度和match_parent。它适用于我的示例,我已经在嵌套的recyclerview中添加了它。
xurqigkl8#
如果你正在从网络上获取数据,确保你正在UI线程中重新加载/刷新你的适配器。这是我的解决方案。
s4chpxco9#
您还没有向
getItemCount
方法添加逻辑,它应该如下所示: