我使用了GridView,列数为1,并使用了适配器,我在其中使用了recyclelerView进行水平滚动。为了清楚起见,我有一个gridview作为在适配器中使用recyclerview的主视图。所有元素都在视图中显示为相应的但在我滚动后我得到的位置不是正确的位置。回收器视图的适配器是:
public class HorizontalRecyclerViewAdapter extends RecyclerView.Adapter<HorizontalRecyclerViewAdapter.ViewHolder>{
private Context context;
private ArrayList<Movie> movieList;
public HorizontalRecyclerViewAdapter(Context context,
ArrayList<Movie> objects) {
this.context = context;
this.movieList = objects;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_item, null);
return new ViewHolder(v);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Movie currentMovie = movieList.get(position);
holder.textView.setText(currentMovie.getMovieName());
//Logger.e("THE MOVIE NAME" , currentMovie.getMovieName());
try {
UrlImageViewHelper.setUrlDrawable(holder.imageView, currentMovie.getMoviePictureLinkString(), R.drawable.placeholder_logo);
} catch (Exception e) {
holder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.placeholder_logo));
}
try {
if (currentMovie.getMoviePurchaseDetail() == Movie.PURCHASE_BOUGHT) {
holder.tagImage.setBackground(context.getResources().getDrawable(R.drawable.tag_upgrade));
} else if (currentMovie.getMoviePurchaseDetail() == Movie.PURCHASE_BUY) {
holder.tagImage.setBackground(context.getResources().getDrawable(R.drawable.tag_buy));
} else if(currentMovie.getMoviePurchaseDetail() == Movie.PURCHASE_DEFAULT) {
holder.tagImage.setBackgroundResource(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return movieList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private ImageView imageView, tagImage;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
tagImage = (ImageView) itemView.findViewById(R.id.tag);
}
}
对于on item项目点击回收站视图,我这样做:
HorizontalRecyclerViewAdapter adapter = new HorizontalRecyclerViewAdapter(context, categoryMovieList);
holder.hListView.setAdapter(adapter);
final GestureDetector mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
holder.hListView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
if (child != null && mGestureDetector.onTouchEvent(motionEvent)) {
int position = recyclerView.getChildPosition(child);
final Movie movie = categoryMovieList.get(position);
if(movie.getMoviePurchaseDetail() == Movie.PURCHASE_DEFAULT) {
LinkConfig
.loadMovieLink(context, movie);
}else if(movie.getMoviePurchaseDetail()==Movie.PURCHASE_BOUGHT){
final CustomDialogManager bought_dialog = new CustomDialogManager(
context,
"You have already bought this movie. Please Upgrade",
CustomDialogManager.MESSAGE);
bought_dialog.build();
bought_dialog.show();
bought_dialog.setPositiveButton("PLAY NOW", new View.OnClickListener() {
@Override
public void onClick(View v) {
LinkConfig
.loadMovieLink(context, movie);
bought_dialog.dismiss();
}
});
bought_dialog.setNegativeButton("OK",
new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bought_dialog.dismiss();
MultipleUsedMethods.showBuyDialog(
context, movie);
}
});
}else {
MultipleUsedMethods.showBuyDialog(
context, movie);
}
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
这可能是什么原因?
答案 0 :(得分:3)
private Context context;
private ArrayList<Movie> movieList;
public HorizontalRecyclerViewAdapter(Context context,
ArrayList<Movie> objects) {
this.context = context;
this.movieList = objects;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_item, null);
return new ViewHolder(v);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Movie currentMovie = movieList.get(position);
holder.textView.setText(currentMovie.getMovieName());
//Logger.e("THE MOVIE NAME" , currentMovie.getMovieName());
try {
UrlImageViewHelper.setUrlDrawable(holder.imageView, currentMovie.getMoviePictureLinkString(), R.drawable.placeholder_logo);
} catch (Exception e) {
holder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.placeholder_logo));
}
try {
if (currentMovie.getMoviePurchaseDetail() == Movie.PURCHASE_BOUGHT) {
holder.tagImage.setBackground(context.getResources().getDrawable(R.drawable.tag_upgrade));
} else if (currentMovie.getMoviePurchaseDetail() == Movie.PURCHASE_BUY) {
holder.tagImage.setBackground(context.getResources().getDrawable(R.drawable.tag_buy));
} else if(currentMovie.getMoviePurchaseDetail() == Movie.PURCHASE_DEFAULT) {
holder.tagImage.setBackgroundResource(0);
}
} catch (Exception e) {
e.printStackTrace();
}
holder.v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Put your code here
}
});
}
@Override
public int getItemCount() {
return movieList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private ImageView imageView, tagImage;
View v;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
tagImage = (ImageView) itemView.findViewById(R.id.tag);
v = itemView;
}
}
答案 1 :(得分:2)
您也可以使用
holder.getAdapterPosition();
答案 2 :(得分:2)
首先将以下代码添加到适配器类:
public class HorizontalRecyclerViewAdapter extends RecyclerView.Adapter<HorizontalRecyclerViewAdapter.ViewHolder>{
public interface OnMyItemClickLitner{
void onItemClick(View v, int position);
}
private OnMyItemClickLitner mOnMyItemClickLitner;
private Context context;
private ArrayList<Movie> movieList;
public HorizontalRecyclerViewAdapter(Context context,
ArrayList<Movie> objects, OnMyItemClickLitner onMyItemClickLitner) {
this.context = context;
this.movieList = objects;
mOnMyItemClickLitner = onMyItemClickLitner;
}
//your code
}
然后更改ViewHolder类,如下所示:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textView;
private ImageView imageView, tagImage;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
tagImage = (ImageView) itemView.findViewById(R.id.tag);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mOnMyItemClickLitner.onItemClick(v, getAdapterPosition());
}
}
然后在您的活动中调用适配器构造函数,如下所示:
HorizontalRecyclerViewAdapter adapter = new HorizontalRecyclerViewAdapter(context, categoryMovieList, new HorizontalRecyclerViewAdapter.OnMyItemClickLitner() {
@Override
public void onItemClick(View v, int position) {
//your code goes here
}
});
答案 3 :(得分:0)
holder.getAdapterPosition();
或holder.setIsRecyclable(false);
您可以使用其中任何一个。