728x90
v7 지원 라이브러리 리사이클러뷰 예시
developer.android.com/guide/topics/ui/layout/recyclerview
<메인>
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public RecyclerView recyclerView;
ArrayList<Item> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
// RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); //상하
// RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this,
// LinearLayoutManager.HORIZONTAL,false); //좌우
// RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 3); //3개 상하 나오게
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 3,
LinearLayoutManager.HORIZONTAL,false); //3개 좌우 나오게
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new MyAdapter(insertItem()));
}
ArrayList insertItem()
{
itemList = new ArrayList<>();
...
return itemList;
}
}
<adapter>
class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private ArrayList<String> itemList;
public MyAdapter(ArrayList<String> itemList) {
this.itemList = itemList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.item_view,parent,false);
return new ViewHolder(view);
}//아이템 뷰로 사용 될 xml inflate 시킴
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.textView.setText(itemList.get(position)); //viewHolder 객체
}//뷰안에 필요한 정보 채움
@Override
public int getItemCount() {//data set 전체크기
return itemList.size();
}
//ViewHolder 클래스
class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.item_text); //파라메타 id 찾기
}
}
}
<메인 xml>
androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="409dp"
android:layout_height="729dp"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
<아이템 xml>
<LinearLayout
android:id="@+id/item_layout"
android:layout_width="155dp"
android:layout_height="231dp"
android:orientation="vertical"
android:layout_gravity="center"
android:marginRight="20dp">
<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="136dp" />
<TextView
android:id="@+id/item_text"
android:layout_width="155dp"
android:layout_height="92dp"
android:gravity="center"
android:text="문구"
android:textSize="30dp" />
</LinearLayout>
androidx RecyclerView
developer.android.com/jetpack/androidx/releases/recyclerview
API 29 버전 Jetpack 라이브러리 사용
allprojects {
repositories {
google()
jcenter()
}
}
아티팩트 | 현재 안정화 버전 | 다음 버전 후보 | 베타 버전 | 알파 버전 |
recyclerview | 1.1.0 | - | - | 1.2.0-alpha05 |
recyclerview-selection | 1.0.0 | 1.1.0-rc02 | - | - |
이 라이브러리는 2020년 9월 2일에 최종 업데이트되었습니다.
dependencies {
implementation "androidx.recyclerview:recyclerview:1.1.0"
// For control over item selection of both touch and mouse driven selection
implementation "androidx.recyclerview:recyclerview-selection:1.1.0-rc03"
}
댓글