我只是简单地将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>
答案 0 :(得分:2)
我有同样的问题。经过多次尝试,然后我注意到xml属性以某种方式禁用了ItemView clickListener。因此,我从布局中删除了以下代码,它现在可以正常工作。
android:clickable="true"
android:focusable="true"
按照我的经验,这绝对不错。
答案 1 :(得分:1)
尝试以下一个代码
public class ViewAdapter extends RecyclerView.Adapter<ViewAdapter.Holder> {
private List<Model> list;
private Context mContext;//Init Context
public ViewAdapter(List<Model> list,Context _context) {
this.list = list;
this.mContext = _context;//getting your activity context
}
@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.mParent.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(mContext, "file not found", Toast.LENGTH_SHORT).show();//here your using context
}
}
});
}
@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;
CardView mParent;
// TextView textView;
public Holder(final View itemView) {
super(itemView);
this.itemView = itemView;
// textView = (TextView) itemView.findViewById(R.id.textview);
mParent = (CardView)itemView.findViewById(R.id.cardview);
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);
}
}
}
答案 2 :(得分:0)
在Adapter的构造函数中,传递getActivity()代替了getContex()为我工作
答案 3 :(得分:0)
将更新xml从RelativeLayout更改为LinearLayout
答案 4 :(得分:0)
尝试此代码。
ViewAdapter
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;
private OnItemClickListener listener;
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);
}
@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);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if (listener != null && position != RecyclerView.NO_POSITION)
{
listener.OnItemClick(getItem(position));
}
}
});
}
}
public interface OnItemClickListener {
void OnItemClick(ChildActivityList item);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
}
Activity.Class 在您的活动中,只需调用适配器和所有if-else条件即可
adater.setOnItemClickListener(new OnItemClickListener);