我试图制作一个只有图像而没有分隔符的列表视图,所以它看起来像一张大图。
这是适配器的代码:
public class ImageListAdapter extends ArrayAdapter<String> {
private final Context context;
private final int[] ImageValue;
private ImageView image;
public ImageListAdapter(Context context, int[] imageValue) {
super(context, R.layout.image_list_item);
this.context = context;
this.ImageValue = imageValue;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
rowView = inflater.inflate(R.layout.image_list_item, parent, false);
image = (ImageView) rowView.findViewById(R.id.image);
image.setImageDrawable(context.getResources().getDrawable(ImageValue[position]));
return rowView;
}
}
这里是image_list_item XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
这是主要活动:
public class Instruction extends Activity {
private ListView listView;
int[] imgValues = {R.drawable.instruction_0, R.drawable.instruction_1};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_instruction);
listView = (ListView) findViewById(R.id.image_list);
listView.setAdapter(new ImageListAdapter(this,imgValues));
}
这里是activity_instruction.xml:
<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#0fa2c0"
tools:context=".Instruction" >
<ListView
android:id="@+id/image_list"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:divider="@null"
android:dividerHeight="0dip" >
</ListView>
当我尝试运行它时,我只得到带有背景颜色的空白屏幕,根本没有图像..
答案 0 :(得分:5)
添加此适配器类
public class ImageListAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.instruction_0, R.drawable.instruction_1
};
// Constructor
public CustomList(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new ListView.LayoutParams(70, 70));
return imageView;
}
}
并简单地在指令类中调用此适配器,如
public class Instruction extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_instruction);
listView = (ListView) findViewById(R.id.image_list);
listView.setAdapter(new ImageListAdapter(this));
}
}
答案 1 :(得分:0)
为什么不使用ImageView.setImageResource?
同时尝试使用父null对rowView进行充气(无论如何都将添加到ListView中)
编辑:试试这个适配器类
public class ImageListAdapter extends BaseAdapter {
private final Context context;
private final int[] ImageValue;
private ImageView image;
public ImageListAdapter(Context context, int[] imageValue) {
super(context, R.layout.image_list_item);
this.context = context;
this.ImageValue = imageValue;
}
@Override
public int getCount() {
return ImageValue.length;
}
@Override
public Object getItem(int position) {
return ImageValue[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
rowView = inflater.inflate(R.layout.image_list_item, null, false);
image = (ImageView) rowView.findViewById(R.id.image);
image.setImageResource(ImageValue[position]);
return rowView;
}