listview固定头标

来源:互联网 发布:windows一键还原v2.0 编辑:程序博客网 时间:2024/05/06 18:44

listview固定头标

由于近期需要这个listview固定头部

原来很简单就是ListView增加一个headView头部然后根据滑动的距离判断是否显示隐藏了的按钮

效果图

接下来就是显示代码了首先是布局

主布局

 <?xml version="1.0" encoding="utf-8"?><FrameLayout 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=".MainActivity">    <LinearLayout        android:id="@+id/contentss"        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <ListView            android:id="@+id/main_list_view"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:background="@color/material_deep_teal_200" />    </LinearLayout>    <LinearLayout        android:id="@+id/main_section_b_outside"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:visibility="gone">        <include            layout="@layout/main_section_b"            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout></FrameLayout>

第二个布局也就是headView

    <?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:background="@color/material_blue_grey_800"            android:clickable="true"            android:id="@+id/abc"            android:layout_width="match_parent"            android:layout_height="300dp"            android:textSize="20dp"            android:text="hi"/>    <include        android:id="@+id/main_section_b_inside"        android:layout_width="match_parent"        android:layout_height="wrap_content"        layout="@layout/main_section_b"/></LinearLayout>

第三个布局是那个按钮

        <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:layout_weight="1"        android:id="@+id/first"        android:text="diyige"        android:layout_width="wrap_content"        android:layout_height="100dp" />    <Button        android:layout_weight="1"        android:text="dierge"        android:id="@+id/two"        android:layout_width="wrap_content"        android:layout_height="100dp" /></LinearLayout>

接下里就是编写逻辑

        public class MainActivity2 extends Activity implements View.OnClickListener{        private ListView listView;        private LinearLayout sectionB;        private int aHeight;        private View headerView;        private FragmentManager fr;        private act1 act1;        private act2 act2;        private Boolean isfrist = true;    @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);            sectionB = (LinearLayout) findViewById(R.id.main_section_b_outside);            headerView = LayoutInflater.from(this).inflate(R.layout.main_header,null);            Button first = (Button) findViewById(R.id.first);            Button two = (Button) findViewById(R.id.two);            first.setOnClickListener(this);            two.setOnClickListener(this);            fr = getFragmentManager();            act1 = new act1();            act2 = new act2();            initListView(headerView);        }        private void initListView(View headerView){            listView = (ListView) findViewById(R.id.main_list_view);            listView.addHeaderView(headerView, null, true);            ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1);            for (int i = 0; i<100; i++){                adapter.add("item "+String.valueOf(i));            }            listView.setAdapter(adapter);            listView.setOnScrollListener(new AbsListView.OnScrollListener() {                @Override                public void onScrollStateChanged(AbsListView view, int scrollState) {                }                @Override                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {                    if (isfrist) {                        if (listView.getChildAt(0) == null) {                            isfrist = true;                        } else {                            getmyselfheight(listView.getChildAt(0));                            isfrist = false;                        }                    }                    if (getScrollY() >= aHeight) {                        if (sectionB.getVisibility() == View.GONE) {                            sectionB.setVisibility(View.VISIBLE);                        }                    } else if (getScrollY() < aHeight) {                        if (sectionB.getVisibility() == View.VISIBLE) {                            sectionB.setVisibility(View.GONE);                        }                    }                }            });        }    //    获取head的高度    public void getmyselfheight(View v){        View vx = v.findViewById(R.id.main_section_b_inside);        aHeight = v.getTop()+v.getHeight()-vx.getHeight();    }        //获取滚动距离        public int getScrollY() {            View c = listView.getChildAt(0);            if (c == null) {                return 0;            }            int firstVisiblePosition = listView.getFirstVisiblePosition();            int top = c.getTop();            int headerHeight = 0;            Log.d("test",top+"top");            Log.d("test",-top +  c.getHeight() + headerHeight+"");            Log.d("test", top + "top"+aHeight+"seleteHeight"+"headerHeight"+headerHeight);            if (firstVisiblePosition >= 1) {                headerHeight = listView.getHeight();            }            return -top +  firstVisiblePosition*c.getHeight() + headerHeight;        }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.first:                ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1);                for (int i = 0; i<100; i++){                    adapter.add("itemact1 "+String.valueOf(i));                }                adapter.notifyDataSetChanged();                listView.setAdapter(adapter);                break;            case R.id.two:                ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1);                for (int i = 0; i<100; i++){                    adapter1.add("itemact2 "+String.valueOf(i));                }                adapter1.notifyDataSetChanged();                listView.setAdapter(adapter1);                break;        }    }    @Override    protected void onStart() {        TextView tv = (TextView) findViewById(R.id.abc);        Button first = (Button) findViewById(R.id.first);        Button two = (Button) findViewById(R.id.two);        tv.setOnClickListener(this);        first.setOnClickListener(this);        two.setOnClickListener(this);        super.onStart();    }}

这里写得比较乱 但是将就着吧

以下说几个关键点就是listView的getScrollY是一个空的方法需要我们自己去写逻辑然后获取当前滑动的距离

看了网上的一些资料,才知道ListView没有提供得到滚动高度的任何方法,必须自己根据getChildAt(0).top和getFirstVisiblePosition()来综合计算获得。

代码如下:

public int getScrollY() {    View c = mListView.getChildAt(0);    if (c == null) {        return 0;    }    int firstVisiblePosition = mListView.getFirstVisiblePosition();    int top = c.getTop();    return -top + firstVisiblePosition * c.getHeight() ;

但是我们需要将其加工需要隐藏和显示按钮

//获取滚动距离

        public int getScrollY() {            View c = listView.getChildAt(0);            if (c == null) {                return 0;            }            int firstVisiblePosition = listView.getFirstVisiblePosition();            int top = c.getTop();            int headerHeight = 0;            if (firstVisiblePosition >= 1) {                headerHeight = listView.getHeight();            }            return -top +  firstVisiblePosition*c.getHeight() + headerHeight;        }

之后就是获取两组按钮的点击事件问题
因为一个在主的布局第一个布局另一个在第二个布局那么注定了不能将他们一次过设置点击事件需要分开进行设置点击事件

然后就是获取headview的高度获取listview的第0个item所以怪不得listview的item下标都是从1开始

  //    获取head的高度    public void getmyselfheight(View v){        View vx = v.findViewById(R.id.main_section_b_inside);        aHeight = v.getTop()+v.getHeight()-vx.getHeight();    }

最后就是出现效果啦 希望对你有帮助

1 0
原创粉丝点击