我在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项目的位置。我必须得到包含所点击的imageview(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。感谢。
答案 0 :(得分:1)
我已经问过这个问题。您需要实现一个视图持有者:请参阅此帖子: Android GridView clickable ImageView
您只需要重新构建适配器。请参阅上面链接中接受的答案。
答案 1 :(得分:0)
一个简单的解决方案是使用TAG管理列表项:
第一 - &GT;将TAG值设置为具有Click操作的视图:
...
private class CustomAdapter extends ArrayAdapter<RowData> {
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
convertView.findViewById(R.id.btn_youtube).setTag(position);
convertView.findViewById(R.id.btn_music).setTag(position);
return convertView;
}
}
然后,从View:
获取TAG值public void myClickHandler1(View v)
{
int position = (int) v.getTag();
String uri[]={"http://www.youtube.com/watch?v=rMltoD1jCGI", "http://www.youtube.com/watch?v=928obph1wo0", "http://www.youtube.com/watch?v=yFUAuIfJNJg"};
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(uri[position])));
}
public void myClickHandler2(View v)
{
int position = (int) v.getTag();
String uri[]={"http://www.youtube.com/watch?v=1ogTxqidSTU", "http://www.youtube.com/watch?v=IMxDnPI_C98", "http://www.youtube.com/watch?v=koVHN6eO4Xg"};
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(uri[position])));
}