仅包含图像的ListView

时间:2018-07-31 12:05:23

标签: android android-layout listview

我如何制作仅包含图像而没有其他内容的listView。我是listView和所有adapter的新手。我成功制作了仅包含文本的listView,但无法通过图像完成此操作。 这是我到目前为止所做的。

我的MainActivity类

public class MainActivity extends AppCompatActivity {

ListView listView;
ImageView imageView;

int[] imageIds = {R.drawable.chrysanthemum, R.drawable.desert, R.drawable.hydrangeas, R.drawable.jellyfish};

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

    listView = (ListView) findViewById(R.id.listView);

    ArrayAdapter<ImageView> adapter = new ArrayAdapter<ImageView>(this, R.layout.imageview, imageIds);

    listView.setAdapter(adapter);
}

}

主要XML代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.danishrizvi.myapplication.MainActivity">

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</ListView>

ImageView布局文件

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

此行ArrayAdapter<ImageView> adapter = new ArrayAdapter<ImageView>(this, R.layout.imageview, imageIds);Unable to Resolve Constructor中出现错误。 到目前为止,我在互联网上搜索到的错误信息是我可能需要一个自定义适配器或类似的东西才能完成工作,但无法知道如何实现以及它如何工作。

2 个答案:

答案 0 :(得分:0)

layout_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<ImageView
    android:id="@+id/img_adapter"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

ImageAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class ImageAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<String> arrayList;

public ImageAdapter(Context mContext, ArrayList<String> arrayList) {
    this.mContext = mContext;
    this.arrayList = arrayList;
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Object getItem(int position) {
    return arrayList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_adapter, parent, false);
    ImageView imgAdapter = (ImageView) convertView.findViewById(R.id.img_adapter);
    Glide.with(mContext).load(arrayList.get(position)).into(imgAdapter);
    return convertView;
}
}

在这样的活动中使用它,

ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("image url");
arrayList.add("image url");
arrayList.add("image url");

ImageAdapter imageAdapter = new ImageAdapter(this, arrayList);
listView.setAdapter(imageAdapter);

答案 1 :(得分:0)

示例代码在这里: https://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ 我正在显示带有图像和文本的行。因此,您可以根据需要对其进行修改。