gpt4 book ai didi

android - 如何在 ListView 中为 ImageView 设置 onClickListener

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:41 25 4
gpt4 key购买 nike

关于这个话题有很多讨论。但他们都不适合我。我在 ListView 中实现了一个带有 ImageView 的自定义 ListView,ListView 是从 ListFragment 扩展而来的。我的 ImageView 是可点击的,因此当用户点击 ImageView 时,Camera Activity 会被激活,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;
}


}

最佳答案

    public class MyListFragment extends ListFragment implements OnItemClickListener {
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,this);
ListView lv = (ListView) view.findViewById(android.R.id.list);
lv.setAdapter(adapter);

lv.setOnItemClickListener(this);

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 onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
mCallback.itemSelected(mListItems[position]);

}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {


}

public void imagepos(int i){
//onImage click comes here
}

}
-----------------------------------------------------
public class customList_Adaptor extends ArrayAdapter<String>{


protected static final int CAMERA_REQUEST = 0;

private Context context;
MyListFragment mMyListFragment;
private List<String> listIDs = new ArrayList<String>();

public customList_Adaptor(Context context, int textViewResourceId,
List<String> objects, MyListFragment mMyListFragment) {
super(context, textViewResourceId, objects);
this.setContext(context);
this.listIDs = objects;
this.mMyListFragment= mMyListFragment
}

@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 onMyClick(position));

// Setting all values in listview
viewHolder.id.setText(listIDs.get(position));

return vi;
}

public class onMyClick implements OnClickListener {

private final int pos;
public onMyClick(int pos) {
this.pos = pos;
}

@Override
public void onClick(View v) {
mMyListFragment.imagepos(pos);
}

}

public Context getContext() {
return context;
}

public void setContext(Context context) {
this.context = context;
}


}

关于android - 如何在 ListView 中为 ImageView 设置 onClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17268110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com