在本例中,我使用GridLayoutManager
。我点击一个项目,然后在它下面会显示一些图片。
我在Adapter的资源中添加了三个数据,然后调用notifyItemRangeInserted(position,3);
然后在项目(位置= 3)下我点击出来三个新项目,我记录他们的位置。三个新项目的位置是4,5,6。但是当我点击7项时,它将其记录为3.我不明白。
Under是我的一些代码。我的英语不太好。请原谅我。
final RecyclerView recyphoto = new RecyclerView(this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (recyphoto.getAdapter().getItemViewType(position)==1){
return 3;
}else {
return 1;
}
}
});
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if(infos.get(position).getType()==1){
final ViewHolderOne holderOne = ((ViewHolderOne)holder);
if(infos.get(position).isExpand()){
holderOne.imageArrow.setImageDrawable(context.getDrawable(R.drawable.arrowdown));
}else{
holderOne.imageArrow.setImageDrawable(context.getDrawable(R.drawable.arrow));
}
TextView tv = holderOne.textView;
tv.setText(infos.get(position).getName());
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<FileInfo> childs = infos.get(position).getChilds();
int childSize = 0;
if(childs!=null){
childSize=childs.size();
}
LogUtil.e(position +"POS");
if(!infos.get(position).isExpand()) {
if(childs!=null) {
infos.addAll(position + 1, childs);
}
infos.get(position).setExpand(true);
notifyItemRangeInserted(position,childSize);
if(onScrollToBottomListener!=null)
onScrollToBottomListener.onBottom(position,infos.size());
}else{
infos.subList(position + 1 , position + 1+childSize).clear();
infos.get(position).setExpand(false);
notifyItemRangeRemoved(position,childSize);
}
}
});
}else if(infos.get(position).getType()==2){
final ViewHolderTwo holderTwo = ((ViewHolderTwo)holder);
Glide.with(context).load(infos.get(position).getLocation()).override(200,200).centerCrop().into( holderTwo.imageView);
holderTwo.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!holderTwo.checkPhoto.isChecked()) {
sendDatas.add(infos.get(position));
holderTwo.checkPhoto.setChecked(true);
holderTwo.checkPhoto.setVisibility(View.VISIBLE);
onSendDataChangeListener.onSendDataChange(sendDatas);
}else{
sendDatas.remove(infos.get(position));
holderTwo.checkPhoto.setChecked(false);
holderTwo.checkPhoto.setVisibility(View.INVISIBLE);
onSendDataChangeListener.onSendDataChange(sendDatas);
}
}
});
if(belong(sendDatas,position)){
holderTwo.checkPhoto.setChecked(true);
holderTwo.checkPhoto.setVisibility(View.VISIBLE);
}else{
holderTwo.checkPhoto.setChecked(false);
holderTwo.checkPhoto.setVisibility(View.INVISIBLE);
}
}
}
@Override
public int getItemCount() {
return infos.size();
}
private class ViewHolderOne extends RecyclerView.ViewHolder{
TextView textView ;
ImageView imageArrow;
ViewHolderOne(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
imageArrow = (ImageView) itemView.findViewById(R.id.image_arrow);
}
}
private class ViewHolderTwo extends RecyclerView.ViewHolder{
ImageView imageView ;
CheckBox checkPhoto;
ViewHolderTwo(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
checkPhoto = (CheckBox) itemView.findViewById(R.id.check_photo);
}
}
@Override
public int getItemViewType(int position) {
return infos.get(position).getType();
}
private boolean belong(List<FileInfo> sendDatas, int pos) {
String str = infos.get(pos).getLocation();
for (int i = 0; i < sendDatas.size(); i++) {
if (str.equals(sendDatas.get(i).getLocation()))
return true;
}
return false;
}
答案 0 :(得分:0)
问题在于您设置OnClickListener
的方式。
在onBindViewHolder()
中,您可以这样写:
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
LogUtil.e(position +"POS");
...
}
};
这里position
是什么?它是在您插入商品之前传递给onBindViewHolder()
的值3
。
要修复此行为,您应该在单击该项目时对该项目的位置进行实时查询。我没有在我面前使用我的IDE,但我相信这会有效:
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
LogUtil.e(holder.getAdapterPosition() +"POS");
...
}
};
holder.getAdapterPosition()
将在点击时确定商品的位置,而非绑定。