分类标题可固定的ListView以及点击分类标题收起和展开Item
(item分组可展开和收起的Listview)
1。导入依赖
dependencies {
compile 'se.emilsjolander:stickylistheaders:2.7.0'
}
2.aty xml 中加入
<se.emilsjolander.stickylistheaders.StickyListHeadersListView
android:id="@+id/stickyListHeadersListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
3.list_header.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/id_tv_head_item"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:background="#9f8f62"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@android:color/white"
tools:context=".MainActivity"
/>
4、list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:id="@+id/id_tv_item"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:textSize="18dp"
tools:text="Aardvark"
android:background="#d71f1f"
tools:context=".MainActivity"/>
5.adapter
package com.louisgeek.louisstickylistheadersdemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
/**
* Created by louisgeek on 2016/4/7.
*/
public class MyAdapter extends BaseAdapter implements StickyListHeadersAdapter {
private List<String> listStr=new ArrayList<>();
private LayoutInflater inflater;
private OnMyItemClickListener listener;
public MyAdapter(Context context,List<String> listStr) {
inflater = LayoutInflater.from(context);
this.listStr=listStr;
this.listStr = new ArrayList(listStr);//解决java.lang.UnsupportedOperationException
}
@Override
public int getCount() {
return listStr.size();
}
@Override
public Object getItem(int position) {
return listStr.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.id_tv_item);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onMyItemClick(position,listStr.get(position));
}
});
convertView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
listener.onMyItemLongClick(position, listStr.get(position));
return false;
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(listStr.get(position)+" pos:"+position);
return convertView;
}
@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
HeaderViewHolder holder;
if (convertView == null) {
holder = new HeaderViewHolder();
convertView = inflater.inflate(R.layout.list_header, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.id_tv_head_item);
convertView.setTag(holder);
} else {
holder = (HeaderViewHolder) convertView.getTag();
}
//set header text as first char in name
String headerText = listStr.get(position).subSequence(0, 1).charAt(0)+" pos:" + position;
holder.text.setText(headerText);
return convertView;
}
@Override
public long getHeaderId(int position) {
//return the first character of the country as ID because this is what headers are based upon
return listStr.get(position).subSequence(0, 1).charAt(0);
/* if (position<3){
return 0;
}else if (position<5){
return 1;
}else if (position<8){
return 2;
}else {
return 3;
}*/
}
class HeaderViewHolder {
TextView text;
}
class ViewHolder {
TextView text;
}
/**
* 内部接口回调方法
*/
public interface OnMyItemClickListener {
void onMyItemClick(int position, Object object);
void onMyItemLongClick(int position, Object object);
}
/**
* 设置监听方法
*
* @param listener
*/
public void setOnMyItemClickListener(OnMyItemClickListener listener) {
this.listener = listener;
}
void addItem(){
listStr.add("新增数据");
notifyDataSetChanged();
}
void deleteItem(int position){
listStr.remove(position);
notifyDataSetChanged();
}
}
6.aty code
StickyListHeadersListView stickyList = (StickyListHeadersListView) findViewById(R.id.stickyListHeadersListView);
MyAdapter adapter = new MyAdapter(this);
stickyList.setAdapter(adapter);
7.拓展 点开收起
aty xml 中改成
<se.emilsjolander.stickylistheaders.ExpandableStickyListHeadersListView
android:id="@+id/expandableStickyListHeadersListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
8。aty code
改成
final ExpandableStickyListHeadersListView expandableStickyList = (ExpandableStickyListHeadersListView) findViewById(R.id.expandableStickyListHeadersListView);
StickyListHeadersAdapter stickyListHeadersAdapter = new MyAdapter(this);
expandableStickyList.setAdapter(stickyListHeadersAdapter);
expandableStickyList.setOnHeaderClickListener(new StickyListHeadersListView.OnHeaderClickListener() {
@Override
public void onHeaderClick(StickyListHeadersListView l, View header, int itemPosition, long headerId, boolean currentlySticky) {
if (expandableStickyList.isHeaderCollapsed(headerId)) {
expandableStickyList.expand(headerId);
} else {
expandableStickyList.collapse(headerId);
}
}
});
//ps:expandableStickyList的setOnItemClickListener不能用 github也有人问
final MyAdapter myAdapter = (MyAdapter) stickyListHeadersAdapter;
myAdapter.setOnMyItemClickListener(new MyAdapter.OnMyItemClickListener() {
@Override
public void onMyItemClick(int position, Object object) {
Toast.makeText(MainActivity.this, "Click on item" + position, Toast.LENGTH_SHORT).show();
myAdapter.addItem();
}
@Override
public void onMyItemLongClick(int position, Object object) {
Toast.makeText(MainActivity.this, "Click long on item" + position, Toast.LENGTH_SHORT).show();
myAdapter.deleteItem(position);
}
});
demo:https://github.com/louisgeek/LouisStickyListHeadersDemo