filter_image.gif
PullToRefreshListView實現到頂部懸浮效果,這里主要是在ListView的addHeaderView里實現的,后面會講在item里實現,以下是主要代碼:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private PullToRefreshListView pullToRefreshListView;
private List<String> dataList = new ArrayList<>();
private FilterAdapter filterAdapter;
private View headerView;
private View headerView2;
private View filterView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
filterView = findViewById(R.id.filterView);
pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pullToRefreshListView);
//第一個headerView 這里加載美女圖片的地方
headerView = LayoutInflater.from(this).inflate(R.layout.item_head_filter, null);
//setLayoutParams防止PullToRefreshListView報Caused by: java.lang.ClassCastException
AbsListView.LayoutParams headerLayoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
headerView.setLayoutParams(headerLayoutParams);
//第二個headerView 這里到頂部后
AbsListView.LayoutParams rgLayoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
headerView2 = LayoutInflater.from(this).inflate(R.layout.layout_filter, null);
headerView2.setLayoutParams(rgLayoutParams);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ListView tempView = pullToRefreshListView.getRefreshableView();
//添加HeaderView
tempView.addHeaderView(headerView);
tempView.addHeaderView(headerView2);
}
}, 0);
pullToRefreshListView.getRefreshableView().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 到頂部時 懸浮tab出現
if (firstVisibleItem < 2) {
filterView.setVisibility(View.GONE);
} else {
filterView.setVisibility(View.VISIBLE);
}
}
});
for (int i = 0; i < 20; i++) {
dataList.add("標題:" + i);
}
filterAdapter = new FilterAdapter(this, dataList);
ListView actualListView = pullToRefreshListView.getRefreshableView();
actualListView.setAdapter(filterAdapter);
pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
}
class FilterAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<String> dataList;
public FilterAdapter(Context context, List<String> dataList) {
this.mInflater = LayoutInflater.from(context);
this.dataList = dataList;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int i) {
return dataList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(android.R.id.text1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(dataList.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.flobberworm.demo.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:lines="1"
android:text="@string/app_name"
android:textSize="20sp" />
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pullToRefreshListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:dividerHeight="0.5dp"
android:paddingTop="0.5dp"
app:ptrDrawable="@mipmap/ic_launcher"
app:ptrRefreshableViewBackground="#ffffff"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<include
android:id="@+id/filterView"
layout="@layout/layout_filter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
注意:因為ListView中的headView和懸浮在頂部的是兩個不同的View,所以在點擊時需要做狀態同步