尽管使用了最佳实践,但Android自定义列表视图非常慢

时间:2016-06-18 22:37:08

标签: android image listview

我正在编写VR Android应用程序并使用自定义列表视图向用户显示图像。无论出于何种原因,列表视图在滚动时都非常滞后。我已经查看了许多关于如何加快列表视图的教程,但我似乎无法弄清楚我做错了什么。我尝试过减小图像按钮的大小,禁用滚动缓存以及许多其他内容。注意:我不认为这个问题是毕加索的问题,因为当我发表评论时,滚动时它仍然很慢。

我的自定义布局的XML是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="170dp"
android:padding="10dp">
<ImageButton
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:id="@+id/redirect_btn"
    android:longClickable="false"
 />


</LinearLayout>

自定义列表视图的代码如下:

public class MyCustomAdapter extends BaseAdapter
{
private Activity activity;
private String[] listOfVideoNames;
private String[] listOfVideoTileImageURLs;
private VrVideoInfo[] listOfVrVideoInfo;
private Context context;

private static LayoutInflater inflater = null;

public MyCustomAdapter(String[] listOfVideoNames, String[] listOfVideoTileImages, VrVideoInfo[] vrList, Activity activity, Context context) {
    this.listOfVideoNames = listOfVideoNames;
    listOfVideoTileImageURLs = listOfVideoTileImages;
    this.context = context;
    this.activity = activity;
    listOfVrVideoInfo = vrList;

    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem (int pos) {
    return listOfVideoNames[pos];
}

@Override
public long getItemId(int pos) {
    return 0;
    // return 0 if your list items do not have an Id variable
}
public class Holder {
    ImageButton imageButton;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final Holder holder;
    if(convertView == null)
    {
        holder = new Holder();
        convertView = inflater.inflate(R.layout.custom_list_layout, parent, false);
        holder.imageButton = (ImageButton) convertView.findViewById(R.id.redirect_btn);

        convertView.setTag(holder);

    }
    else
    {
        holder = (Holder)convertView.getTag();
    }

    Picasso.with(context).load(listOfVideoTileImageURLs[position])
    .into(holder.imageButton);
    holder.imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(activity, VideoViewer.class);
            intent.putExtra("video_title", listOfVideoNames[position]);
            activity.startActivity(intent);
        }
    });
    return convertView;
}

以防它是相关的,这是调用自定义列表视图的原因:

public class MenuActivity extends Activity {

// string adapter that will handle the data of the list view
MyCustomAdapter adapter;
String[] vrVideoTitles = null;

String[] videoTileImageListURLs = null;
ArrayList<VrVideoInfo> videoInfo;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_activity);


    videoInfo = getIntent().getParcelableArrayListExtra("rData");
    vrVideoTitles = new String[videoInfo.size()];
    videoTileImageListURLs = new String[videoInfo.size()];




    for (int i = 0; i < videoInfo.size(); i++) {
        vrVideoTitles[i] = videoInfo.get(i).GetTitle();
    }

    for(int i = 0; i < videoInfo.size(); i++)
    {
        videoTileImageListURLs[i] = videoInfo.get(i).GetImageURl();
    }

    VrVideoInfo[] info = videoInfo.toArray(new VrVideoInfo[videoInfo.size()]);

    //instantiate custom adapter
    adapter = new MyCustomAdapter(vrVideoTitles,    
    videoTileImageListURLs,info, this, this);

    //handle listview and assign adapter
    ListView lView = (ListView) findViewById(R.id.list_container);
    lView.setOverScrollMode(View.OVER_SCROLL_NEVER);
    lView.setScrollingCacheEnabled(false);
    lView.setDrawingCacheEnabled(false);
    lView.setAdapter(adapter);
}



}

任何信息或提示都会非常有用和赞赏。

2 个答案:

答案 0 :(得分:0)

使你的viewholder静态,非静态内部类也引用它们的包含类,它不是一个内存有效的实现。

答案 1 :(得分:0)

我过了一会儿就弄明白了。显然,我正在工作的另一个实习生在列表视图的顶部放置了一个图像视图,我没有看到它,因为他将它设置为透明。换句话说,不要将图像视图放在列表视图之上。