gpt4 book ai didi

android - 在 Cardview 内的 TextView 上设置 OnClickListener

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:22 27 4
gpt4 key购买 nike

我在 Cardview 内的 TextView 上实现 setOnClickListener 时遇到了一些麻烦。我使用此 Cardview 来填充 recicleview。我已经尝试在 onBindViewHolder 中设置监听器,但我看不到日志。

单个项目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:clickable="true">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/fotoUser"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:background="#f9fbff"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:minHeight="300dp" />

<LinearLayout
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_below="@+id/fotoUser"
android:weightSum="1"
android:background="@color/colorPrimary"
android:gravity="center_horizontal">


<ImageView
android:src="@drawable/ic_diaf"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="@+id/imageView2"
android:layout_weight="0.03"
android:layout_gravity="center" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="1.0"
android:id="@+id/txtDiaf"
android:paddingTop="0dp"
android:paddingRight="5dp"
android:textColor="#ffffff"
android:layout_gravity="center" />

<ImageView
android:src="@drawable/ic_tempi"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="@+id/imageView3"
android:layout_weight="0.03"
android:layout_gravity="center" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="1sec"
android:id="@+id/txtTempo"
android:paddingTop="0dp"
android:paddingRight="5dp"
android:textColor="#ffffff"
android:layout_gravity="center" />

<ImageView
android:src="@drawable/ic_iso"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="@+id/imageView4"
android:layout_weight="0.03"
android:layout_gravity="center" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="100"
android:id="@+id/txtIso"
android:paddingTop="0dp"
android:paddingRight="5dp"
android:textColor="#ffffff"
android:layout_gravity="center" />
<ImageView
android:src="@drawable/ic_fl"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="@+id/imageView5"
android:layout_weight="0.03"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="100mm"
android:id="@+id/txtFl"
android:paddingTop="0dp"
android:textColor="#ffffff"
android:layout_gravity="center" />

</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="USERNAME"
android:id="@+id/userName"
android:clickable="true"
android:textStyle="bold|italic"
android:paddingTop="4dp"
android:paddingLeft="2dp"
android:layout_below="@+id/linear"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DESCRIZIONE"
android:id="@+id/descFoto"
android:paddingTop="4dp"
android:paddingLeft="2dp"
android:background="@color/colorPrimary"
android:paddingBottom="3dp"
android:layout_below="@+id/userName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/abc_primary_text_material_dark" />

<ImageView
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:src="@drawable/ic_like"
android:layout_toLeftOf="@+id/likes"
android:layout_alignTop="@+id/descFoto"
android:layout_alignBottom="@+id/descFoto" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NL"
android:id="@+id/likes"
android:paddingTop="4dp"
android:background="@color/colorPrimary"
android:paddingBottom="3dp"
android:layout_below="@+id/userName"
android:textColor="@color/abc_primary_text_material_dark"
android:layout_above="@+id/spazio"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />


<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="@+id/spazio"
android:background="@color/colorPrimaryDark"
android:layout_below="@+id/descFoto"
android:layout_alignParentLeft="true"
/>


<ImageView
android:layout_width="50dp"
android:layout_height="70dp"
android:id="@+id/btnLIKE"
android:clickable="true"
android:src="@drawable/ic_nolike"
android:layout_gravity="bottom"
android:adjustViewBounds="true"
android:paddingBottom="3dp"
android:layout_weight="0.06"
android:layout_above="@+id/linear"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

列表->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="5dp"
android:background="#fffffc">


<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listaFoto"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:dividerHeight="0dp"
android:divider="#fff9fa" /></RelativeLayout>

绑定(bind)->

 public void onBindViewHolder(final SeguitiFragment_FotoADP.FotoViewHolder holder,final int position) {
SeguitiFragment_Foto fotoS = foto.get(position);

//.....
holder.nomeUser.setText(fotoS.getUser());
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});
//.........

}

适配器->

public class SeguitiFragment_FotoADP extends RecyclerView.Adapter<SeguitiFragment_FotoADP.FotoViewHolder>{

private List<SeguitiFragment_Foto> foto;
private Context c;
private String NomeFile;

public SeguitiFragment_FotoADP(List<SeguitiFragment_Foto> foto,Context c) {
this.foto = foto;
this.c=c;
}


@Override
public SeguitiFragment_FotoADP.FotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_subfoto, parent, false);

return new FotoViewHolder(itemView);
}

@Override
public void onBindViewHolder(final SeguitiFragment_FotoADP.FotoViewHolder holder,final int position) {
SeguitiFragment_Foto fotoS = foto.get(position);


if(fotoS.getDiaframma()==-1){
holder.diaf.setText("--");
}else{
holder.diaf.setText(fotoS.getDiaframma()+"");
}

if(fotoS.getIso()==-1){
holder.iso.setText("--");
}else{
holder.iso.setText(fotoS.getIso()+"");
}

holder.nomeUser.setText(fotoS.getUser());
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});
holder.desc.setText(fotoS.getDescrizione());
holder.tempo.setText(fotoS.getTempi()+ "sec");
holder.fl.setText(fotoS.getMillimetri()+"mm");


NomeFile=fotoS.getIdFoto();

File file = new File (c.getFilesDir(), NomeFile+".jpg");
if (!file.exists ()) {
downloadFTP ftp = new downloadFTP(fotoS.getIdFoto(), holder);
ftp.execute();
}else{
settaImmagine(holder);
}



}

@Override
public int getItemCount() {
return foto.size();
}


public static class FotoViewHolder extends RecyclerView.ViewHolder {
public ImageView immagine;
public TextView nomeUser;
public TextView desc;
public TextView diaf;
public TextView tempo;
public TextView iso;
public TextView fl;
public ImageView like;

public FotoViewHolder(View convertView) {
super(convertView);

nomeUser=(TextView)convertView.findViewById(R.id.userName);
desc=(TextView)convertView.findViewById(R.id.descFoto);
immagine=(ImageView)convertView.findViewById(R.id.fotoUser);
diaf=(TextView)convertView.findViewById(R.id.txtDiaf);
iso=(TextView)convertView.findViewById(R.id.txtIso);
fl=(TextView)convertView.findViewById(R.id.txtFl);
tempo=(TextView)convertView.findViewById(R.id.txtTempo);
like=(ImageView) convertView.findViewById(R.id.btnLIKE);


}


public void settaImmagine(FotoViewHolder v){
File file = new File(c.getFilesDir(), NomeFile + ".jpg");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
v.immagine.setImageBitmap(bitmap);

}

谢谢!

最佳答案

Try to set onClickListener after setText on your onBindViewHolder.

引用这个。

 holder.mTextView.setText(mValues.get(position));
holder.mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click", "Effettuato");
}
});

As per your code change this.

holder.nomeUser.setText(fotoS.getUser());
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});

编辑 1:

In your FotoViewHolder overRide this before settaImmagine function.

@Override
public String toString() {
return super.toString() + " '" + nomeUser.getText();
}

关于android - 在 Cardview 内的 TextView 上设置 OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37251643/

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