回收站视图未显示
Posted
技术标签:
【中文标题】回收站视图未显示【英文标题】:Recycler View Not Showing 【发布时间】:2015-02-26 15:30:41 【问题描述】:我一直在制作一个在导航抽屉中使用回收器视图的应用程序。为什么没有显示回收站视图的内容。视图肯定在那里,因为我可以看到滚动阴影。我不确定我做错了什么,因为应用程序在运行时不会崩溃。
导航抽屉片段:
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_
android:layout_
android:id="@+id/drawerLayout">
<!--Main Content -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<TextView
android:layout_
android:layout_
android:text="New Text" />
</LinearLayout>
<!--Nav Drawer -->
<fragment
android:layout_
android:layout_
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_
android:layout_>
<ImageView
android:padding="@dimen/list_padding"
android:id="@+id/list_icon"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:src="@drawable/ic_launcher"/>
<TextView
android:padding="@dimen/list_padding"
android:layout_
android:layout_
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_
android:layout_
android:background="#eee"
android:orientation="vertical">
<ImageView
android:layout_
android:layout_
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_
android:layout_
android:id="@+id/drawerList">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
【问题讨论】:
能分享一下包含RecyclerView的布局文件吗?我的猜测是您设置了宽度或高度来包装内容,默认布局管理器尚不支持。 @yigit 更新了布局 所以我猜到了,默认布局管理器尚不支持回收器视图上的 android:layout_ 。您可以将其更改为 match_parent 或在布局管理器中覆盖 onMeasure 并自己进行测量。 @yigit 仍然没有显示任何内容 这没有意义。您有可以附加的示例应用程序吗? APK 应该足以调试,但如果您可以附加 w/ 源代码,那就太棒了。 【参考方案1】:问题是您将项目计数返回为 0,这表明没有要显示的行。您应该返回 List 的大小,如下所示。
@Override
public int getItemCount()
return data.size();
【讨论】:
我在更新我的 dataListdataList=getMyItems()
时遇到了这个问题。相反,我使用dataList.addAll(getMyItems());
【参考方案2】:
以下行替换
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
与
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setAdapter(adapter);
【讨论】:
【参考方案3】:我同意您需要返回正确的项目编号,但我认为您在将回收站视图的高度设置为 wrap_content 时也可能存在问题。我发现this 链接很有帮助。希望对您有所帮助。
【讨论】:
【参考方案4】:确保 RecyclerView 父容器宽度未设置为 WrapContent
【讨论】:
就这么简单,很好的答案。我更改为 match_parent 回收器,现在它出现了【参考方案5】:@Override
public int getItemCount()
return data.size();
你所在行的布局高度是android:layout_height="match_parent"
,但必须是android:layout_height="wrap_content"
【讨论】:
【参考方案6】:检查您的 RecyclerView 的宽度和高度属性,如果它们设置为包装内容,请将其更改为匹配父级或限制您在某些特定区域中的视图。 还要检查父视图的宽度属性,如果它设置为包装内容,也将其更改为匹配父视图。
【讨论】:
【参考方案7】:我已经给出了静态高度和 match_parent。它适用于我的示例,我已将其添加到嵌套回收器视图中。
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/new_store_page_phoneNumber_rv"
android:layout_
android:layout_
android:scrollIndicators="bottom"
android:fadeScrollbars="false"
android:layout_gravity="center_vertical"
app:adapter="@adapter"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbars="none"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="horizontal" />
【讨论】:
以上是关于回收站视图未显示的主要内容,如果未能解决你的问题,请参考以下文章