首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在一个版本的应用程序中,同样的Android列表视图代码是起伏不定的,而在另一个版本中则不是。(已知已进行调整)

在一个版本的应用程序中,同样的Android列表视图代码是起伏不定的,而在另一个版本中则不是。(已知已进行调整)
EN

Stack Overflow用户
提问于 2014-08-01 01:23:34
回答 1查看 818关注 0票数 3

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

我发现了一些奇怪的问题。尽管如此,我在应用程序中使用的listview代码在两个应用程序中都是完全相同的,在一些设备上,没有广告的一个滚动卷非常(我是指非常)起伏不定,其他版本的应用程序滚动非常流畅。

有趣的是,我只在一些安装了原始rom的三星设备上观察到了这一点:GalaxyS2和SlimKat 4.4.2rom--这两个应用程序都滚动得很流畅。Galaxy 10.1,最新的原始rom (抱歉,不记得Android的版本)--这两个应用都滚动得很顺利。Galaxy S3的原始4.3rom-免费版本滚动是不稳定的。老实说,我不知道什么可能会造成这个问题,而且总体上会发生不同的行为。值得注意的是,在发生这种行为的设备上,我可以看到很多"AbsListView unregisterIRListener()“称为”LogCat中的日志“。(AbsListView unregisterIRListener() is called)

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

在这两个应用程序中,我经常使用GC,通过禁用listview的滚动和动画缓存属性可以很容易地解决问题。嗯,尽管存在GC问题,应用程序的根版本仍在顺利地滚动,在无广告版本中修复后,滚动仍然滞后。

我使用UrlImageViewHelper (https://github.com/koush/UrlImageViewHelper)在后台从互联网加载列表的图像,并缓存它们。我知道,现在它已经被否决了,但是当我没有把图片放进列表视图时,奇怪的行为仍然会发生,所以它不是一个引起问题的东西。

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

在代码中,除了listview之外,还会有其他东西对滚动性能产生负面影响吗?因为那份侧写的研究,我很怀疑。有什么问题吗?我已经看到了一些问题的解决方案--有些不清楚--把我找不到任何帮助作为答案的地方联系起来,这被标记为一个正确的答案--所以,没有运气。

这是我的listview代码:

FragmentList.java

代码语言:javascript
运行
AI代码解释
复制
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

代码语言:javascript
运行
AI代码解释
复制
<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_list.xml

代码语言:javascript
运行
AI代码解释
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-26 08:19:30

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25076968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文