我使用的是ListView,但加载太慢,我需要它加载速度更快,不知道如何让适配器逐个加载每个列表视图,是否可能? 这是我的代码:
ArrayList<String> myTripOrigin = new ArrayList<>();
ArrayList<String> myTripDestiny = new ArrayList<>();
ArrayList<String> myTripHour = new ArrayList<>();
ArrayList <boolean[]> myTripSchedule = new ArrayList<>();
ArrayList<Boolean> myTripBools = new ArrayList<Boolean>();
for(int i = 0; i<response.body().size();i++)
{
myTripBools.add(response.body().get(i).isRol());
myTripOrigin.add(getAdress(new LatLng(Double.parseDouble(response.body().get(i).getOrigin_lat()),Double.parseDouble(response.body().get(i).getOrigin_lng()))));
myTripDestiny.add(getAdress(new LatLng(Double.parseDouble(response.body().get(i).getDestiny_lat()),Double.parseDouble(response.body().get(i).getDestiny_lng()))));
Date date = new Date((Long.parseLong(response.body().get(i).getArrival_time()))*1000L);
DateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss aa");
format.setTimeZone(TimeZone.getDefault());
String formatted = format.format(date);
myTripHour.add(formatted);
}
//Send the array to constructor
ListAdapter userAdapter = new CustomAdapterRoute(getApplicationContext(), myTripOrigin,myTripDestiny,myTripHour,myTripSchedule,myTripBools);
ListView userListView = (ListView) findViewById(R.id.listViewRoute);
userListView.setAdapter(userAdapter);
}
这是自定义适配器路由:
public class CustomAdapterRoute extends ArrayAdapter<String>{
private ArrayList<String> itemFrom = new ArrayList<>();
private ArrayList<String> itemTo = new ArrayList<>();
private ArrayList<String> itemHour = new ArrayList<>();
private ArrayList<boolean[]> itemSchedule = new ArrayList<>();
private ArrayList<Boolean> itemRole = new ArrayList<>();
//Send arrays
public CustomAdapterRoute(Context context, ArrayList<String> stringArray, ArrayList<String> stringTo, ArrayList<String> stringHour, ArrayList<boolean[]> stringSchedule,ArrayList<Boolean> boolRole){
super(context, R.layout.custom_row_route,stringArray);
//save the arrays in global var
this.itemFrom = stringArray;
this.itemTo = stringTo;
this.itemHour = stringHour;
this.itemSchedule = stringSchedule;
this.itemRole = boolRole;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater customInflater = LayoutInflater.from(getContext());
final View customView = customInflater.inflate(R.layout.custom_row_route, parent, false);
String singleFrom = itemFrom.get(position);
String singleTo = itemTo.get(position);
String singleHour = itemHour.get(position);
boolean singleRole = itemRole.get(position);
//Get a string chain from the booleans routine from each user
//String singleSchedule = ScheduleText(itemSchedule.get(position));
//Get TextView from the custom row
TextView customText = (TextView) customView.findViewById(R.id.fromText);
TextView customTextTo = (TextView) customView.findViewById(R.id.toText);
TextView customTextHour = (TextView) customView.findViewById(R.id.hourText);
ImageView RoleImage = (ImageView) customView.findViewById(R.id.ImageRole);
//TextView customTextSchedule = (TextView) customView.findViewById(R.id.repeatText);
//set the image of the role
if(singleRole) {RoleImage.setImageResource(R.mipmap.steer3);}
else{ RoleImage.setImageResource(R.mipmap.hand3);}
//Give values from each position of the array
customText.setText(singleFrom);
customTextTo.setText(singleTo);
customTextHour.setText(singleHour);
//User want to edit a route
ImageButton buttonEditTrip = (ImageButton) customView.findViewById(R.id.ImageRole);
return customView;
}
}
答案 0 :(得分:3)
就像我想的那样。你应该使用持有人模式:http://developer.android.com/training/improving-layouts/smooth-scrolling.html使其更快
答案 1 :(得分:1)
在第一段代码中,您一直在调用response.body()
(为什么不将主体变为变量并重新使用它?)。
然后在适配器中,您没有重用convertView
:
View customView;
if(convertView == null) {
view = customInflater.inflate(R.layout.custom_row_route, parent, false);
} else {
customView = convertView;
}
此外,您还可以将LayoutInflater
存储为适配器的实例变量。
BTW 自定义前缀非常多余,因为根据定义,您编码的所有内容都将是自定义 ...
另一个答案(来自@Gavriel)的平滑滚动链接也很重要。