今天學習到了RecyclerView,有人提出讓我在RecyclerView前面添加頭部,之前寫ListView的時候可以添加HeaderView,但是現在顯然沒有了,怎麼辦呢?在網上查了一圈發現都有點複雜,那沒辦法了,慢慢來寫唄,但是我並沒有給頭部添加點擊事件。就是簡單的展示一下怎麼添加頭部。
效果圖
那麼實現這樣的佈局就用到了RecyclerView, RecyclerView的具體創建方法就請大家移步到別的博客啦。
主XML佈局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.practice.chapter8.section3.FloatingSearchActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
RecyclerView中的item佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/frog"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="列表項"
android:paddingLeft="10dp"
android:textColor="#201b1b"
android:shadowColor="#8f8e8e"
android:shadowDx="2"
android:shadowDy="2"
android:textSize="14sp"/>
</LinearLayout>
recyclerView頭部XML佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--搜索框-->
<EditText
android:id="@+id/edit_search"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@null"
android:drawableLeft="@android:drawable/ic_menu_search"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:hint="請輸入關鍵詞查詢!"
android:imeOptions="actionSearch"
android:singleLine="true" />
</LinearLayout>
接下來就是Adapter了:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private List<String> datas = null;
//Type只要不爲0都行,因爲函數默認返回的Type都爲0
private int TYPE_HEADER = 1001;
@NonNull
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (i==TYPE_HEADER){
View headerView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_search_list, viewGroup, false);
return new HeaderViewHolder(headerView);
}
View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_normal_list, viewGroup, false);
return new ViewHolder(inflate);
}
public RecyclerAdapter(List<String> datas) {
this.datas = datas;
}
@Override
public void onBindViewHolder(@NonNull RecyclerAdapter.ViewHolder viewHolder, int i) {
//這裏可以添加點擊事件等,綁定控件
}
@Override
public int getItemCount() {
//加了頭佈局,多一個
return datas.size()+1;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
}
private class HeaderViewHolder extends ViewHolder {
public HeaderViewHolder(View headerView) {
super(headerView);
}
}
@Override
public int getItemViewType(int position) {
//在第一個位置添加頭
if (position==0){
return TYPE_HEADER;
}
return super.getItemViewType(position);
}
}
最後就是啓動程序啦!:
public class RecyclerSearch extends AppCompatActivity {
private List<String> datas = new ArrayList<>();
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private SwipeRefreshLayout swipeRefreshLayout;
private RecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_search);
initView();
}
private void initView() {
recyclerView = findViewById(R.id.recycler_search);
assert recyclerView != null;
initDatas();
layoutManager = new LinearLayoutManager(this);
//設置縱向佈局(默認佈局)
((LinearLayoutManager) layoutManager).setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
//創建並設置Adapter
adapter = new RecyclerAdapter(datas);
recyclerView.setAdapter(adapter);
//下拉刷新
swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);//設置刷新進度條的顏色
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//處理刷新邏輯
refresh();
}
});
}
// 模擬了網絡交互
private void refresh() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();//通知數據變化
swipeRefreshLayout.setRefreshing(false);//停止刷新
}
});
}
}).start();
}
private void initDatas() {
datas.add("阿爾巴尼亞");
datas.add("安道爾");
datas.add("奧地利");
datas.add("白俄羅斯");
datas.add("保加利亞");
datas.add("法國");
datas.add("德國");
datas.add("意大利");
datas.add("葡萄牙");
datas.add("羅馬尼亞");
datas.add("俄羅斯");
datas.add("塞爾維亞");
datas.add("西班牙");
datas.add("英國");
}
}
裏面有些代碼是爲了測試別的數據用的,大家興趣的可以自己再實現相關的功能。其實添加Header的主要核心內容還是讓viewType不同就行了,但是這中添加方式只能添加少量的頭部,多了就要處理很多不同View的響應事件,相對來說Adapter中的代碼會過長。並不是很好理解。