为了达到此效果,我做了很多尝试。
思路一:将tabhost这一layout(叫做A)放到listview 的heardview中,通过调整高度,使他显示出来,这样就达到了在listview中添加tabhost的效果。然后在界面顶部在创建一个和A一样的layout(暂时叫做B),然后监听listview的滑动事件,当firstitem>1时B显示,否则B隐藏。
目的达到了,但是效果不太流畅。
后来在网上看了很多DEMO,发现了一个好的方法
思路二:用framelayout直接在listview布局中添加一个布局,然后导入tabhost模块的view,这样可以很方便的控制它的缩放了
可能没听明白,直接上代码
activity_sticky.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#332b3b"
android:gravity="center"
android:text="标题"
android:textColor="#ffffff" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title" >
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"/>
<LinearLayout
android:id="@+id/invis"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#ccedc7"
android:orientation="horizontal"
>
<!-- 可以替换成tabhost-->
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="悬浮部分" />
</LinearLayout>
</FrameLayout>
</RelativeLayout>
FrameList .java
package com.example.listview;
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
public class FrameList extends Activity{
private LinearLayout invis;
private String [] strs;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sticky);
invis = (LinearLayout) findViewById(R.id.invis);
strs = new String[100];
for (int i = 0; i < 20; i++) {
strs[i] = "data-----" + i;
}
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, strs));
lv.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
if (firstVisibleItem >= 1) {
android.view.ViewGroup.LayoutParams p=invis.getLayoutParams();
p.height = 50;
invis.setLayoutParams(p);
//也可以用缩放动画
// Animation translateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoomin);
// //设置动画时间
// invis.startAnimation(translateAnimation);
} else {
android.view.ViewGroup.LayoutParams p=invis.getLayoutParams();
p.height = 100;
invis.setLayoutParams(p);
//也可以用缩放动画
// Animation translateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoomout);
// //设置动画时间
// invis.startAnimation(translateAnimation);
}
}
});
}
}
上面只是一个demo,可以更具自己的需求去改一下
上个效果图