我在 ListView 的每个项目上添加了两个图像视图。我想添加这些图像视图 OnClick 操作。我添加android:OnClick="action"
到 list_item.xml 上的 imageview 中。
<?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/listitem"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@drawable/point" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/imageView1"
android:textColor="#000000" />
<ImageView
android:id="@+id/btn_youtube"
android:layout_width="30dp"
android:layout_height="30dp"
android:clickable="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:background="@drawable/youtube"
android:onClick="myClickHandler2" />
<ImageView
android:id="@+id/btn_music"
android:layout_width="30dp"
android:layout_height="30dp"
android:clickable="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/btn_youtube"
android:background="@drawable/musicstore"
android:onClick="myClickHandler1" />
</RelativeLayout>
然后我将 myClickHandler1 和 myClickHandler2 函数添加到我的类中。当我单击 btn_music 时,调用了 myClickHandler1 函数,并且在 btn_youtube 上是相同的。但我无法获得 ListView 项目的位置。我必须获取包含单击的图像视图(btn_music 或 btn_youtube)的列表项的行号位置。我的班级代码:
public class Tab2 extends Activity implements AdapterView.OnItemClickListener{
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
Context context ;
Bitmap bitmap;
public static String title[] = { "song1", "son2", "song3"};
public static Integer[] imgid = {R.drawable.play, R.drawable.play, R.drawable.play};
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.tab2);
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String _status = (String) intent.getSerializableExtra("status");
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for (int i = 0; i < title.length; i++) {
try {
rd = new RowData(i, title[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this,
R.layout.activity_list_view, R.id.list, data);
android.widget.ListView list = (android.widget.ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
}
private class RowData {
protected int mId;
protected String mTitle;
RowData(int id, String title) {
mId = id;
mTitle = title;
}
@Override
public String toString() {
return mId + " " + mTitle;
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource,
int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
ImageView i11 = null;
RowData rowData = getItem(position);
if (null == convertView) {
convertView = mInflater
.inflate(R.layout.activity_list_view, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
title.setText(rowData.mTitle);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private ImageView i11 = null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if (null == title) {
title = (TextView) mRow.findViewById(R.id.title);
}
return title;
}
}
}
public void myClickHandler1(View v)
{
RelativeLayout vwParentRow = (RelativeLayout)v.getParent();
ListView list = (ListView)findViewById(R.id.list);
String uri[]={"http://www.youtube.com/watch?v=rMltoD1jCGI", "http://www.youtube.com/watch?v=928obph1wo0", "http://www.youtube.com/watch?v=yFUAuIfJNJg"};
int position = vwParentRow.indexOfChild(v);
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(uri[position-2])));
}
public void myClickHandler2(View v)
{
RelativeLayout vwParentRow = (RelativeLayout)v.getParent();
String uri[]={"http://www.youtube.com/watch?v=1ogTxqidSTU", "http://www.youtube.com/watch?v=IMxDnPI_C98", "http://www.youtube.com/watch?v=koVHN6eO4Xg"};
ListView list = (ListView)findViewById(R.id.list);
int position = vwParentRow.indexOfChild(v);
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(uri[position-2])));
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
我再次想说我的问题在于 Tab2 类底部的函数。没有堆栈,没有错误。但我无法控制点击了哪个 ListItem 的 imageview。谢谢。