ListView分组显示,设置悬浮条

  • 最近好忙,没时间看书,没时间写博客。最近 做了个listview分组显示的东西,发现ios那边的ListView 头可以

  • 悬浮显示,然后我就不服了,自己实现了一下,分享出来,反正也不难。 首选
    是分组显示,应ios那边的要求。json字符串做成了二维数组的形式。java解析出来就是

List<Map<String,Object>>

这样一个鬼东西。
分组的主要在于adapter. 这里做了两个item布局,一个是group头的,一个是每一列的。如下

ListView分组显示,设置悬浮条_第1张图片

group_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fafcfd"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/reserveDateLayout"
        android:layout_width="fill_parent"
        android:layout_height="24dp"
        android:background="@android:color/darker_gray">

        <TextView
            android:id="@+id/tvDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:text="123"
            android:textColor="#000000"
            android:textSize="20dp"
            android:textStyle="bold" />
    RelativeLayout>

    <LinearLayout
        android:id="@+id/groupLayout"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">
    LinearLayout>
LinearLayout>
col_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvContent"
        android:textSize="16dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:text="123"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@android:color/darker_gray" />
LinearLayout>

下面看看adapter

public class MyAdapter extends BaseAdapter {

    private LayoutInflater inflater;

    public MyAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            holder = new Holder();
            convertView = inflater.inflate(R.layout.group_item, null);
            holder.groupLayout = (LinearLayout) convertView.findViewById(R.id.groupLayout);
            holder.tvDate = (TextView) convertView.findViewById(R.id.tvDate);
            convertView.setTag(holder);
        }else {
            holder = (Holder) convertView.getTag();
            holder.groupLayout.removeAllViews(); //一定要清空
        }

        Map listItem = list.get(position);
        String date = listItem.get("date").toString();
        holder.tvDate.setText(date);

        List itemList = (List) listItem.get("col");
        for (String item : itemList) {
            View itemView = inflater.inflate(R.layout.col_item, null);
            TextView tvContent = (TextView) itemView.findViewById(R.id.tvContent);
            tvContent.setText(item);
            tvContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, ((TextView) v).getText().toString(), 0).show();
                }
            });
            holder.groupLayout.addView(itemView);
        }
        return convertView;
    }
}

public class Holder {
    private LinearLayout groupLayout;
    private TextView tvDate;
}

以上就是分组的实现 。这样每次里面item都得重新初始化,并没有复用到。下面将实现头标题的悬浮显示。
1.改动activity布局文件,用FrameLayout把ListView包起来,并把头的部分加起来
2. 初始化数据后 把头显示出来,并设置显示的内容
3. 设置ListView的onScrollListener, 在onScroll方法中更新悬浮条

下面给出代码

actvity_main.xml

<RelativeLayout 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="mytest.jiang.wei.remoteview.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">ListView>

        <RelativeLayout
            android:visibility="gone"
            android:id="@+id/floatGroupLayout"
            android:layout_width="fill_parent"
            android:layout_height="24dp"
            android:background="@android:color/darker_gray">

            <TextView
                android:id="@+id/tvDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"
                android:text="123"
                android:textColor="#000000"
                android:textSize="20dp"
                android:textStyle="bold" />
        RelativeLayout>

    FrameLayout>
RelativeLayout>

下面是完整的Activity代码

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private RelativeLayout floatGroupLayout;
    private List> list;
    private TextView tvDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        floatGroupLayout = (RelativeLayout) findViewById(R.id.floatGroupLayout);
        tvDate = (TextView) floatGroupLayout.findViewById(R.id.tvDate);
        listView = (ListView) findViewById(R.id.listView);
        initData();


        listView.setAdapter(new MyAdapter(this));
        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 (totalItemCount > 0) {
                    tvDate.setText(list.get(firstVisibleItem).get("date").toString());
                }
            }
        });

    }

    private void initData() {
        list = new ArrayList>();

        for (int i = 0; i < 10; i ++) {
            Map map = new HashMap();
            map.put("date", "2015-12-1" + i);
            List colList = new ArrayList();
            for (int j = 0; j < 10; j++) {
                colList.add("第" + i + "组,第" + j + "条数据");
            }
            map.put("col", colList);
            list.add(map);
        }

        //要放在初始后
        floatGroupLayout.setVisibility(View.VISIBLE);
        if (list.size() > 0) {
            tvDate.setText(list.get(0).get("date").toString());
        }
    }


    public class MyAdapter extends BaseAdapter {

        private LayoutInflater inflater;

        public MyAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                holder = new Holder();
                convertView = inflater.inflate(R.layout.group_item, null);
                holder.groupLayout = (LinearLayout) convertView.findViewById(R.id.groupLayout);
                holder.tvDate = (TextView) convertView.findViewById(R.id.tvDate);
                convertView.setTag(holder);
            }else {
                holder = (Holder) convertView.getTag();
                holder.groupLayout.removeAllViews(); //一定要清空
            }

            Map listItem = list.get(position);
            String date = listItem.get("date").toString();
            holder.tvDate.setText(date);

            List itemList = (List) listItem.get("col");
            for (String item : itemList) {
                View itemView = inflater.inflate(R.layout.col_item, null);
                TextView tvContent = (TextView) itemView.findViewById(R.id.tvContent);
                tvContent.setText(item);
                tvContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, ((TextView) v).getText().toString(), 0).show();
                    }
                });
                holder.groupLayout.addView(itemView);
            }
            return convertView;
        }
    }

    public class Holder {
        private LinearLayout groupLayout;
        private TextView tvDate;
    }
}

你可能感兴趣的:(android控件)