1. 实现效果
如果判断当前的属性和上一个属性相同时,不显示编号信息 ,否则就显示
2. 主要是在适配器上面做判断
bean --> Tip
package com.example.timetest; /** * 封装要显示数据的bean ---> Tip * * @author Administrator * */ public class Tip { private String name; private String time; private String day; private String number; public Tip ( ) { super ( ); } public Tip ( String name , String time , String day , String number ) { super ( ); this.name = name; this.time = time; this.day = day; this.number = number; } public String getName ( ) { return name; } public void setName ( String name ) { this.name = name; } public String getTime ( ) { return time; } public void setTime ( String time ) { this.time = time; } public String getDay ( ) { return day; } public void setDay ( String day ) { this.day = day; } public String getNumber ( ) { return number; } public void setNumber ( String number ) { this.number = number; } }
Java
listView = ( ListView ) findViewById ( R.id.listView ); List < Tip > list = new ArrayList < Tip > ( ); list.add ( new Tip ( "1111" , "11:11" , "1" , "111111" ) ); list.add ( new Tip ( "2222" , "22:22" , "1" , "2222" ) ); list.add ( new Tip ( "3333" , "33:33" , "2" , "3333" ) ); list.add ( new Tip ( "4444" , "44:44" , "3" , "4444" ) ); list.add ( new Tip ( "5555" , "55:55" , "3" , "5555" ) ); list.add ( new Tip ( "6666" , "66:66" , "6" , "6666" ) ); adapter = new ListAdapter ( this , list ); listView.setAdapter ( adapter );
3. 适配器
package com.example.timetest; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class ListAdapter extends BaseAdapter { public Context context; private List < Tip > list; public ListAdapter ( Context context , List < Tip > list ) { this.context = context; this.list = list; } @ Override public int getCount ( ) { // TODO Auto-generated method stub return list.size ( ); } @ Override public Tip getItem ( int position ) { // TODO Auto-generated method stub return list.get ( position ); } @ Override public long getItemId ( int position ) { // TODO Auto-generated method stub return position; } @ Override public View getView ( int position , View convertView , ViewGroup parent ) { // TODO Auto-generated method stub ViewHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from ( context ) .inflate ( R.layout.list_item , null ); holder = new ViewHolder ( ); holder.name = ( TextView ) convertView .findViewById ( R.id.name ); holder.time = ( TextView ) convertView .findViewById ( R.id.time ); holder.day = ( TextView ) convertView .findViewById ( R.id.day ); holder.number = ( TextView ) convertView .findViewById ( R.id.number ); convertView.setTag ( holder ); } else { holder = ( ViewHolder ) convertView .getTag ( ); } Tip tip = list.get ( position ); holder.name.setText ( tip.getName ( ) ); holder.time.setText ( tip.getTime ( ) ); holder.day.setText ( tip.getDay ( ) ); holder.number.setText ( tip.getNumber ( ) ); if (position == 0) { } else { if (tip.getDay ( ) .equals ( list.get ( position - 1 ) .getDay ( ) )) { holder.day.setVisibility ( View.GONE ); } else { holder.day.setVisibility ( View.VISIBLE ); } } return convertView; } private class ViewHolder { public TextView name; public TextView time; public TextView day; public TextView number; } }
4. layout
<?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="horizontal" > <!-- day和time 的显示 --> <LinearLayout android:layout_width="wrap_content" android:layout_height="70dp" android:layout_marginLeft="10dp" android:orientation="vertical" > <View android:layout_width="1dp" android:layout_height="15dp" android:layout_marginLeft="15dp" android:background="#c7c7c7" /> <TextView android:id="@+id/day" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:background="#c7c7c7" android:text="今天" /> <View android:layout_width="1dp" android:layout_height="5dp" android:layout_marginLeft="15dp" android:background="#c7c7c7" /> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="12:00" /> <View android:layout_weight="1" android:layout_width="1dp" android:layout_height="15dp" android:layout_marginLeft="15dp" android:background="#c7c7c7" /> </LinearLayout> <!-- 中间部分信息的显示 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_weight="1" android:orientation="vertical" > <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/test" android:textSize="20dp" /> <TextView android:id="@+id/number" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/test" android:textSize="16dp" /> </LinearLayout> <!-- 图片的显示 --> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginRight="10dp" android:src="@drawable/ic_launcher" /> </LinearLayout>
5. 每天进步一点点 嘿嘿 加油
时间: 2024-10-24 15:57:50