带有许多图像的Listview无效

时间:2017-02-19 16:00:41

标签: android listview android-studio-2.0

我正在创建一个包含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>

1 个答案:

答案 0 :(得分:0)

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