하고재비

[Android] Recycler View, StaggeredGrid 본문

Android

[Android] Recycler View, StaggeredGrid

DeadDE 2018. 1. 17. 11:45



주의. project structure -> dependencies 에서 cardview 와 recyclerview 에 대한 라이브러리 추가를 선행할것.



new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);

에서 int 변수가 2냐 3이냐 에 따라서 아래와 같은 결과가 나옴.



리사이클러뷰 StaggeredGrid



MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    //private RecyclerView.LayoutManager mLayoutManager;
 
 
    private static ArrayList<item> itemArrayList;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //DB에서 아이템을 가져와 배열에 저장
        //ArrayList 생성
        itemArrayList = new ArrayList<>();
        //ArrayList에 값 추가하기
        itemArrayList.add(new item("코트"15000"무료배송, 적립금",R.drawable.coats));
        itemArrayList.add(new item("자켓"2200"무료배송, 적립금",R.drawable.jackets));
        itemArrayList.add(new item("니트"35000"무료배송, 적립금",R.drawable.neat));
        itemArrayList.add(new item("메인화면"29000"무료배송, 적립금",R.drawable.main));
        itemArrayList.add(new item("셔츠"21000"무료배송, 적립금",R.drawable.shirts));
        itemArrayList.add(new item("신발"29000"무료배송, 적립금",R.drawable.shoes));
 
 
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);//옵션
        //Linear layout manager 사용
        //mLayoutManager = new LinearLayoutManager(this);
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
 
        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);
        mAdapter = new MyAdapter(itemArrayList);
        mRecyclerView.setAdapter(mAdapter);
    }
 
 
    public class item {
        String name;
        int age;
        String email;
        String photo;
        int photo_id;
 
        public item(String name, int age, String email, String photo, int photo_id) {
            this.name = name;
            this.age = age;
            this.email = email;
            this.photo = photo;
            this.photo_id = photo_id;
        }
        public item(String name, int age, String email, int photo_id) {
            this.name = name;
            this.age = age;
            this.email = email;
            this.photo_id = photo_id;
        }
 
        public String getName() {
            return name;
        }
 
        public int getAge() {
            return age;
        }
 
        public String getEmail() {
            return email;
        }
 
        public String getPhoto() {
            return photo;
        }
        public int getPhoto_id(){
            return photo_id;
        }
    }
}
 
cs

MyAdapter.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
 
    private ArrayList<MainActivity.item> mDataset; //MainActivity에 item class를 정의해 놓았음
 
    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // 사용될 항목들 선언
        public TextView mName;
        public TextView mAge;
        public TextView mEmail;
        public ImageView mPhoto;
 
        public ViewHolder(View v) {
            super(v);
            mPhoto = (ImageView) v.findViewById(R.id.iv_photo);
            mName = (TextView) v.findViewById(R.id.info_text);
            mAge = (TextView) v.findViewById(R.id.info_age);
            mEmail = (TextView) v.findViewById(R.id.info_email);
        }
 
    }
 
    public MyAdapter(ArrayList<MainActivity.item> myDataset) {
        mDataset = myDataset;
    }
 
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
        // set the view's size, margins, paddings and layout parameters
 
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }
 
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
    // Replace the contents of a view (invoked by the layout manager)
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.mName.setText(mDataset.get(position).getName());
        holder.mAge.setText(String.valueOf(mDataset.get(position).getAge())+"원");
        holder.mEmail.setText(mDataset.get(position).getEmail());
        holder.mPhoto.setImageResource(mDataset.get(position).getPhoto_id());
    }
 
    @Override
    public int getItemCount() {
        return mDataset.size();
    }
}
 
cs


activity_main.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.example.user.test_recycle.MainActivity">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-목록-"
        android:id="@+id/tv_title"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_below="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
 
</RelativeLayout>
 
cs



card_item.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    card_view:cardCornerRadius="4dp">
 
    <RelativeLayout
        android:id="@+id/rel_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
 
        <ImageView
            android:id="@+id/iv_photo"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:scaleType="centerInside" />
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="#99FFFFFF"
            android:orientation="vertical">
 
            <TextView
                android:id="@+id/info_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#106897"
                android:textSize="18dp" />
 
            <TextView
                android:id="@+id/info_age"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
 
            <TextView
                android:id="@+id/info_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </RelativeLayout>
 
</android.support.v7.widget.CardView>
 
cs



참고 : https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html

  https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

http://blog.naver.com/PostView.nhn?blogId=cosmosjs&logNo=220961054247&categoryNo=80&parentCategoryNo=0&viewDate=&currentPage=1&postListTopCurrentPage=1&from=postList



Comments