【发布时间】:2017-04-28 06:29:40
【问题描述】:
我只是简单地实现 setOnClickListener 到适配器相同的 setOnClickListener 一天前工作,
几天前我遇到了同样的问题,但我做了另一个适配器,它成功了,但今天我又遇到了同样的问题
虽然我用谷歌搜索但确实得到了答案,我在这里发布问题,问我经常遇到我犯错的原因是什么,并记住这一点以获取更多代码。
适配器类
package "";
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.android.Youtubedownloader.R;
import com.android.Youtubedownloader.activities.CustomPlayer;
import com.android.Youtubedownloader.database.Model;
import com.tonyodev.fetch.Fetch;
import com.tonyodev.fetch.request.RequestInfo;
import java.util.List;
import static com.android.Youtubedownloader.service.AppService.fetch;
public class ViewAdapter extends RecyclerView.Adapter<ViewAdapter.Holder> {
private List<Model> list;
public ViewAdapter(List<Model> list) {
this.list = list;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item, parent, false));
}
@Override
public void onBindViewHolder(final Holder holder, final int position) {
final Model model = list.get(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (model.getStatus().equalsIgnoreCase("done")) {
if (!model.getFilePath().equals("") && model.getFilePath() != null) {
/* Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(model.getFilePath()));
intent.setDataAndType(Uri.parse(model.getFilePath()), "video*//*");*/
Intent intent = new Intent(holder.itemView.getContext(), CustomPlayer.class);
intent.putExtra("path", model.getFilePath());
holder.itemView.getContext().startActivity(intent);
}
} else {
Toast.makeText(holder.itemView.getContext(), "file not found", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class Holder extends RecyclerView.ViewHolder {
TextView title, percentage, downloaded;
ProgressBar progressBar;
ImageView pause, resume;
LinearLayout bottom;
View itemView;
// TextView textView;
public Holder(final View itemView) {
super(itemView);
this.itemView = itemView;
// textView = (TextView) itemView.findViewById(R.id.textview);
title = (TextView) itemView.findViewById(R.id.video_title);
percentage = (TextView) itemView.findViewById(R.id.txt_percentage);
downloaded = (TextView) itemView.findViewById(R.id.txt_downloaded);
progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
pause = (ImageView) itemView.findViewById(R.id.img_pause);
resume = (ImageView) itemView.findViewById(R.id.img_resume);
bottom = (LinearLayout) itemView.findViewById(R.id.bottom);
}
}
}
适配器项 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:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/img_app"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/margin_5">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:src="@drawable/ic_download"
android:tint="@color/youtube" />
<TextView
android:id="@+id/txt_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:gravity="center_vertical"
android:padding="2dp"
android:text="0%" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="10dp"
android:layout_toLeftOf="@+id/bottom"
android:layout_toRightOf="@+id/img_app"
android:orientation="vertical">
<TextView
android:id="@+id/video_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:text="video title" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="100" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/txt_downloaded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:text="downloaded:" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical">
<ImageView
android:id="@+id/img_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_margin="5dp"
android:background="@color/youtube"
android:clickable="true"
android:padding="15dp"
android:src="@mipmap/ic_pause_circle_filled_white_24dp"
android:tint="@color/white"
android:visibility="visible" />
<ImageView
android:id="@+id/img_resume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_margin="5dp"
android:background="@color/youtube"
android:clickable="true"
android:padding="15dp"
android:src="@mipmap/ic_play_circle_filled_white_24dp"
android:tint="@color/white"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
更新
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("click", "click working");
Toast.makeText(holder.itemView.getContext(), "file not found", Toast.LENGTH_SHORT).show();
if (model.getStatus().equalsIgnoreCase("done")) {
if (!model.getFilePath().equals("") && model.getFilePath() != null) {
/* Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(model.getFilePath()));
intent.setDataAndType(Uri.parse(model.getFilePath()), "video*//*");*/
Intent intent = new Intent(holder.itemView.getContext(), CustomPlayer.class);
intent.putExtra("path", model.getFilePath());
holder.itemView.getContext().startActivity(intent);
}
} else {
Toast.makeText(holder.itemView.getContext(), "file not found", Toast.LENGTH_SHORT).show();
}
}
});
更新 xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
【问题讨论】:
-
如果反对者解释一下我会更好'
-
这段代码很好,onclick 监听器可以在这种情况下工作。请检查您的 if 条件。
-
@anddevmanu 但它不起作用
-
在 if 条件之前和 if 条件之后添加日志,以便您知道结果
-
@anddevmanu 试过但没用
标签: android android-recyclerview onclicklistener