Java 同样的Android listview代码在一个版本的应用程序中滚动不定,而在另一个版本中则不然。(已执行已知调整)

Java 同样的Android listview代码在一个版本的应用程序中滚动不定,而在另一个版本中则不然。(已执行已知调整),java,android,android-listview,lag,smooth-scrolling,Java,Android,Android Listview,Lag,Smooth Scrolling,我开发了一个Android应用程序。它有两个版本:根版本一,有广告;第二个,有点像叉子:根版本的付费、改名、无广告版本。它们几乎是一样的,它们只是在一些字符串、颜色和图形上有所不同,并且注释了一些admob(以及应用程序中材料中的视频广告,但我不认为这会对其他内容产生任何影响)代码 我遇到了一个奇怪的问题。尽管事实上,我在应用程序中使用的listview代码在两个应用程序中完全相同,但在某些设备上,ad-free one的滚动非常(我的意思是非常)剧烈,而其他版本的应用程序的滚动非常平稳 有趣的

我开发了一个Android应用程序。它有两个版本:根版本一,有广告;第二个,有点像叉子:根版本的付费、改名、无广告版本。它们几乎是一样的,它们只是在一些字符串、颜色和图形上有所不同,并且注释了一些admob(以及应用程序中材料中的视频广告,但我不认为这会对其他内容产生任何影响)代码

我遇到了一个奇怪的问题。尽管事实上,我在应用程序中使用的listview代码在两个应用程序中完全相同,但在某些设备上,ad-free one的滚动非常(我的意思是非常)剧烈,而其他版本的应用程序的滚动非常平稳

有趣的是,我只在一些安装了原始rom的三星设备上观察到了这一点:Galaxy S2和SlimKat 4.4.2 rom,这两款应用程序都可以平滑滚动。Galaxy tab 10.1,最新的原版rom(对不起,我记不起Android的版本了)-两个应用程序都可以流畅地滚动。GalaxyS3的原版4.3 rom-ad免费版滚动不稳定。老实说,我不知道是什么造成了这个问题,行为上的差异通常会发生。 可能值得注意的是,在发生这种行为的设备上,我可以在LogCat中看到很多“AbsListView unregisterListener()被称为”日志。()

在这两个应用程序中,我都尽力使用正确的方法实现平滑滚动listview,之后我尝试了在Google上找到的所有技巧,以提高解决该问题的性能。(此处汇编:)

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

我正在使用urlmageviewhelper()在后台从Internet加载列表的图像并缓存它们。我知道,现在它已经被弃用了,但是当我在listview中没有放入任何图像时,奇怪的行为仍然会发生,所以这不是一件会导致问题的事情

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

除了listview之外,代码中还有其他东西会对滚动性能产生负面影响吗?这对我来说是值得怀疑的,因为那个剖析器的研究。有什么问题吗?我见过一些问题,这些问题的解决方案与有些不清楚的矿山类似——只是链接,我找不到任何帮助作为答案,这被标记为正确答案——因此,没有运气

下面是我的listview代码:

FragmentList.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;

        }

    }

}
导入android.content.Context;
导入android.support.v4.app.Fragment;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.BaseAdapter;
导入android.widget.FrameLayout;
导入android.widget.ImageView;
导入android.widget.ListView;
导入android.widget.TextView;
导入com.googlecode.androidannotations.annotations.afterview;
导入com.googlecode.androidannotations.annotations.Bean;
导入com.googlecode.androidannotations.annotations.EFragment;
导入com.googlecode.androidannotations.annotations.ViewById;
导入com.googlecode.androidannotations.annotations.SharedReferences.Pref;
导入com.koushikdutta.urlmageviewHelper.urlmageviewHelper;
导入com.squareup.otto.Bus;
导入com.squareup.otto.BusProvider;
导入com.squareup.otto.Subscribe;
@EFragment(右布局、碎片列表)
公共类FragmentList扩展了片段{
@优先股
MyPrefs_uuMyPrefs;
@ViewById
列表视图列表视图;
专用信道适配器;
专用最终总线=总线提供程序.getInstance();
私人频道[]频道;
@豆子
沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟沟;
//私有静态字符串日志\u ID=“FragmentList”;
@后景
受保护的void init(){
BusProvider.getInstance().register(此);
//视图页脚视图=((LayoutFlater)
//getActivity().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE))
//.充气(右布局页脚,
//空,假);
//addFooterView(footerView);
//((按钮)
//footerView.findViewById(R.id.footerButton)).setOnClickListener(新
//OnClickListener(){
//
//@覆盖
//公共void onClick(视图v)
// {
//意向i=新意向(意向.行动\视图);
//i.setData(Uri.parse(“http://watch2.netvi.tv/oferta"));
//i.设置标志(意图、标志、活动、新任务);
//getActivity().getBaseContext().startActivity(i);
//
// }
// });
}
@凌驾
公共空间{
BusProvider.getInstance().unregister(此);
super.ondestory();
}
@订阅
通道上的公共无效日期(通道删除事件){
channels=event.getChannels();
adapter=newchanneladapter(getActivity());
适配器。设置通道(通道);
setAdapter(适配器);
setDivider(getResources().getDrawable(R.drawable.divider));
setDividerHeight(2);
setOnItemClickListener(新的OnItemClickListener(){
<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>
<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>