2016-10-19 2 views
1

Recycler보기에는 각 행에 두 개의 텍스트 필드가 있습니다. 하나의 텍스트 뷰를 클릭하면 확장 가능한 레이아웃이 확장됩니다.이 확장 가능한 레이아웃은 스크롤 할 때 복제됩니다.Android 리사이클보기 행 항목 스크롤시 중복보기 관련 레이아웃 확장

가 여기 내 어댑터 클래스입니다

public class AlbumsAdapter extends RecyclerView.Adapter<AlbumsAdapter.MyViewHolder> { 

private Context mContext; 
private List<Album> albumList; 

RecyclerView rv; 
public class MyViewHolder extends RecyclerView.ViewHolder { 
    public TextView title, count; 
    public ImageView thumbnail, overflow; 
    ExpandableRelativeLayout expandableLayout11; 


    public MyViewHolder(final View view) { 
     super(view); 
     title = (TextView) view.findViewById(R.id.title); 
     count = (TextView) view.findViewById(R.id.count); 
     thumbnail = (ImageView) view.findViewById(R.id.thumbnail); 
     expandableLayout11 = (ExpandableRelativeLayout)view. findViewById(R.id.expandableLayout11); 


    } 
} 



public AlbumsAdapter(Context mContext, List<Album> albumList) { 
    this.mContext = mContext; 
    this.albumList = albumList; 

} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View itemView = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.album_card, parent, false); 

    return new MyViewHolder(itemView); 
} 

@Override 
public void onBindViewHolder(final MyViewHolder holder, final int position) { 
    final Album album = albumList.get(position); 
    holder.title.setText(album.getName()); 
    holder.count.setText(album.getNumOfSongs() + " songs"); 

holder.title.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
      holder.expandableLayout11.toggle(); 

    } 

}); 


    } 

@Override 
public int getItemCount() { 
    return albumList.size(); 
} 

} 

내 주요 활동은 재사용 RecyclerView에서

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main2); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setHomeButtonEnabled(true); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    initCollapsingToolbar(); 

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 

    albumList = new ArrayList<>(); 
    adapter = new AlbumsAdapter(this, albumList); 

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this); 
    recyclerView.setLayoutManager(mLayoutManager); 
    recyclerView.addItemDecoration(new GridSpacingItemDecoration(1, dpToPx(10), true)); 
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 
    recyclerView.setAdapter(adapter); 

    prepareAlbums(); 

    try { 
     Glide.with(this).load(R.drawable.cover).into((ImageView) findViewById(R.id.backdrop)); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

/** 
* Initializing collapsing toolbar 
* Will show and hide the toolbar title on scroll 
*/ 
private void initCollapsingToolbar() { 
    final CollapsingToolbarLayout collapsingToolbar = 
      (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar); 
    collapsingToolbar.setTitle("Developer List"); 
    AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar); 
    appBarLayout.setExpanded(true); 

    // hiding & showing the title when toolbar expanded & collapsed 
    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { 
     boolean isShow = false; 
     int scrollRange = -1; 

     @Override 
     public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { 
      if (scrollRange == -1) { 
       scrollRange = appBarLayout.getTotalScrollRange(); 
      } 
      if (scrollRange + verticalOffset == 0) { 
       collapsingToolbar.setTitle("Developer List"); 
       isShow = true; 
      } else if (isShow) { 
       collapsingToolbar.setTitle("Developer List"); 
       isShow = false; 
      } 
     } 
    }); 
} 


private void prepareAlbums() { 
    int[] covers = new int[]{ 
      R.drawable.album1, 
      R.drawable.album2, 
      R.drawable.album3, 
      R.drawable.album4, 
      R.drawable.album5, 
      R.drawable.album6, 
      R.drawable.album7, 
      R.drawable.album8, 
      R.drawable.album9, 
      R.drawable.album10, 
      R.drawable.album11}; 

    Album a = new Album("True Romance", 13, covers[0]); 
    albumList.add(a); 

    a = new Album("Xscpae", 8, covers[1]); 
    albumList.add(a); 

    a = new Album("Maroon 5", 11, covers[2]); 
    albumList.add(a); 

    a = new Album("Born to Die", 12, covers[3]); 
    albumList.add(a); 

    a = new Album("Honeymoon", 14, covers[4]); 
    albumList.add(a); 

    a = new Album("I Need a Doctor", 1, covers[5]); 
    albumList.add(a); 

    a = new Album("Loud", 11, covers[6]); 
    albumList.add(a); 

    a = new Album("Legend", 14, covers[7]); 
    albumList.add(a); 

    a = new Album("Hello", 11, covers[8]); 
    albumList.add(a); 

    a = new Album("Greatest Hits", 17, covers[9]); 
    albumList.add(a); 

    adapter.notifyDataSetChanged(); 
} 
} 
+0

가능한 복제본 [RecyclerView 모든 몇 가지 항목이 동일합니다 - 확장 항목] (http://stackoverflow.com/questions/39097764/recyclerview-every-few-items-are-the-same-expandable-item) – Abbas

+0

@Abbas 내 생각 엔 당신은 일을 게시했습니다. 내 대답 아래에 올바른 방법임을 확인하십시오. 이 링크를 확인할 수도 있습니다. http : //stackoverflow.com/questions/37629004/selected-image-button-changes-its-position-in-recyclerview-when-scrolling/37630203#37630203 –

답변

0

항목입니다. 따라서 확장 가능한 항목의 위치를 ​​저장해야합니다.
이 그런 다음 Album 모델 클래스에서

int[] expandablePositionArray; // define int array for save all position of expandable items 

public AlbumsAdapter(Context mContext, List<Album> albumList) { 
    this.mContext = mContext; 
    this.albumList = albumList; 
    // init int array 
    expandablePositionList = new int[albumList.size()]; 
} 

@Override 
public void onBindViewHolder(final MyViewHolder holder, final int position) { 
    ... 

    // expand or collapse expandableLayout11 when row load base on expandable position 
    if(expandablePositionList[position] == 1){ 
     holder.expandableLayout11.expand(); 
    }else{ 
     holder.expandableLayout11.collapse(); 
    } 

    holder.title.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      holder.expandableLayout11.toggle(); 

      // save the position of expandable row 
      if(expandablePositionList[position] == 1){ 
       expandablePositionList[position] = 0; 
      }else{ 
       expandablePositionList[position] = 1; 
      } 
    } 
} 
2

boolean 플래그를 추가 할 때 열 부하 expandableLayout의 상태를 변경하는 말 : 그런 다음

// Getter for isExpanded flag 
public boolean isExpanded() { 
return isExpanded; 
} 

// setter for isExpanded flag 
public void setIsExpanded(boolean flag) { 
isExpanded = flag; 
} 

: isExpanded을 동일한에 대한 게터 세터를 추가로 가정 할 수 있습니다 onBindViewHolder 방법을 따르십시오 :

@Override 
public void onBindViewHolder(final MyViewHolder holder, final int position){ 

final Album album = albumList.get(position); 
holder.title.setText(album.getName()); 
holder.count.setText(album.getNumOfSongs() + " songs"); 

if(album.isExpanded()) { 

    holder.expandableLayout11.expand(); 

    } else { 

    holder.expandableLayout11.collapse(); 
    } 

holder.title.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     // toggle view 
     holder.expandableLayout11.toggle(); 
     // toggle isExpanded flag 
     album.setIsExpanded(! album.isExpanded()) 
    } 
} 
+0

의견을 보내 주셔서 감사합니다. 내 생각은 해결 방법이고, 내 생각과 똑같은 원리로 일한다. – Abbas

+0

@Abbas 지속 시간을 0으로 설정하면보기 애니메이션이 지속될 시간을 의미합니다. 그러나 광산 솔루션은 각 행의 내용이나 가시성과 관련이 있는지 여부를 각 상태로 지정해야한다는 리사이클 러 뷰 문서의 내용과 같습니다. –

+0

충분히 자세히 읽으면 상태 목록을 추가하는 것에 대해 언급했습니다. "그 이유는 어댑터 목록의 각 항목에 상태를 연결해야하기 때문입니다."*. – Abbas