带有许多图像的列表视图不起作用

问题描述:

我正在创建一个包含listview的活动。列表视图的每个元素将包含一个图像和一个文本。当我以10个元素运行应用程序时,它工作得很好。但是当我有90个元素时,我的应用程序停止工作。你有解决这个问题的办法吗?带有许多图像的列表视图不起作用

PS。我的应用程序不会使用互联网。

我的活动XML:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".Testlistview" > 

    <ListView 
     android:id="@+id/list66" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:cacheColorHint="@android:color/transparent" 
     android:fastScrollEnabled="true" 
     android:persistentDrawingCache="scrolling" 
     android:scrollingCache="false"> 

    </ListView> 

</RelativeLayout> 

我的适配器:

import android.app.Activity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class customadaptertest extends ArrayAdapter<String> { 

private final Activity context; 
private final String[] web; 
private final Integer[] imageId; 
public customadaptertest(Activity context, 
        String[] web, Integer[] imageId) { 
    super(context, R.layout.listtestitem, web); 
    this.context = context; 
    this.web = web; 
    this.imageId = imageId; 
} 
@Override 
public View getView(int position, View view, ViewGroup parent) { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    View rowView= inflater.inflate(R.layout.listtestitem, null, true); 
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); 

    ImageView imageView = (ImageView) rowView.findViewById(R.id.img); 
    txtTitle.setText(web[position]); 

    imageView.setImageResource(imageId[position]); 
    return rowView; 
} 
} 

我的活动:

import android.content.res.TypedArray; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 

public class Testlistview extends AppCompatActivity { 


ListView list66; 
String[] web = { 
     "some text" 
} ; 
Integer[] imageId = { 
     R.drawable.some_item_image 
}; 

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


    customadaptertest adapter = new 
      customadaptertest(Testlistview.this, web, imageId); 
    list66=(ListView)findViewById(R.id.list66); 
    list66.setAdapter(adapter); 

} 
} 

我的项目:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 
<TableRow> 
    <ImageView 
     android:id="@+id/img" 
     android:layout_width="50dp" 
     android:layout_height="50dp"/> 

    <TextView 
     android:id="@+id/txt" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" /> 

</TableRow> 
</TableLayout> 
+0

我认为,而不是使用listview使用recyclerview。因为recyclerview仅加载了当前正在预览的项目。 –

+0

*因为recyclerview只加载当前正在预览的项目*与ListView相同,因为它也使用视图回收...主要问题是在主线程中加载位图。 – Selvin

您应该使用ViewHolder模式。看看这个:Making ListView Scrolling Smooth

+0

你能解释一下问题所在吗?我的意思是,为什么我的应用程序冻结时,有很多图像?这是什么原因? –

+0

你膨胀了很多的意见和使用大量的内存,他们每个人都有一个形象。使用视图可以防止膨胀视图并重复使用令人兴奋的视图。 –