RecyclerView
의 레이아웃 프리젠 테이션을 목록에서 그리드로 또는 그 반대로 변경하고 싶습니다. 그래서 난 내 onCreateViewHolder
방법을 변경 한 내 기능의 방법RecyclerView의 Android 변경 레이아웃 프리젠 테이션
if (presentation.equals("grid")) {
return LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item_grid, parent, false);
}
else {
return LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item_list, parent, false);
}
, 나는 LayoutManager
다음 방법 notifyDataSetChanged();
if (presentation.equals("grid")) {
recyclerView.setLayoutManager(new GridLayoutManager(context, 3));
}
else {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
}
adapter.notifyDataSetChanged();
을 .setLayoutManager(..)
에서하지만이 기능을 사용할 때, 레이아웃의 일부에서 서로 다른 설정 그리드 레이아웃과 일부는 목록 레이아웃에서.
나는 그 요소가 보이지 않을 때 RecyclerView
요소를 재사용한다는 것을 알고 있지만, 이것을 올바르게 표시하고 요소를 계속 재사용하는 방법을 알고 있습니까?
'LayoutManager'를 바꿀 코드를 보여줍니다 .. !! –
당신의 질문과 관련이 없지만,'presentation'을'String'이 아닌'enum' (타입 안전성을 줄 것입니다)으로 저장하거나'int' 필드를'int'로 저장하는 것을 고려해야합니다 값에 대한 상수 (#enumsmatter를 믿지 않으면 더 나은 성능을 제공합니다). –
그것은 복잡한 인터페이스입니다,하지만 나는이 글의 목적을 위해 간단한 문자열과 IF로 변경하여 여러분을 혼란스럽게하고 싶지 않았습니다. – Lau