본문 바로가기
Android

[Android/Java] expandable 사용(+ getadapterposition deprecated에러)

by noddu 2022. 2. 1.
728x90
반응형

 

<CardView>
<LinearLayout>

...

    <RelativeLayout
        android:id="@+id/relativeLayout_expandable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/board_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="board_content" />
            
    </RelativeLayout>

</LinearLayout>
</CardView>

게시판 내에 content TextView를 넣어 게시물을 눌렀을때 RelativeLayout을 VISIBLE <-> GONE을 반복하게 만들겠다

 

 


 

public RelativeLayout relativeLayout_expandable;
relativeLayout_expandable = view.findViewById(R.id.relativeLayout_expandable);

어댑터에 RelativeLayout을 불러오고

 

 

 


 

 

cardView_content.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        BoardData boardData = boardDataList.get(getBindingAdapterPosition());
        boardData.setExpandable(!boardData.isExpandable());
        notifyItemChanged(getBindingAdapterPosition());
        Log.d("pandable", String.valueOf(boardData.isExpandable()));
    }
});

ViewHolder에서 

 

notifyItemChanged(getAbsoluteAdapterPosition()) - 리스트의 크기와 아이템이 둘 다 변경되는 경우에 사용

 

 

getAdapterPosition()을 사용했더니 getadapterposition deprecated 에러가 발생했다

그럴땐 getBindingAdapterPosition()이나

getAbsoluteAdapterPosition()을 사용하면 된다

 

 

getBindingAdapterPosition() - 바인딩된 항목과 관련하여 이 ViewHolder가 나타내는 항목의 어댑터 위치를 반환

 

getAbsoluteAdapterPosition() -  ViewHolder가 나타내는 항목의 어댑터 위치를 반환(다른 어댑터 내부에 있는 경우 다를 수 있다)

 

 

 

누를때마다 true와 false를 번갈아가며 Log에 출력된다


 

 

boolean isExpandable = boardDataList.get(position).isExpandable();
viewHolder.relativeLayout_expandable.setVisibility(isExpandable ? View.VISIBLE : View.GONE);

onBindViewHolder에서 position의 isExpandable값을 가져와 눌렀을때 VISIBLE과 GONE으로 번갈아가며 실행할것이다

반응형