java - 相同的 Android ListView 代码在一个版本的应用程序中滚动时断断续续,而在其他版本中则不是。 (已知的调整执行)

标签 java android android-listview lag smooth-scrolling

我开发了一个 Android 应用程序。它有两个版本:根版本,带广告,第二个版本,有点分支:根版本的付费、更名、无广告版本。它们几乎相同,它们仅在一些字符串、颜色和图形方面有所不同,并且注释掉了一些 admob(和应用程序 Material 内的视频广告,但我认为它不会对其余部分产生任何影响)代码。

我遇到了奇怪的问题。事实上,我在应用程序中使用的 listview 代码在两个应用程序中完全相同,无广告滚动在某些设备上非常(我的意思是非常)不稳定,而其他版本的应用程序滚动流畅。

有趣的是,我只在一些安装了原始 rom 的三星设备上观察到了这一点:带有 SlimKat 4.4.2 rom 的 Galaxy S2 - 两个应用程序都可以流畅地滚动。 Galaxy tab 10.1,最新的原始 rom(抱歉,不记得 Android 版本)——两个应用程序都可以流畅地滚动。具有原始 4.3 rom 的 Galaxy S3 - 无广告版本滚动时断断续续。老实说,我不知道什么可能会导致这个问题,并且通常会出现行为差异。 可能值得注意的是,在发生此行为的设备上,我可以在 LogCat 中看到很多“AbsListView unregisterIRListener() is called”日志。 ( AbsListView unregisterIRListener() is called )

在这两个应用程序中,我都尽力使用正确的方式来实现平滑滚动的 ListView ,然后我尝试了在 Google 上找到的所有技巧来提高性能以解决该问题。 (此处编译:http://optimizationtricks.blogspot.com/2014/01/tricks-to-boost-performance-of-list-view.html)

在这两个应用程序中,我经常遇到 GC,可以通过禁用 ListView 的滚动和动画缓存属性轻松解决。好吧,尽管有 GC 问题,根版本的应用程序滚动流畅,在无广告版本中修复后,滚动仍然滞后。

我正在使用 UrlImageViewHelper ( https://github.com/koush/UrlImageViewHelper ) 在后台从 Internet 加载列表的图像并缓存它们。我知道,现在它已被弃用,但是当我没有将图像放入 ListView 时,奇怪的行为仍然会发生,所以这不会导致问题。

我做了一些分析,在两个应用程序中滚动列表后,我可以看到虽然滚动方法 getView 和 displayData 在此应用程序中占用了大部分处理器时间。事实上,显示数据嵌套在 getView 中,因此这是造成大部分处理器负载的方法。我开始分析,进行一些滚动,然后停止分析,这大约是 getView 在出现延迟的设备上启动的两个应用程序中占用的整个处理器时间的 6%。我的应用程序名称描述的其他方法占用了大约 0% 的处理器时间。 Tl;dr:在探查器中,我找不到这两个版本之间的行为差​​异。在 LogCat 中,我也看不到任何可能与不正确的应用程序行为有关的日志。

除了 ListView 之外,代码中是否还有其他任何东西可能会对滚动性能产生负面影响?由于该分析器研究,我对此表示怀疑。可能是什么问题?我已经看到一些问题,其中问题的解决方案类似于我的问题,但有些不清楚 - 只是链接我找不到任何帮助作为答案的地方,并且被标记为正确答案 - 所以,运气不好。

这是我的 ListView 代码:

fragment 列表.java

import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.ViewById;
import com.googlecode.androidannotations.annotations.sharedpreferences.Pref;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;
import com.squareup.otto.Bus;
import com.squareup.otto.BusProvider;
import com.squareup.otto.Subscribe;

@EFragment(R.layout.fragment_list)
public class FragmentList extends Fragment {
    @Pref
    MyPrefs_ myPrefs;
    @ViewById
    ListView listView;
    private ChannelAdapter adapter;
    private final Bus bus = BusProvider.getInstance();
    private Channel[] channels;
    @Bean
    ChannelsHolder channelsHolder;

    // private static String LOG_ID = "FragmentList";

    @AfterViews
    protected void init() {
        BusProvider.getInstance().register(this);
        // View footerView = ((LayoutInflater)
        //getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
        //.inflate(R.layout.footer,
        // null, false);
        // listView.addFooterView(footerView);
        // ((Button)
        // footerView.findViewById(R.id.footerButton)).setOnClickListener(new
        // OnClickListener() {
        //
        // @Override
        // public void onClick(View v)
        // {
        // Intent i = new Intent(Intent.ACTION_VIEW);
        // i.setData(Uri.parse("http://watch2.netvi.tv/oferta"));
        // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // getActivity().getBaseContext().startActivity(i);
        //
        // }
        // });
    }

    @Override
    public void onDestroy() {
        BusProvider.getInstance().unregister(this);
        super.onDestroy();
    }

    @Subscribe
    public void onChannelsReady(ChannelsLoadedEvent event) {
        channels = event.getChannels();
        adapter = new ChannelAdapter(getActivity());
        adapter.setChannels(channels);
        listView.setAdapter(adapter);
        listView.setDivider(getResources().getDrawable(R.drawable.divider));
        listView.setDividerHeight(2);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                bus.post(new ChannelChangeEvent(channels[arg2]));
                channelsHolder.setCurrentChannerl(channels[arg2]);
                channelsHolder.setChannelOrderNumber(arg2);
            }
        });
        if (myPrefs.settingsLastUrl().exists()
                && myPrefs.settingsLastUrl().get()) {
            if (myPrefs.lastUrl().exists() && myPrefs.lastUrl().get() != null) {
                for (Channel channel : channels) {
                    if (channel.getId().equals(myPrefs.lastUrl().get())) {
                        bus.post(new ChannelChangeEvent(channel));

                    }
                }

            }
        }
    }

    class ChannelElement extends FrameLayout {
        TextView channelName;
        TextView channelNumber;
        ImageView channelLogo;

        // ProgressBar progress;
        // int position;

        public ChannelElement(Context context) {
            super(context);
            inflate(context, R.layout.list_view_element, this);
            channelName = (TextView) findViewById(R.id.textView1);
            channelNumber = (TextView) findViewById(R.id.textView3);
            channelLogo = (ImageView) findViewById(R.id.imageView1);
            channelName.setTypeface(FontUtils.getRobotoTypeface(getActivity(),
                    FontType.NORMAL));

        }

        public void displayData(Channel channel) {

            channelName.setText(channel.getName());
            channelNumber.setText(channel.getId());
            UrlImageViewHelper.setUrlDrawable(channelLogo,
                    channel.getThumbnail());

        }

    }

    private class ChannelAdapter extends BaseAdapter {

        private final Context context;
        private Channel[] channels;

        public void setChannels(Channel[] channels) {
            this.channels = channels;
        }

        public ChannelAdapter(Context context) {
            super();
            this.context = context;
        }

        @Override
        public int getCount() {
            return channels.length;
        }

        @Override
        public Object getItem(int position) {
            return channels[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

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

            ChannelElement myView = null;
            if (convertView == null) {
                myView = new ChannelElement(context);

            } else {
                myView = (ChannelElement) convertView;
            }
            myView.displayData(channels[position]);
            return myView;

        }

    }

}

list_view_element.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="@android:color/transparent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_gravity="left|center_vertical"
        android:scaleType="centerInside" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:text="TextView"
        android:visibility="invisible" />

</FrameLayout>

fragment 列表.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/application_background_lighter" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:smoothScrollbar="true" />

</LinearLayout>

最佳答案

我已经设法解决了我的问题。有一个相当大的图像 (1280x1024) 设置为我的应用程序基本布局的背景(该图像一直被其他布局覆盖,所以它是不必要的,它不可见)。将背景颜色设置为不可见而不是该图像后,问题就解决了。

关于java - 相同的 Android ListView 代码在一个版本的应用程序中滚动时断断续续,而在其他版本中则不是。 (已知的调整执行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25076968/

相关文章:

java - 构建失败错误 : cannot find symbol if (Build. VERSION.SDK_INT >= Build.VERSION_CODES.R)

Java正则表达式替换和条件

java - 使用java的zip目录

android - 三星 Galaxy S7 支持 Android Camera2 API 的级别是多少

java - ListView 打开一个新 Activity

java - ListView 中的 map View (精简模式)仅显示一张 map

java - 如何确定 Android 应用程序崩溃的原因

java - float 操作按钮未显示在 bottomsheetfragment 折叠状态

java - 按钮音效仅起作用28次

android - 在 ViewPager 中重新加载或通知 Fragment 的数据