列表视图加载更快

List view load faster

我正在使用 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;

    }

}

和我想的一样。您应该使用 holder 模式:http://developer.android.com/training/improving-layouts/smooth-scrolling.html 使其更快

在第一段代码中,您一直在调用 response.body()(为什么不将主体放入变量中并重新使用它?)。

然后在适配器中,您没有重复使用 convertView:

View customView;
if(convertView == null) {
   view = customInflater.inflate(R.layout.custom_row_route, parent, false);
} else {
   customView = convertView;
}

此外,您还可以将 LayoutInflater 存储为适配器的实例变量。

顺便说一句,custom 前缀非常多余,因为根据定义,您编写的所有代码都将是 custom...

另一个答案(来自@Gavriel)的平滑滚动 link 也很重要。