반응형

RecyclerView 스태그 그리드

  • 동적으로 데이터 생성
  • 동적으로 2Column, 3Column 변환 기능

선언부

Button btn_regenerate;
Button btn_2col;
Button btn_3col;
RecyclerView recycler;
StaggeredGridLayoutManager staggeredGridLayoutManager;
RecyclerListAdapter mAdapter;

초기화

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

    btn_regenerate = findViewById(R.id.btn_regenerate);
    btn_2col = findViewById(R.id.btn_2col);
    btn_3col = findViewById(R.id.btn_3col);
    recycler = findViewById(R.id.recycler);

    btn_regenerate.setOnClickListener(this);
    btn_2col.setOnClickListener(this);
    btn_3col.setOnClickListener(this);

    //datas
    ArrayList<Object> datas = new ArrayList<>();
    Random random = new Random();
    for(int i=0; i<100; i++) {
        int num = i+1;
        int height = (int)dp2px(50 + random.nextInt(200));
        int color = Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255));
        datas.add(new DataStag(num, height, color));
    }

    staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    staggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
    recycler.setLayoutManager(staggeredGridLayoutManager);
    mAdapter = new RecyclerListAdapter(datas);
    recycler.setAdapter(mAdapter);
}

클릭이벤트

public void onClick(View view) {
    if(view == btn_regenerate) {
        //datas
        ArrayList<Object> datas = new ArrayList<>();
        Random random = new Random();
        for(int i=0; i<100; i++) {
            int num = i+1;
            int height = (int)dp2px(50 + random.nextInt(200));
            int color = Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255));
            datas.add(new DataStag(num, height, color));
        }
        mAdapter.setDatas(datas);
        mAdapter.notifyDataSetChanged();
    } else if(view == btn_2col) {
        StaggeredGridLayoutManager manager = (StaggeredGridLayoutManager)recycler.getLayoutManager();
        manager.setSpanCount(2);
    } else if(view == btn_3col) {
        StaggeredGridLayoutManager manager = (StaggeredGridLayoutManager)recycler.getLayoutManager();
        manager.setSpanCount(3);
    }
}

어뎁터

public class RecyclerListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<Object> mDatas;

    //ViewHolder
    public class NumViewHolder extends RecyclerView.ViewHolder {
        TextView tv_num;
        public NumViewHolder(View view) {
            super(view);
            this.tv_num = view.findViewById(R.id.tv_num);
        }
    }

    public RecyclerListAdapter(ArrayList<Object> list) {
        this.mDatas = list;
    }
    public ArrayList<Object> getDatas() {
        return this.mDatas;
    }
    public void setDatas(ArrayList<Object> datas) {
        this.mDatas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_recycler_stag, viewGroup, false);
        NumViewHolder viewHolder = new NumViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewholder, int position) {
        DataStag dataStag = (DataStag)mDatas.get(position);
        ((NumViewHolder)viewholder).tv_num.setText(dataStag.num + "");
        ((NumViewHolder)viewholder).tv_num.setBackgroundColor(dataStag.color);
        ((NumViewHolder)viewholder).tv_num.getLayoutParams().height = dataStag.height;
    }

    @Override
    public int getItemCount() {
        return (null != mDatas ? mDatas.size() : 0);
    }
}

데이터 클래스

public class DataStag {
    int num;
    int height;
    int color;
    public DataStag(int num, int height, int color) {
        this.num = num;
        this.height = height;
        this.color = color;
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_regenerate"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Regenerate!"/>
        <Button
            android:id="@+id/btn_2col"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:text="2 Column"/>
        <Button
            android:id="@+id/btn_3col"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:text="3 Column"/>
    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

layout_recycler_stag.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_num"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text=""/>

</LinearLayout>

 

 

프로젝트 소스 다운↓

RecyclerStaggered.zip
0.49MB

반응형

'안드로이드' 카테고리의 다른 글

Android adb 사용  (0) 2021.11.14
Android Version List  (1) 2021.11.14
Android RecyclerView  (0) 2021.11.14
TextView Center Line(취소선, 텍스트 중간에 줄긋기 옵션)  (0) 2017.11.24
RoundRect Background xml 만들기  (0) 2017.11.23
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기