listview 顶部添加tabhost,list滑动时tabhost可以悬浮、缩放的效果

为了达到此效果,我做了很多尝试。

   思路一:将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,可以更具自己的需求去改一下

上个效果图




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值