简体   繁体   English

自定义ListView的滚动问题

[英]Scrolling issues with custom ListView

I've created a custom listview which loads data from a database. 我创建了一个自定义列表视图,该视图从数据库加载数据。 This data is then brought into an array. 然后将这些数据放入数组。

I can get all the data to show up where it should on the listview. 我可以获取所有数据,以将其显示在listview上的哪个位置。 So no issues there. 因此,那里没有问题。 The issue I'm having is scrolling. 我遇到的问题正在滚动。 When I scoll the listview it appears to run slow. 当我对列表视图进行排序时,它似乎运行缓慢。

Below is an extract of my code: 以下是我的代码的一部分:

static class ViewHolder {
    public TextView txtTitle;
    public TextView txtDescription;
    public TextView txtLocations;
    public TextView txtShowingDate;
    public TextView txtAdded;
    public TextView txtProvider;
    public TextView txtTicketUrl;
    public ImageView imgProviderImage;
    }

public class FilmItemAdapter extends ArrayAdapter<FilmRecord> {
    public ArrayList<FilmRecord> films;

    public FilmItemAdapter(Context context, int textViewResourceId, ArrayList<FilmRecord> films) {
        super(context, textViewResourceId, films);
        this.films = films;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            /* There is no view at this position, we create a new one. 
               In this case by inflating an xml layout */
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.listitem, null);

            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            //Find find by ID once, and store details in holder.
            viewHolder.txtTitle = (TextView)convertView.findViewById(R.id.filmtitle);
            viewHolder.txtShowingDate = (TextView) convertView.findViewById(R.id.filmshowingtime);
            viewHolder.txtAdded = (TextView) convertView.findViewById(R.id.filmadded);
            viewHolder.txtLocations = (TextView) convertView.findViewById(R.id.filmlocations);
            viewHolder.txtDescription = (TextView) convertView.findViewById(R.id.filmdescription);
            viewHolder.txtProvider = (TextView) convertView.findViewById(R.id.filmprovider);
            viewHolder.txtTicketUrl = (TextView) convertView.findViewById(R.id.filmticketurl);
            viewHolder.imgProviderImage = (ImageView) convertView.findViewById(R.id.providerImage);

            convertView.setTag(viewHolder);
        } else {
             /* We recycle a View that already exists */
            viewHolder = (ViewHolder) convertView.getTag();
        }

        FilmRecord film = films.get(position);

        //Get film details from ARRAY and not the database - getting records from a db is time consuming.
        viewHolder.txtTitle.setText(films.get(position).filmTitle);
        viewHolder.txtDescription.setText(films.get(position).filmDescription);
        viewHolder.txtLocations.setText(films.get(position).filmLocations);
        viewHolder.txtShowingDate.setText("Showing time: " + films.get(position).filmShowingDate);
        viewHolder.txtAdded.setText("Added on: " + films.get(position).filmAdded);
        viewHolder.txtProvider.setText(films.get(position).filmProvider);
        viewHolder.txtTicketUrl.setText(films.get(position).filmTicketUrl);

        return convertView;

    }

}

Help on this would be great :) 帮助会很棒:)

可能会帮助您: API演示示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM