SimpleAdapter响应item点击事件

Fragment:

package com.woyoli.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.woyoli.R;

public class PickGiftFragment extends Fragment {

	private ListView mListView;
	private SimpleAdapter simpleAdapter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View mView = inflater.inflate(R.layout.fragment_main_home_page, null);
		mListView = (ListView) mView.findViewById(R.id.lv_home_page);
		simpleAdapter = new SimpleAdapter(getActivity(), getData(), R.layout.home_page_list_row, new String[]{"image"}, new int[]{R.id.iv_home_page});
		mListView.setAdapter(simpleAdapter);
		mListView.setOnItemClickListener(new MyListener());
		return mView;
	}
	
	private List> getData() {
		List> mList = new ArrayList>();
		Map mMap = new HashMap();
		mMap.put("image", R.drawable.home_page_sample);
		mMap.put("data", "one");
		mList.add(mMap);
		
		mMap = new HashMap(); 
		mMap.put("image", R.drawable.home_page_sample);
		mMap.put("data", "two");
		mList.add(mMap);
		return mList;
	}
	
	class MyListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView parent, View view, int position,
				long id) {
			Map mMap = (Map) simpleAdapter.getItem(position);
			Toast.makeText(getActivity(), mMap.get("data").toString(), 0).show();
		}
		
	}
}

home_page_list_row.xml:



    


fragment_main_home_page.xml:



    




你可能感兴趣的:(Android)