我是Android初学者,我正在构建一个音乐应用程序以获得乐趣,我使用Picasso
并排显示2列ImageButton
。为什么选择毕加索?因为简单的ScrollView
和ImageButton
,我的应用程序在向下滚动时会滞后很多。
无论如何,我在点击图片时试图发出声音,但我无法在互联网上找到与此相关的任何内容。
这是我的代码:
ImageListAdapter.java
package com.kitbass.mastersynthetizer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class ImageListAdapter extends ArrayAdapter {
private Context context;
private LayoutInflater inflater;
private Integer[] imageResources;
static class ViewHolder {
ImageButton mImageButton;
}
public ImageListAdapter(Context context, Integer[] imageUrls) {
super(context, R.layout.listview_item_image, imageUrls);
this.context = context;
this.imageResources = imageUrls;
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder pattern in play
ViewHolder viewHolder;
if (null == convertView) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.listview_item_image, parent, false);
viewHolder.mImageButton = (ImageButton) convertView.findViewById(R.id.iv_button);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Picasso
.with(context)
.load(imageResources[position])
.fit()
.centerInside()
.into(viewHolder.mImageButton);
return convertView;
}
}
PicassoActivity.java
GridView gridView = (GridView) findViewById(R.id.heenok_grid_view);
gridView.setAdapter(new ImageListAdapter(PicassoActivity.this, imgResourcesHeenok));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(PicassoActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
activity_picasso.xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/heenok_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2" />
listview_item_image.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/iv_button"
android:layout_width="match_parent"
android:layout_height="121dp"
android:background="@null"
android:foreground="?attr/selectableItemBackgroundBorderless" />
谢谢!
更新:错误在于我使用ImageButton
代替ImageView
...这就是为什么我无法拥有Toast
工作
答案 0 :(得分:1)
首先,我会为您的适配器类做一些不同的事情,使用ViewHolder模式回收您的视图:
将您的xml更改为:
按照:
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/iv_button"
android:layout_width="match_parent"
android:layout_height="121dp"
android:background="@null"
android:foreground="?attr/selectableItemBackgroundBorderless"/>
然后在适配器中使用ViewHolder模式:
public class ImageListAdapter extends ArrayAdapter {
private Context context;
private LayoutInflater inflater;
private Integer[] imageResources;
static class ViewHolder{
ImageButton mImageButton;
}
public ImageListAdapter(Context context, Integer[] imageUrls) {
super(context, R.layout.listview_item_image, imageUrls);
this.context = context;
this.imageResources = imageUrls;
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder pattern in play
ViewHolder viewHolder;
if (null == convertView) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.listview_item_image, parent, false);
viewHolder.mImageButton = (ImageButton) convertView.findViewById(R.id.iv_button);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
Picasso
.with(context)
.load(imageResources[position])
.fit()
.centerInside()
.into(viewHolder.mImageButton);
return convertView;
}
}
或切换到更受欢迎的RecyclerView
然后,因为您使用GridView
来处理点击,您需要在Activity类中添加AdapterView.OnItemClickListener
:
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
//... handle the sound playing here
// onSoundButtonClick() ? maybe
}
});
所有这些你可以找到一个例子here
祝你好运,编码愉快!
答案 1 :(得分:0)
更新 listview_item_image.xml ,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_button"
android:layout_width="match_parent"
android:layout_height="121dp"
android:background="@null"
android:foreground="?attr/selectableItemBackgroundBorderless"/>
将 ImageListAdapter 方法 getView()更新为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = inflater.inflate(R.layout.listview_item_image, parent, false);
}
ImageButton imageButton = (ImageButton) convertView.findViewById(R.id.image_button);
Picasso
.with(context)
.load(imageResources[position])
.fit()
.centerInside()
.into(imageButton);
return convertView;
}
希望这会奏效。