我正在尝试在Android上实现onClick
侦听器。当我点击时,我需要从GridLayoutManager
获取一个特定项目的详细信息。 onClick
有效,但它列出了数组列表中的所有项目。
适配器类:
public class MoviesGridAdapter extends RecyclerView.Adapter<MoviesGridAdapter.PosterViewHolder> {
private Context context;
private List<Movies> movieItems = new ArrayList<>();
public MoviesGridAdapter(Context context, List<Movies> movieItems) {
this.context = context;
this.movieItems = movieItems;
}
@Override
public PosterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_poster_grid_view_item, parent, false);
return new PosterViewHolder(view);
}
@Override
public void onBindViewHolder(PosterViewHolder holder, int position) {
final Movies movies = movieItems.get(position);
String posterUrlPath = movies.getPosterPath();
Picasso.with(context)
.load(Constants.MOVIE_POSTER_URL + posterUrlPath).into(holder.poster);
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, MovieDetailsActivity.class);
intent.putExtra("Title", movies.getTitle());
intent.putExtra("Overview", movies.getOverview());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return movieItems.size();
}
public class PosterViewHolder extends RecyclerView.ViewHolder {
ImageView poster;
final View mView;
public PosterViewHolder(View itemView) {
super(itemView);
mView = itemView;
poster = (ImageView) itemView.findViewById(R.id.movies_poster_mv);
}
}
public void setMovies(List<Movies> movies) {
movieItems.clear();
movieItems = movies;
notifyDataSetChanged();
}
}
我在ViewHolder
类上尝试了这一点,并使用API中声明的RecyclerView.ViewHolder
扩展了getAdapterPosition()
。我得到了另一个Activity / Fragment的意图,但我没有得到项目的位置点击而不是加载整个列表。这是Github project link。 ListView
有OnItemClickListner
或ArrayAdapter
,RecyclerView
缺乏功能。
提前感谢所有帮助。
答案 0 :(得分:1)
将您的点击监听器放在 PosterViewHolder 中,就像我在下面的示例中所做的那样。
这是我项目的适配器,我的点击工作正常
/**
* Created by Rakshit on 7/20/2016.
*/
public class MyWishListAdapter extends RecyclerView.Adapter<MyWishListAdapter.DataViewHolder> {
private List<UserWishlist> mList;
Activity mContext;
MyWishListFragment fragment;
public MyWishListAdapter(Activity mContext, MyWishListFragment fragment) {
mList = new ArrayList<>();
this.mContext = mContext;
this.fragment = fragment;
}
@Override
public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_wishlist_table_row_item, parent, false);
Constants.setAppFont((ViewGroup) v, parent.getContext());
DataViewHolder dataView = new DataViewHolder(v);
return dataView;
}
@Override
public void onBindViewHolder(final DataViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return mList.size();
}
public class DataViewHolder extends RecyclerView.ViewHolder implements APIServerResponse {
public DataViewHolder(View v) {
super(v);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//View click listener here
}
});
}
}
答案 1 :(得分:1)
在ClickListener上实施
public class MoviesGridAdapter extends RecyclerView.Adapter<MoviesGridAdapter.PosterViewHolder> implements View.OnClickListener {
public PosterViewHolder(View itemView) {
super(itemView);
.......
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
.......
}
答案 2 :(得分:0)
你的做法是错误的尝试这样的事情。
public class YourClassAdapter extends RecyclerView.Adapter<YourClassAdapter.YourClassViewHolder>
implements View.OnClickListener {
private ArrayList<Object> objects;
private View.OnClickListener listener;
public YourClassAdapter(ArrayList<Object> objects) {
this.objects = objects;
}
@Override
public YourClassViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.your_layout, parent, false);
itemView.setOnClickListener(this);
return new YourClassViewHolder(itemView);
}
@Override
public void onBindViewHolder(YourClassViewHolder holder, int position) {
Object object = objects.get(position);
holder.bindObject(object);
}
@Override
public int getItemCount() {
return objects.size();
}
public void setOnClickListener(View.OnClickListener listener) {
this.listener = listener;
}
@Override
public void onClick(View view) {
if(listener != null)
listener.onClick(view);
}
public static class YourClassViewHolder extends RecyclerView.ViewHolder {
public YourClassViewHolder(View itemView) {
super(itemView);
}
public void bindObject(Object object) {
}
}
}
在你的片段/活动中
yourClassAdapter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = recyclerView.getChildAdapterPosition(v);
//Do your things
}
});