I have vertical recyclerview and is working fine, the problem comes when I i try to make another recyclerview horizontal.My data are displayed correctly but item click lister isnt working. I have tried to check on onClick Method in my adapter surprisingly it is working but listener is null.I have spent my whole day here to look what i have done wrong without success.Here are my codes
This is Other Adapter Class
public class OthersAdapter extends RecyclerView.Adapter<OthersAdapter.ItemViewHolder> {
private static ArrayList<OthersModel> dataList;
private LayoutInflater mInflater;
private Context context;
private OthersClickListener clicklistener = null;
public OthersAdapter(Context ctx, ArrayList<OthersModel> data) {
context = ctx;
dataList = data;
mInflater = LayoutInflater.from(context);
}
public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView image;
private TextView moviename;
private TextView category;
private TextView likes;
private TextView views;
public ItemViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
image = (ImageView) itemView.findViewById(R.id.image);
moviename = (TextView) itemView.findViewById(R.id.txtmoviename);
category = (TextView) itemView.findViewById(R.id.txtcategory);
views = (TextView) itemView.findViewById(R.id.txtviews);
likes = (TextView) itemView.findViewById(R.id.txtlikes);
}
@Override
public void onClick(View v) {
if (clicklistener != null) {
clicklistener.itemClicked(v, getAdapterPosition());
}
}
}
public void setClickListener(OthersClickListener listener) {
this.clicklistener = listener;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_item, parent, false);
ItemViewHolder itemViewHolder = new ItemViewHolder(view);
return itemViewHolder;
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.priority(Priority.HIGH);
Glide.with(context).load(dataList.get(position).getImageUrl())
.apply(options)
.into(holder.image);
holder.moviename.setText(dataList.get(position).getMoviename());
holder.category.setText("Category : "+ dataList.get(position).getCategories());
holder.views.setText(dataList.get(position).getViews());
holder.likes.setText(dataList.get(position).getRating());
}
@Override
public int getItemCount() {
return dataList.size();
}
}
Here is my listerner
public interface OthersClickListener {
void itemClicked(View view, int position);
}
Here is my Activity
public class PlayMoviesActivity extends AppCompatActivity implements OthersClickListener,View.OnClickListener{
....
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
RecyclerView rView = (RecyclerView)findViewById(R.id.recyclerView);
rView.setLayoutManager(layoutManager);
rView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), LinearLayoutManager.VERTICAL));
rcAdapter = new OthersAdapter(getApplicationContext(), (ArrayList<OthersModel>) listModals);
rView.setAdapter(rcAdapter);
......
@Override
public void itemClicked(View view, int position) {
OthersModel dt = listModals.get(position);
Toast.makeText(getApplicationContext(), dt.getMoviename() + " clicked!", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:1)
In the activity, replace getApplicationContext()
with this
and you need to actually set your other listener, otherwise, yes, it's null.
rcAdapter = new OthersAdapter(this, listModals);
rcAdapter.setClickListener(this); // <-- Missed this
rView.setAdapter(rcAdapter);
You could also implement a different constructor
private final ArrayList<OthersModel> dataList;
private final LayoutInflater mInflater;
private final Context context;
private OthersClickListener clicklistener;
public OthersAdapter(Context ctx, List<OthersModel> data) {
this(ctx, data, null);
}
public OthersAdapter(Context ctx, List<OthersModel> data, OthersClickListener listener) {
context = ctx;
dataList = data;
mInflater = LayoutInflater.from(context);
clicklistener = listener;
}
then, in the activity
rcAdapter = new OthersAdapter(this, listModals, this);
Or
rcAdapter = new OthersAdapter(this, listModals, new OthersClickListener() {
});
答案 1 :(得分:0)
To save memory make your ViewHolder static, then pass the onClickListener to the constructor and set it to a private variable (like the adapter itself) and set the click listener to that.
Also pass the clickListener (this
) in the activity to the adapter.
Also make sure your root view in the layout is clickable
and nothing is blocking the clicks. You can double check this with having a background that acts upon clicks and also check with another view inside the layout.