android recycleview怎么添加headerview
1个回答
2016-09-04
展开全部
接下来讲addheaderview最关键的部分了,小伙伴们看好了--
首先添加依赖库:
compile 'com.bartoszlipinski.recyclerviewheader:library:1.2.0'
compile 'com.android.support:recyclerview-v7:23.0.1'
接下来实现布局文件编写:<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|top" />
<com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_horizontal|top">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher"/>
</com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader>
</FrameLayout>最后只需要在代码中设置完adapter之后,调用attachto方法即可实现了核心代码就这么点:header = (RecyclerViewHeader) findViewById(R.id.header);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
mAdapter = new MyAdapter(this, mDatas, R.layout.template_text);
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
header.attachTo(recyclerView, true);
首先添加依赖库:
compile 'com.bartoszlipinski.recyclerviewheader:library:1.2.0'
compile 'com.android.support:recyclerview-v7:23.0.1'
接下来实现布局文件编写:<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|top" />
<com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_horizontal|top">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher"/>
</com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader>
</FrameLayout>最后只需要在代码中设置完adapter之后,调用attachto方法即可实现了核心代码就这么点:header = (RecyclerViewHeader) findViewById(R.id.header);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
mAdapter = new MyAdapter(this, mDatas, R.layout.template_text);
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
header.attachTo(recyclerView, true);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询