在Cardview中的TextView上安装android – setOnClickListener

在Cardview中的TextView上安装android – setOnClickListener,第1张

概述我在Cardview内的TextView上实现setOnClickListener时遇到了一些麻烦.我使用这个Cardview来填充一个recicleview. 我已经尝试在onBindViewHolder中设置监听器,但我看不到日志. 单品 <?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_height 我在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;}@OverrIDepublic 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);}@OverrIDepublic 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);    }}@OverrIDepublic 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.

@OverrIDepublic String toString() {     return super.toString() + " '" + nomeUser.getText();}
总结

以上是内存溢出为你收集整理的在Cardview中的TextView上安装android – setOnClickListener全部内容,希望文章能够帮你解决在Cardview中的TextView上安装android – setOnClickListener所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1123790.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存