我试图将ImageButton添加到ListView中的每一行(我通过CustomAdapter实现)。每个按钮应该打开一个不同的WebView。我似乎无法找到任何教程来做到这一点,所以任何建议/提示/链接都会高度贬值。
编辑1:
我的Listview XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_x="15dp">
<ImageView
android:layout_width="30dp"
app:srcCompat="@drawable/a"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:id="@+id/local"
android:layout_height="30dp"
android:layout_alignBottom="@+id/versus"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/resultsiz"
android:textColor="@android:color/white"
android:textStyle="normal|bold"
android:gravity="center_vertical"
android:elevation="15dp"
android:layout_alignBaseline="@+id/resultsde"
android:layout_alignBottom="@+id/resultsde"
android:layout_alignRight="@+id/resultsde"
android:layout_alignEnd="@+id/resultsde"
android:layout_marginRight="61dp"
android:layout_marginEnd="61dp" />
<ImageView
android:layout_width="15dp"
app:srcCompat="@drawable/a"
android:id="@+id/versus"
android:layout_height="15dp"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp"
android:layout_alignBottom="@+id/visit"
android:layout_toRightOf="@+id/local"
android:layout_toEndOf="@+id/local" />
<ImageView
android:layout_width="30dp"
app:srcCompat="@drawable/a"
android:id="@+id/visit"
android:layout_height="30dp"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_alignBottom="@+id/resultsiz"
android:layout_toRightOf="@+id/versus"
android:layout_toEndOf="@+id/versus" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/local"
android:layout_alignStart="@+id/local"
android:id="@+id/dia"
android:textColor="@color/white"
android:textStyle="normal|bold"
android:textAllCaps="true" />
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="@+id/resultsde"
android:layout_marginRight="24dp"
android:layout_marginEnd="24dp"
android:textColor="@android:color/white"
android:layout_marginTop="19dp"
android:textStyle="normal|bold"
android:textAlignment="viewEnd"
android:gravity="end"
android:layout_width="70dp"
android:layout_below="@+id/dia"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/local"
android:layout_toRightOf="@+id/local"
android:layout_toEndOf="@+id/local"
android:layout_marginTop="155dp"
android:id="@+id/stats" />
</RelativeLayout>
我的CustomAdapter:
protected void onPostExecute(Void aVoid) {
CustomAdapter customAdapter = new CustomAdapter();
lista.setAdapter(customAdapter);
}
class CustomAdapter extends BaseAdapter{
@Override
public int getCount() {
return resultsizq.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
view = getLayoutInflater().inflate(R.layout.customlayout,null);
ImageView versus = (ImageView)view.findViewById(R.id.versus);
ImageView local = (ImageView)view.findViewById(R.id.local);
ImageView visit = (ImageView)view.findViewById(R.id.visit);
TextView dia= (TextView)view.findViewById(R.id.dia);
TextView resultsiz= (TextView)view.findViewById(R.id.resultsiz);
TextView resultsde= (TextView)view.findViewById(R.id.resultsde);
dia.setText(di[position]);
resultsiz.setText(resultsizq[position]);
resultsde.setText(resultsder[position]);
versus.setImageResource(versu[position]);
local.setImageResource(loc[position]);
visit.setImageResource(vis[position]);
if ((position+1)%4==0){
view.setPadding(0,0,0,150);
}
if ((position)%4==0){
view.setPadding(0,150,0,0);
}
return view;
}}
答案 0 :(得分:1)
在getView
方法中,您应根据给定的位置将onClick
函数指定给Button
。您可能应该将项目设置为position
,根据此项的属性生成一些网址,并在onClick
函数中打开此网址。
除此之外,我还有3条改进代码的建议: