zoukankan      html  css  js  c++  java
  • ListView 实现分组

    1:FragmentHack4.java

    /**
     * Created by y on 15-1-2.
     */
    public class FragmentHack4 extends Fragment{
    
        View view;
        List<String> names;
    
        ListView lvNames;
        NameSectionAdapter adapter;
        TextView tvHeader;
        int topVisiblePos;
    
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
    
            names = new ArrayList<String>();
    
            //测试数据
            String []t={"A","B","C","D","E","F"};
            for(int i=0;i<t.length;i++){
                for(int j=0;j<10;j++){
                    names.add(t[i]+"00"+j);
                }
            }
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.fragment_hack4, container,false);
    
            tvHeader = (TextView)view.findViewById(R.id.tvHeader);
            lvNames = (ListView)view.findViewById(R.id.lvNames);
    
            adapter = new NameSectionAdapter(getActivity(),R.layout.list_name_item,names);
            lvNames.setAdapter(adapter);
    
            //设置第一个分组栏数据
            setTopHeader(0);
    
            lvNames.setOnScrollListener( lvOnScrollListener());
    
    
            return view;
        }
    
        AbsListView.OnScrollListener lvOnScrollListener(){
            return new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView absListView, int i) {
    
                }
    
                @Override
                public void onScroll(AbsListView absListView, int i, int i2, int i3) {
    
                    if(i!=topVisiblePos){
    
                        topVisiblePos = i;
    
                        setTopHeader(i);
                    }
                }
            };
        }
    
    
        private void setTopHeader(int pos){
            tvHeader.setText(names.get(pos).substring(0,1));
        }
    }

    2:NameSectionAdapter.java

    /**
     * Created by y on 15-1-2.
     */
    public class NameSectionAdapter extends ArrayAdapter<String>{
        private int resourceId;
    
        public NameSectionAdapter(Context context, int resourceId, List<String> objects){
            super(context,resourceId,objects);
    
            this.resourceId = resourceId;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            String name = getItem(position);
            ViewHolder holder;
    
            if(convertView==null){
                convertView = LayoutInflater.from(getContext()).inflate(resourceId,null);
                holder = new ViewHolder();
    
                holder.tvHeader = (TextView)convertView.findViewById(R.id.tvHeader);
                holder.tvName = (TextView)convertView.findViewById(R.id.tvName);
    
               convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();
            }
    
            holder.tvName.setText(name);
    
            //如果是第一项或者首字母发生了变化,则显示分组栏,否则隐藏
            if(position==0 || getItem(position-1).charAt(0)!=name.charAt(0)){
                holder.tvHeader.setVisibility(View.VISIBLE);
                holder.tvHeader.setText(name.substring(0,1));
            }else{
                holder.tvHeader.setVisibility(View.GONE);
            }
    
            return convertView;
        }
    
        static class ViewHolder{
            public TextView tvHeader;
            public TextView tvName;
        }
    }

    3:布局文件:

    (1)fragment_hack4.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ListView
            android:id="@+id/lvNames"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
        <include layout="@layout/list_header"/>
    </FrameLayout>

    (2)list_header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tvHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0000ff"
        android:textColor="#ffffff"
        style="@android:style/TextAppearance.Large"
        android:textStyle="bold"/>

    (3)list_name_item.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="wrap_content"
        android:orientation="vertical">
    
        <include layout="@layout/list_header"/>
    
        <TextView
            android:id="@+id/tvName"
            style="@android:style/TextAppearance.Small"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    4:运行结果

  • 相关阅读:
    linux下实现nginx反向代理
    linux下实现nginx安装实现端口区分,域名区分
    redis整合spring
    linux下安装redis以及redis集群
    Kindeditor编辑器
    ActiveMQ
    redis(保存邮件激活码)
    AngularJS (验证码倒计时)
    linux部署项目(oracle+redis+activeMQ+elasticSearch+tomcat+JDK8)
    WebService
  • 原文地址:https://www.cnblogs.com/yshyee/p/4199135.html
Copyright © 2011-2022 走看看