我正在创建一个由listView组成的活动。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>
您应该使用视图持有人模式。看看这一点:使ListView滚动光滑。