Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
In this Example, I use
GridLayoutManager
. I click one item, then under it will show some picture.
I add three data in my Adapter's Resource then call
notifyItemRangeInserted(position,3);
then Under the item(position = 3) i clicked come out three new item and I Log their position. The three new item's position is 4,5,6. But when i click the 7 item, it log it's position as 3. I didn't understand.
Under is Some of my Code. My English is not that good. Please Forgive me.
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;
The problem is the way you're setting up your OnClickListener
.
In onBindViewHolder()
, you write something like this:
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtil.e(position +"POS");
What is position
here? It's the value passed to onBindViewHolder()
, which was 3
before you inserted your items.
To fix the behavior, you should do a live query for the item's position when you click it. I don't have my IDE in front of me, but I believe this will work:
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtil.e(holder.getAdapterPosition() +"POS");
holder.getAdapterPosition()
will determine the item's position at the time of the click instead of at binding.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.