自定义列表视图scrollin不顺畅 [英] Custom listview scrollin isn't smooth

查看:146
本文介绍了自定义列表视图scrollin不顺畅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建这是一个事实,即列表不会平滑滚动伟大的工作,除了自定义列表视图。它的震荡和缓慢的。

I've created a custom listview which is working great apart from the fact that the list doesn't smooth scroll. Its choppy and slow.

这里的code,我填充的ListView:

Here's the code where I populate the listview:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.listitem, null);
        }

        FilmRecord film = films.get(position);
        if (film != null) {
            TextView filmTitle = (TextView) v.findViewById(R.id.filmtitle);
            TextView filmShowingDate = (TextView) v.findViewById(R.id.filmshowingtime);
            TextView filmAdded = (TextView) v.findViewById(R.id.filmadded);
            TextView filmLocations = (TextView) v.findViewById(R.id.filmlocations);
            TextView filmDescription = (TextView) v.findViewById(R.id.filmdescription);
            TextView filmProvider = (TextView) v.findViewById(R.id.filmprovider);
            TextView filmTicketUrl = (TextView) v.findViewById(R.id.filmticketurl);


            if (filmTitle != null) {
                filmTitle.setText(film.filmTitle);
            }

            if(filmDescription != null) {
                filmDescription.setText(film.filmDescription );
            }

            if(filmLocations != null) {
                filmLocations.setText(film.filmLocations );
            }

            if(filmShowingDate != null) {
                filmShowingDate.setText("Showing: " + film.filmShowingDate );
            }

            if(filmAdded != null) {
                filmAdded.setText("Added on: " + film.filmAdded );
            }

            if(filmProvider != null) {
                filmProvider.setText(film.filmProvider );
            }

            if(filmTicketUrl != null) {
                filmTicketUrl.setText(film.filmTicketUrl );
            }
        }

        //Check who the provider is and set the imageview to provider logo
        ImageView imageView = (ImageView) v.findViewById(R.id.providerImage);
        if(film.filmProvider.equals("Cineworld")) {
            Log.d("Provider", "Provider is Cineworld");
            imageView.setImageResource(R.drawable.cineworld);
        }
        else if(film.filmProvider.equals("Show Film Fist")){
            Log.d("Provider", "Provider is Show Film Fist");
            imageView.setImageResource(R.drawable.show_film_first);
        }

        return v;
    }

有没有人创建自定义列表视图时,也有类似的问题?任何帮助一如既往将是非常美联社preciated:)

Has anyone had similar issues when creating a custom listview? Any help as always would be much appreciated :)

推荐答案

您需要使用 ViewHolder 办法。

因为ID找到View还需要时间来执行。

because finding the View by id also takes time to execute.

使用 ViewHolder 办法,你将能够最大限度地减少通过缓存的意见发现通过ID查看过程中产生的负荷:)

by using ViewHolder approach you will be able to minimize the load created during finding View by id by caching the views :)

这里是很好的教程如何做到这一点。

Here is nice tutorial how to do that.

什么ViewHolder办法做的是:

假设你有你的的ListView 在一个时间4可见项。你必须充气查看 4的时间和 findViewById()只有4次,即使你滚动到第1000个项目的ListView

Say you have 4 visible items in your ListView at a time. You have to inflate the View 4 time and findViewById() 4 times only even if you scroll to 1000th item in ListView.

但是,如果你不使用 ViewHolder ,你已经把

But if you don't use ViewHolder and you have put

if (v == null) {
   LayoutInflater vi = ...;
}else{
   //... other implementation 
}

您已经吹大查看只有4次,但 findViewById()将被称为每时间 getView()被称为(通常当的ListView 滚动)。

You have to inflate the View 4 time only, but findViewById() will be called every time when the getView() is called (usually when ListView is scrolled).

编程快乐!

这篇关于自定义列表视图scrollin不顺畅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆