在检索ListView的项目时显示"正在加载..."文本,可以通过以下几个步骤实现:
以下是一个简单的示例代码:
public class MyAdapter extends BaseAdapter {
private List<String> dataList;
private Context context;
private boolean isLoading;
public MyAdapter(Context context, List<String> dataList) {
this.context = context;
this.dataList = dataList;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = convertView.findViewById(R.id.textView);
viewHolder.loadingTextView = convertView.findViewById(R.id.loadingTextView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (isLoading && position == getCount() - 1) {
viewHolder.loadingTextView.setVisibility(View.VISIBLE);
viewHolder.textView.setVisibility(View.GONE);
} else {
viewHolder.loadingTextView.setVisibility(View.GONE);
viewHolder.textView.setVisibility(View.VISIBLE);
viewHolder.textView.setText(dataList.get(position));
}
return convertView;
}
public void setLoading(boolean isLoading) {
this.isLoading = isLoading;
notifyDataSetChanged();
}
static class ViewHolder {
TextView textView;
TextView loadingTextView;
}
}
在数据加载完成后,调用setLoading(false)方法通知适配器数据已更新,刷新ListView。
myAdapter.setLoading(false);
这样就可以在检索ListView的项目时显示"正在加载..."文本了。
领取专属 10元无门槛券
手把手带您无忧上云