I am trying to open the Android sample project - Navigation Drawer in Android Studio, but got error while building it.
These are the steps to reproduce it:
The compiler cannot find this attribute in the activity_navigation_drawer.xml
:
app:layoutManager="LinearLayoutManager"
To fix this
Remove this attribute from the XML so your Recyclerview looks like this:
<android.support.v7.widget.RecyclerView
android:id="@+id/left_drawer"
android:scrollbars="vertical"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:choiceMode="singleChoice"
android:divider="@null"/>
press the Gradle Sync button
In NavigationDrawerActivity
, add the following 2 lines of code in your OnCreate();
LinearLayoutManager mLinearLayoutmanager = new LinearLayoutManager(this);
mDrawerList.setLayoutManager(mLinearLayoutmanager);
So your code now looks like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (RecyclerView) findViewById(R.id.left_drawer);
LinearLayoutManager mLinearLayoutmanager = new LinearLayoutManager(this);
mDrawerList.setLayoutManager(mLinearLayoutmanager);
....
Good luck
Edit:
The reason why this the original xml code is no longer working, is most likely that the xmlns:app="http://schemas.android.com/apk/res-auto"
namespace is no longer supporting the layoutManager
attribute.
More info on the namespace can be found here: https://stackoverflow.com/a/26692768/3708094
Just upgrade the build.gradle file(module) to the newest version, I tried it and everything is OK now.
references:
https://github.com/googlesamples/android-NavigationDrawer/issues/9
Are you sure that gradle resolved all dependencies? I had the same problem when I occasionally removed recyclerview support library from dependencies:
compile 'com.android.support:recyclerview-v7:25.0.1'
You can certainly still define LayoutManagers and more via xml. Here's an example using the native GridLayoutManager:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/posters_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:reverseLayout="false"
app:spanCount="3"
app:stackFromEnd="false" />
If you want to use a custom LayoutManager follow the rules defined here:
R.attr.html#layoutManager