我在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>
列表 - &GT;
<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-&GT;
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");
}
});
//.........
}
适配器 - &GT;
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);
}
}
谢谢!
答案 0 :(得分:0)
使用 在xml中
<TextView
<-- other attributes -->
android:clickable="true"
/>
适配器中的
nomeUser= (TextView)view.findViewById(R.id.**id**);
最后
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});
答案 1 :(得分:0)
在你的内心
class FotoViewHolder
nomUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});
如果您想识别不同的nomUser
,可以使用此代码获取适配器位置
int pos = getAdapterPosition();
答案 2 :(得分:0)
这是您布局中的TextView:
android:clickable="true"
你没有android:clickable="true"
。将TextView
添加到您需要的holder.nomeUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click","clicked");
}
});
并在你的onBindViewHolder中添加:
window.someObject?.someProperty?.subProperty
答案 3 :(得分:0)
首先在单个项目布局的父级(表示单个项目的父级线性布局)中添加以下属性,
<强>机器人:descendantFocusability = “blocksDescendants”强>
然后尝试如下,
public void onBindViewHolder(final SeguitiFragment_FotoADP.FotoViewHolder holder,final int position)
{
SeguitiFragment_Foto fotoS = foto.get(position);
bind(position,fotoS);
}
现在在 FotoViewHolder 类make方法中,如下所示
public void bind(int pos, SeguitiFragment_Foto fotoS) {
nomeUser.setText(fotoS.getUser());
nomeUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});
}
答案 4 :(得分:0)
尝试在
onClickListener
上setText
之后设置onBindViewHolder
。
请参阅此内容。
holder.mTextView.setText(mValues.get(position));
holder.mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click", "Effettuato");
}
});
根据您的代码更改此内容。
holder.nomeUser.setText(fotoS.getUser());
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});
编辑1:
在
overRide
功能之前的FotoViewHoldersettaImmagine
中。
@Override
public String toString() {
return super.toString() + " '" + nomeUser.getText();
}