【问题标题】:ListView with images only仅包含图像的 ListView
【发布时间】:2014-02-19 23:20:49
【问题描述】:

我只是在问自己是否有办法制作没有任何文本的 ListView。我只找到了带有图像+文本的 ListViews,而这正是我不想要的。我只想在列表中添加很多图像,并且不想使用 ImageView 制作它,因为这会使我的应用程序崩溃。另外,我不希望我的应用程序滞后。谢谢!

这是我在 activity_main.xml 中得到的内容

<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

/>

我从另一个对我有帮助的帖子中找到了这个,但它没有显示所有图像

public class MainActivity extends Activity {


// Array of integers points to images stored in /res/drawable-hdpi/
int[] vehs = new int[]{
    R.drawable.veh1,
    R.drawable.veh2,
    R.drawable.veh3,
    R.drawable.veh4,
    R.drawable.veh5,
    R.drawable.veh6,
    R.drawable.veh7,
    R.drawable.veh8,
    R.drawable.veh9,
    R.drawable.veh10,
    R.drawable.veh11,
    R.drawable.veh12,
    R.drawable.veh13,
    R.drawable.veh14,
    R.drawable.veh15,
    R.drawable.veh16,
    R.drawable.veh17,
    R.drawable.veh18,
    R.drawable.veh19,
    R.drawable.veh20,
    R.drawable.veh21,
    R.drawable.veh22,
    R.drawable.veh23,
    R.drawable.veh24,
    R.drawable.veh25,
    R.drawable.veh26,
    R.drawable.veh27,
    R.drawable.veh28,
    R.drawable.veh29,
    R.drawable.veh30,
    R.drawable.veh31,
    R.drawable.veh32,
    R.drawable.veh33,
    R.drawable.veh34,
    R.drawable.veh35,
    R.drawable.veh36,
};


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Each row in the list stores country name, currency and flag
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

    for(int i=0;i<10;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("vehs", Integer.toString(vehs[i]) );
        aList.add(hm);
    }

    // Keys used in Hashmap
    String[] from = { "vehs","txt","cur" };

    // Ids of views in listview_layout
    int[] to = { R.id.vehs,R.id.txt,R.id.cur};

    // Instantiating an adapter to store each items
    // R.layout.listview_layout defines the layout of each item
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);

    // Getting a reference to listview of main.xml layout file
    ListView listView = ( ListView ) findViewById(R.id.listview);

    // Setting the adapter to the listView
    listView.setAdapter(adapter);
}

}

没有编辑所有的代码,所以你可能会发现有些东西让你感到困惑 xD

【问题讨论】:

  • 你能发布你的代码吗?
  • 一些代码将帮助我理解你的观点:)
  • @mohammedmomn 我实际上没有代码,希望你能给我一个基本的代码,我可以在其中添加图像。
  • @mohammedmomn 但是可以,我可以试试
  • 你不应该在 ListView 中使用 android:layout_height="wrap_content"。这会导致性能问题。

标签: java android eclipse image listview


【解决方案1】:

ListView 显示数据的方式是使用适配器。适配器获取您的数据,将其插入到自定义视图中,然后将其添加到列表中。

要构建快速映像ListView,您要做的第一件事就是将Picasso 添加到您的项目中。这个库会自动下载和缓存你的图片,处理ImageView回收等等。

接下来您要做的是编写您的项目视图。如果您想要仅包含图像的列表,则视图可以很简单:

<!-- res/layout/list_item.xml -->

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight" />

接下来,您要创建适配器。它接受图像 URL 的List&lt;String&gt; 作为输入,构建项目并将它们插入到ListView

public class ImageListAdapter extends ArrayAdapter<String> {
    List<String> items;

    public ImageListAdapter(Context context, int resource, List<String> items) {
        super(context, resource, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.list_item, null);
        }

        Picasso.with(getContext())
               .load(items.get(position))
               .into((ImageView) convertView);

        return convertView;
    }
}

如果您想添加更多选项,例如图像占位符、转换等,请阅读Picasso library documentation

最后,要在 ListView 上使用适配器,请将其添加到 Activity 的 onCreate 方法中:

List<String> images = new ArrayList<String>();
images.add("http://hometowncolumbia.files.wordpress.com/2007/12/lol-cats-dont-look-behind-cat.jpg");
images.add("http://i232.photobucket.com/albums/ee245/topswing/cat-lol.jpg");

listView = (ListView)findViewById(R.id.myListView);
listView.setAdapter(new ImageListAdapter(this, R.layout.list_item, images))

但在实际应用程序中,您可能希望从服务器加载图像列表。为此,您需要使用 AsyncTask

【讨论】:

  • 我只需要显示很多图像。这看起来很难理解。我对这个 android 开发的东西真的很陌生,这种事情让我很困惑。我在drawable的文件夹中有一堆图像,我想用ListView显示它们。除非有其他更好的方式来展示它们。
【解决方案2】:

不要为难,经典的3步解决方法很简单

第 1 步:
在 XML 中创建一个RelativeLayout。将图像移动到所需位置(始终居中,但您可以选择左或右)并将layout_height更改为wrap_content

第 2 步:
为 ListView 创建一个自定义适配器,如果这样做,您将能够将所有任何布局作为一个项目

public class ImagesAdapter extends ArrayAdapter<String> {

    private Activity mContext;
    private ArrayList<Bitmap> mImages;

    public ImagesAdapter(Activity context, ArrayList<String> listOfValues, ArrayList<Bitmap> images) {
        //The listOfValues is used when you make item click to get value
        //Each image must to have a text value
        super(context, R.layout.yourlayout, listOfValues);
        mContext = context;
        mImages = images;
    }

    @Override
    public void getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = mContext.getLayoutInflater();
        View returnView = inflater.inflate(R.layout.yourlayout, null);
        ImageView imageView = (ImageView) returnView.findViewById(R.id.yourimageid);
        imageView.setImageBitmap(mImages.get(position));
        return returnView;
    }

}

第 3 步:
在您的代码中实现

ImagesAdapter adapter = new ImagesAdapter(this, myList, myImages);
mylistView.setAdapter(adapter);
myListView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //volov = Values Of ListOfValues
        String volov = (String) myListView.getItemPosition(position);
        Log.i(TAG, "listOfValues is useful " + volov);
    }

});

【讨论】:

  • 怎么会有“void”方法类型和“View”返回类型?
猜你喜欢
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
  • 2021-06-02
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
相关资源
最近更新 更多