XML 부분
Recycler View 안에 들어갈 아이템들 디자인
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/collect_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/imageView2"
android:text="TextView" />
<TextView
android:id="@+id/collect_grade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/collect_title"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="-76dp"
android:layout_toRightOf="@id/imageView2"
android:text="TextView" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:adjustViewBounds="true"></ImageView>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Recycler View Activity XML부분
recyclerView를 넣어주시면 됩니다. TextView, space는 제꺼 디자인 부분이여서 안넣어도 됩니다.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/bg_round_rect"
android:gravity="center_vertical"
android:text="모아보기"
android:textColor="#9b92b3"
android:textSize="24sp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/bg_round_rect"
tools:layout_editor_absoluteX="101dp"
tools:layout_editor_absoluteY="137dp">
</androidx.recyclerview.widget.RecyclerView>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
자바 부분
Data Class 부분
package com.example.gui.adapter;
public class collect_data {
private String Day;
private String example;
public collect_data(String day, String example){
this.Day = day;
this.example = example;
}
public String getDay(){
return Day;
}
public void setDay(String day){
this.Day = day;
}
public String getExample(){
return example;
}
public void setExample(){
this.example = example;
}
}
adapter부분
package com.example.gui.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.gui.R;
import java.util.ArrayList;
public class collect_adapter extends RecyclerView.Adapter<collect_adapter.ItemViewHolder> {
private ArrayList<collect_data> listData = null;
public collect_adapter(ArrayList<collect_data> datalist){
listData = datalist;
}
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.collect_recyclerview, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull collect_adapter.ItemViewHolder holder, int position) {
holder.onBind(listData.get(position));
}
@Override
public int getItemCount() {
return listData.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView textView1;
private TextView textView2;
ItemViewHolder(View itemView) {
super(itemView);
textView1 = itemView.findViewById(R.id.collect_title);
textView2 = itemView.findViewById(R.id.collect_grade);
}
void onBind(collect_data data) {
textView1.setText(data.getDay());
textView2.setText(data.getExample());
}
}
}
activity부분
저는 Fragment로 해서 getActivity()-> this, RecyclerView recyclerview = findViewById(R.id.recycler_view); 로하시면 됩니다.
public class CollectView extends Fragment {
private ArrayList<collect_data> dataList;
public CollectView(){/*Construct*/}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.activity_collect,container,false); //container <-부모 사이즈를 주고 , false=아직 붙이지 않는다.
// init
this.init();
// RecyclerView set
RecyclerView recyclerView = (RecyclerView)rootView.findViewById(R.id.recycler_view);
LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(manager); // LayoutManager 등록
recyclerView.setAdapter(new collect_adapter(dataList)); // Adapter 등록
return rootView;
}
// Init Function
public void init(){
dataList = new ArrayList<>();
dataList.add(new collect_data("테스트", "테스트2"));
}
}
결과
'안드로이드 > 잡다한 지식' 카테고리의 다른 글
ViewModel 및 사용 (0) | 2021.07.12 |
---|---|
안드로이드 스튜디오 EditText 밑줄 제거 (0) | 2021.03.02 |
안드로이드 스튜디오 intent로 bitmap 넘기기 (0) | 2021.01.30 |
안드로이드 스튜디오 내부 데이터베이스 연결하기 (0) | 2021.01.25 |
안드로이드 앱에서 API 요청하기 (Flask 사용) (0) | 2021.01.19 |