这个话题有很多讨论。但它们都不适合我。我实现了一个自定义的 ListView,其中包含一个ImageView
,ListView
并且ListView
是从ListFragment
. 我的 ImageView 是可点击的,因此当用户单击 ImageView 时,会激活 Camera 活动,并且 ImageView 会使用捕获的图像进行更新。XML如下所示
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_background_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:clickable="true"
android:focusable="false"
android:contentDescription="@string/image_view_description"
android:layout_height="50dip"
android:src="@drawable/icons_preview"
/>
</LinearLayout>
</RelativeLayout>
现在我需要区分用户的单击是针对列表项还是针对 ImageView。我设置onListItemClick
它可以捕获用户单击 ListView 中的列表项,但是当我单击 ImageView 时,它不起作用。如何检测用户何时单击 ImageView。我的 ListFragment 类如下所示。
public class MyListFragment extends ListFragment {
private customList_Adaptor adapter;
private static final int CAMERA_REQUEST = 0;
private static final int RESULT_OK = 0;
private static MyListFragmentInterface mCallback = null;
static Context thisListFragmentContext = null;
private String[] mListItems;
private TrackerDBAdapter dbHelper;
private List<String> listIDs;
public MyListFragment() {
}
public MyListFragment(MyListFragmentInterface callback) {
mCallback = callback;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.my_list_fragment, container, false);
dbHelper = new TrackerDBAdapter(getActivity());
dbHelper.open();
Cursor cursor = dbHelper.fetchAllTrackerInTheList();
mListItems = new String[dbHelper.getLastInsertedID()];
thisListFragmentContext = getActivity();
int i = 0;
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
mListItems[i++] = cursor.getString(1);
dbHelper.close();
listIDs = new ArrayList<String>();
listIDs = Arrays.asList(mListItems);
adapter = new customList_Adaptor(thisListFragmentContext, R.layout.list_row, listIDs);
ListView lv = (ListView) view.findViewById(android.R.id.list);
lv.setAdapter(adapter);
return view;
}
public void addNewTrackerintoListDisplay(TrackerInfo newTracker){
mListItems = append(mListItems, newTracker.getIdnumber());
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
mListItems));
}
@SuppressLint("NewApi")
private String[] append(String[] mListItems2, String idnumber) {
// TODO Auto-generated method stub
final int N = mListItems2.length;
mListItems2 = Arrays.copyOf(mListItems2, N + 1);
mListItems2[N] = idnumber;
return mListItems2;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mCallback.itemSelected(mListItems[position]);
}
}
我的适配器类如下。
public class customList_Adaptor extends ArrayAdapter<String>{
protected static final int CAMERA_REQUEST = 0;
private Context context;
private List<String> listIDs = new ArrayList<String>();
public customList_Adaptor(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
this.setContext(context);
this.listIDs = objects;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(listIDs != null)
return listIDs.size();
else
return 0;
}
@Override
public String getItem(int arg0) {
// TODO Auto-generated method stub
if(listIDs != null)
return listIDs.get(arg0);
else
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder viewHolder=new ViewHolder();
LayoutInflater inflater = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(vi==null){
vi = inflater.inflate(R.layout.list_row, parent, false);
viewHolder.id=(TextView)vi.findViewById(R.id.title);
viewHolder.thumbnailImage=(ImageView)vi.findViewById(R.id.list_image);
viewHolder.arrow=(ImageView)vi.findViewById(R.id.list_arrow);
vi.setTag(viewHolder);
}
else
viewHolder=(ViewHolder) vi.getTag();
// viewHolder.thumbnailImage.setOnClickListener(new OnClickListener(){
//
// @Override
// public void onClick(View arg0) {
// // TODO Auto-generated method stub
// Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// ((Activity)context).startActivityForResult(cameraIntent, CAMERA_REQUEST);
// }
//
// });
// Setting all values in listview
viewHolder.id.setText(listIDs.get(position));
return vi;
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
}