我有一个Android应用,可从Firebase数据库返回足球装备的ListView
。该列表目前可以使用,但我想改进UI。下图显示了我所拥有的以及我想要实现的目标;
在左侧,我有一个布局xml,它循环遍历记录并显示每条记录的日期,但是我想做的是返回一个列表,如果它们相同,则显示1号以下的灯具,如果不相同,则显示在新日期的下方。日期(如右侧)。
下面是我的xml的布局;
为了让您对代码有所了解(请问您是否要查看代码,我最近在张贴50行代码时被打下了标记);
<TextView
android:id="@+id/txtDate"
android:layout_width="0dp"
android:layout_height="24dp"
android:background="#ff0000"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:textColor="@color/cardview_light_background"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="dateofmatch" />
日期红色标题位于listview xml内。
我猜想这是一个递归函数,它计算出日期的所有子项,然后绘制出列表。
我的问题是,您会推荐哪种方法,因为我不想降低性能,而我在2-3种可以做到这一点的方法之间陷入了困境。
编辑:ListView的适配器
public class matchList extends ArrayAdapter<matches> {
private Activity context;
private List<matches> matchList;
public matchList(Activity context, List<matches> matcheslist) {
super(context, R.layout.match_layout, matcheslist);
this.context = context;
this.matchList = matcheslist;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.match_layout, null, true);
TextView textViewDate = (TextView) listViewItem.findViewById(R.id.txtDate);
TextView textGameID = (TextView) listViewItem.findViewById(R.id.txtGameID);
TextView textViewTime = (TextView) listViewItem.findViewById(R.id.txtTime);
TextView textViewHomeTeam = (TextView) listViewItem.findViewById(R.id.txtAction);
TextView textViewAwayTeam = (TextView) listViewItem.findViewById(R.id.txtAwayTeam);
TextView textViewPitchNo = (TextView) listViewItem.findViewById(R.id.txtPitch);
ImageView imgLiveIco = (ImageView) listViewItem.findViewById(R.id.imgLive);
matches match = matchList.get(position);
textViewDate.setText(match.getDate());
textGameID.setText(match.getGameID());
textViewHomeTeam.setText(match.getHomeTeam());
textViewAwayTeam.setText(match.getAwayTeam());
textViewTime.setText(match.getTime());
String fdate = helper.dateFormater(match.getDate() , "EE dd MMM yyyy" , "dd/MM/yyyy");
textViewDate.setText(fdate);
textViewPitchNo.setText("Pitch " + match.getPitch().toString());
if(match.getPlayed() == 1) {
imgLiveIco.setVisibility(View.VISIBLE);
} else {
imgLiveIco.setVisibility(View.INVISIBLE);
}
return listViewItem;
}
}
最佳答案
为了实现所需的功能,建议您使用简单的if语句。从您的代码中我无法理解的是dateofmatch
是否位于整个项目视图中。如果在该视图之外,则需要使用findViewById()
方法找到该视图,并使用if语句,如下所示:
if (dateOfPreviousMatch.equals(dateOfMath)) {
//set visibility of dateofmatch to GONE
} else {
//set visibility of dateofmatch to VISIBLE
}