Android 固定頭部的ListView以及下拉重新整理(附原始碼)
阿新 • • 發佈:2019-02-02
先上圖
前言:由於這段時間專案中需要用到這個功能,所以在此小計,以便以後方便檢視
實現的原理很簡單,就是給ListView設定一個HeanderView,當ListView資料第一行在頂部的時候,將HeanderView檢視顯示就行。
所以我們需要建立倆個HanderView的xml
第一個HeaderView佈局listview_headerview1.xml
第二個HeaderView佈局listview_headerview2.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:layout_width="match_parent" android:layout_height="240dp" android:background="@drawable/image" android:scaleType="fitXY" /> </LinearLayout>
<?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="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="@android:color/darker_gray" android:text="好帥" android:gravity="center_vertical" /> </LinearLayout>
則主佈局程式碼如下
<?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" android:fitsSystemWindows="true" tools:context="com.pengguichu.testlistheaderview.MainActivity"> <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content"></com.handmark.pulltorefresh.library.PullToRefreshListView> <include android:id="@+id/listview_headerview2" layout="@layout/listview_headerview2"></include> </RelativeLayout>
PS:主佈局需要使用RL佈局,不然往上滑動時,會影響視覺效果 。
接下來就是邏輯程式碼。
思路如下:給ListView設定倆個HeaderView,--------->當滑動ListView到頂部時顯示固定頭部檢視,否則就隱藏。這裡需要注意的是,PullToRefreshListView是一個重寫的LinearLayout,所以我們需要獲取他內部巢狀的ListView,程式碼如下
ListView listView = pullToRefreshListView.getRefreshableView();
當然你可以將PullToRefreshListView換成ListView,則不需要上面的程式碼
新增HeaderView程式碼
listView.addHeaderView(headerview1);
listView.addHeaderView(headerview2);
拖動ListView判斷程式碼
pullToRefreshListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
headerview_in.setVisibility(firstVisibleItem >=2 ? View.VISIBLE : View.GONE);
}
});
下拉重新整理以及全部程式碼我貼在下面,可以自己好好研究,不懂可以留言或者下載原始碼(原始碼在部落格最後)
package com.pengguichu.testlistheaderview;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private PullToRefreshListView pullToRefreshListView;
private View headerview_in;
private View headerview1;
private View headerview2;
private List<String> data;
private int a=10;
private int b;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = new ArrayList<>();
headerview_in = findViewById(R.id.listview_headerview2);
pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.listview);
headerview1 = View.inflate(this, R.layout.listview_headerview1, null);
headerview2 = View.inflate(this, R.layout.listview_headerview2, null);
ListView listView = pullToRefreshListView.getRefreshableView();
listView.addHeaderView(headerview1);
listView.addHeaderView(headerview2);
pullToRefreshListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
headerview_in.setVisibility(firstVisibleItem >=2 ? View.VISIBLE : View.GONE);
}
});
pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
b=0;
a=10;
data.clear();
initData();
myAdapter.initData(data);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
}
}).start();
}
});
pullToRefreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
b=a;
a=a+10;
initData();
myAdapter.initData(data);
}
});
initData();
myAdapter = new MyAdapter(data,getApplicationContext());
pullToRefreshListView.setAdapter(myAdapter);
}
//重新整理資料
private void initData(){
for(int i=b;i<a;i++){
data.add("帥"+i);
};
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
pullToRefreshListView.onRefreshComplete();
}
};
}
原始碼下載點選這裡
----------------------不合格的程式狗