关于分组ListView的示例,如下效果:

Android 分组ListView 案例_java

按年月进行分组排序。

1、Activity:

package com.gshop.mine.mypoints;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.gbi.adapter.WealthAdapter;
import com.gbi.bean.WealthBean;
import com.gbi.customview.Title;
import com.gbi.utils.DateUtils;
import com.gbi.yfshop.BaseActivity;
import com.gbi.yfshop.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class WealthActivity extends BaseActivity implements View.OnClickListener {

private ListView lv_wealth;
private List<WealthBean> datas;
private WealthAdapter wealthAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wealth);
initView();
}

private void initView(){
datas = new ArrayList<>();
datas.add(new WealthBean(1,"2022-06-10 10:10:22"));
datas.add(new WealthBean(2,"2022-06-12 10:10:26"));
datas.add(new WealthBean(3,"2022-07-10 10:10:28"));
datas.add(new WealthBean(4,"2022-07-12 10:10:30"));
datas.add(new WealthBean(5,"2022-08-10 10:10:32"));
datas.add(new WealthBean(6,"2022-08-12 10:10:36"));
datas.add(new WealthBean(7,"2022-09-10 10:10:38"));
datas.add(new WealthBean(8,"2022-09-12 10:10:50"));

Collections.sort(datas, new Comparator<WealthBean>(){

@Override
public int compare(WealthBean o1, WealthBean o2) {
Date d1 = DateUtils.stringToDate(o1.getBuyDate());
Date d2 = DateUtils.stringToDate(o2.getBuyDate());
return d2.compareTo(d1);
}
});

List tags = getTag();
lv_wealth = findViewById(R.id.lv_wealth);
wealthAdapter = new WealthAdapter(this,datas,tags);
wealthAdapter.setDataList(datas);
lv_wealth.setAdapter(wealthAdapter);
wealthAdapter.notifyDataSetChanged();
}

private List getTag(){
List result = new ArrayList<>();
for (WealthBean w: datas){
if(!result.contains(DateUtils.stringToDateStr(w.getBuyDate()))){
result.add(DateUtils.stringToDateStr(w.getBuyDate()));
}
}
return result;
}

@Override
public void onClick(View v) {

}
}

2、WealthAdapter:

package com.gbi.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gbi.bean.WealthBean;
import com.gbi.utils.DateUtils;
import com.gbi.yfshop.R;

import java.util.List;

public class WealthAdapter extends ArrayAdapter {

private Context mContext;
private List<WealthBean> mList;
private OnClickedListener mListener;
private List listTag = null;

public WealthAdapter(@NonNull Context context, int resource) {
super(context, resource);
}

public WealthAdapter(Context context, List<WealthBean> list, List tags) {
super(context, 0, list);
mContext = context;
mList = list;
this.listTag = tags;
}

public interface OnClickedListener{
void onCliked(int id);
}

public void setmListener(OnClickedListener mListener) {
this.mListener = mListener;
}

public void setDataList(List<WealthBean> list){
mList = list;
}

@Override
public boolean isEnabled(int position) {

if(position==0){
return true;
}
String preDate = DateUtils.stringToDateStr(getItem(position - 1).getBuyDate());
String currentDate = DateUtils.stringToDateStr(getItem(position).getBuyDate());
if(preDate.equals(currentDate)){
return false;
}
return super.isEnabled(position);
}

@Override
public int getCount() {
return mList.size();
}

@Override
public WealthBean getItem(int position) {
return mList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null) {
convertView = View.inflate(mContext, R.layout.item_wealth,null);
holder = initView(convertView);
}else{
holder = (ViewHolder) convertView.getTag();
}
WealthBean bean = mList.get(position);
boolean isEnabled = isEnabled(position);
String buyDate = DateUtils.stringToDateStr(bean.getBuyDate());
Log.i("test","-----"+buyDate);
if(isEnabled){
holder.tv_buyDate.setText(buyDate);
holder.tv_buyDate.setVisibility(View.VISIBLE);
}else{
holder.tv_buyDate.setVisibility(View.GONE);
}
holder.tv_detail_buyDate.setText(bean.getBuyDate());
convertView.setTag(holder);
return convertView;
}
private ViewHolder initView(View v){
ViewHolder holder = new ViewHolder();
holder.tv_buyDate = v.findViewById(R.id.tv_buyDate);
holder.tv_detail_buyDate = v.findViewById(R.id.tv_detail_buyDate);
return holder;
}

class ViewHolder {
TextView tv_buyDate;
TextView tv_detail_buyDate;
}
}

3、WealthBean

package com.gbi.bean;

public class WealthBean {
private int id;
private String buyDate;

public WealthBean() {
}

public WealthBean(int id, String buyDate) {
this.id = id;
this.buyDate = buyDate;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getBuyDate() {
return buyDate;
}

public void setBuyDate(String buyDate) {
this.buyDate = buyDate;
}
}

4、DateUtils

package com.gbi.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
public static String stringToDateStr(String dateString) {
String result = "";
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date dateValue = sdf.parse(dateString);
result = sdf.format(dateValue);
return result;
}catch (Exception exception){
exception.getMessage();
}
return result;
}

public static Date stringToDate(String dateString) {
Date result = new Date();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
result = sdf.parse(dateString);

return result;
}catch (Exception exception){
exception.getMessage();
}
return result;
}
}

5、activity_wealth.xml

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

6、item_wealth.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">
<TextView
android:id="@+id/tv_buyDate"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="2022-10-10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"></TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:background="@color/white"
android:padding="10dp">
<LinearLayout
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购买商品 皇室仙草 皇室仙草"></TextView>
<TextView
android:id="@+id/tv_detail_buyDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2022-10-10 10:10:10"></TextView>
</LinearLayout>
<TextView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+300"
android:textColor="@color/red_color"
></TextView>
</LinearLayout>
</LinearLayout>