我在看起来很简单的 GridView 问题上遇到了重大问题,因为我无法注册我的自定义 ClickListener。
我可以让标准点击事件正常工作,但不能通过我的 AlbumClickListener,因此无法根据点击的位置检索我的对象详细信息。
因为我通常是严格的 iOS,所以在这件事上扯了我的头发。请帮忙!!
专辑 Java 对象
package com.pixelperfect.mayday;
public class AlbumGridView extends GridView implements
android.widget.AdapterView.OnItemClickListener {
private List<Album> albums;
private AlbumClickListener albumClickListener;
public AlbumGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public AlbumGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AlbumGridView(Context context) {
super(context);
}
public void setAlbums(List<Album> albums){
this.albums = albums;
MusicGridAdapter adapter = new MusicGridAdapter(getContext(), albums);
setAdapter(adapter);
setOnItemClickListener(this);
}
// Calling this method sets a listener to the list
// Whatever class is passed in will be notified when the list is pressed
// (The class that is passed in just has to 'implement VideoClickListener'
// meaning is has the methods available we want to call)
public void setOnAlbumClickListener(AlbumClickListener l) {
albumClickListener = l;
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
}
// When we receive a notification that a list item was pressed
// we check to see if a video listener has been set
// if it has we can then tell the listener 'hey a video has just been clicked' also passing the video
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
// this is crashing
if(albumClickListener != null){
albumClickListener.onAlbumClicked(albums.get(position));
}
}
}
专辑点击听众
package com.pixelperfect.mayday;
import com.pixelperfect.mayday.Album;
public interface AlbumClickListener {
public void onAlbumClicked(Album album);
}
音乐网格适配器
package com.pixelperfect.mayday;
public class MusicGridAdapter extends BaseAdapter
{
List<Album> albums;
private LayoutInflater mInflater;
public MusicGridAdapter(Context context, List<Album> albums) {
this.albums = albums;
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return albums.size();
}
@Override
public Object getItem(int position) {
return albums.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.music_grid, null);
}
Album album = albums.get(position);
String newString = album.getArtworkURL().replace("100x100", "600x600");
LoaderImageView thumb = (LoaderImageView) convertView.findViewById(R.id.grid_item_image);
thumb.setImageDrawable(newString);
return convertView;
}
}
活动音乐.xml
<?xml version="1.0" encoding="utf-8"?>
<com.pixelperfect.mayday.AlbumGridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="150dp"
android:stretchMode="columnWidth"
android:id="@+id/gridView1"/>
音乐网格.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp" >
<com.pixelperfect.mayday.LoaderImageView
android:id="@+id/grid_item_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:focusable="false"
android:clickable="false"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"
android:layout_marginBottom="4dp" />
</LinearLayout>
主要活动
public class Music extends Activity implements AlbumClickListener {
private AlbumGridView gridView;
// Album[] Album;
private ProgressDialog m_ProgressDialog = null;
@SuppressLint("HandlerLeak")
@Override protected void onResume()
{
super.onResume(); // setText() here
responseHandler = new Handler() {
public void handleMessage(Message msg) {
populateGridWithAlbums(msg);
};
};
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
gridView = (AlbumGridView) findViewById(R.id.gridView1);
gridView.setOnAlbumClickListener(this);
new Thread(new GetAlbumsFromiTunesTask(responseHandler, "GrabTheAlbums", "1")).start();
m_ProgressDialog = ProgressDialog.show(this, "Please wait...", "Loading Albums ...", true);
}
// This is the handler that receives the response when the YouTube task has finished
@SuppressLint("HandlerLeak")
Handler responseHandler = new Handler() {
public void handleMessage(Message msg) {
populateGridWithAlbums(msg);
m_ProgressDialog.dismiss();
};
};
/**
* This method retrieves the Library of videos from the task and passes them to our ListView
* @param msg
*/
private void populateGridWithAlbums(Message msg) {
@SuppressWarnings("unchecked")
List<Album> albums = (List<Album>) (msg.getData().get(GetAlbumsFromiTunesTask.LIBRARY));
gridView.setAdapter(new MusicGridAdapter(this, albums));
}
@Override
protected void onStop() {
responseHandler = null;
super.onStop();
m_ProgressDialog.dismiss();
}
@Override
public void onAlbumClicked(Album album) {
System.out.println("CLICKED 2");
}
}