JEP's Diary

RecyclerView 본문

Development/Android

RecyclerView

지으니88 2015. 11. 19. 17:37

RecyclerView

 RecyclerView는 리스트와 유사하지만, 동적으로 변하는 리스트나 large sets of views에 더 적합하다.

LayoutManager - 아이템의 항목 배치



1. 라이브러리 추가

Project Structure > app > Dependencies > + > Library Dependency > cardview, recyclerview 추가


2. 레이아웃 구성

main.xml

1
2
3
4
5
6
7
8
9
10
11
12
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
 
</RelativeLayout>
cs


row_general.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
<LinearLayout    
    xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:card_view="http://schemas.android.com/apk/res-auto"    
    android:orientation="vertical"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent">    
    <android.support.v7.widget.CardView        
        android:id="@+id/cardView"        
        android:layout_width="match_parent"        
        android:layout_height="wrap_content"        
        android:layout_margin="@dimen/row_cardview_margin"        
        card_view:cardCornerRadius="3dp" >        
        <LinearLayout            
            android:layout_width="match_parent"            
            android:layout_height="wrap_content"            
            android:orientation="vertical">            
            <ImageView                
                android:id="@+id/imgView"                
                android:layout_width="match_parent"                
                android:layout_height="wrap_content"                
                android:scaleType="center"                
                android:src="@drawable/ic_launcher"/>            
            <TextView                
                android:id="    @+id/textView"                
                android:layout_width="wrap_content"                
                android:layout_height="wrap_content"                
                android:padding="@dimen/row_textView_padding"                
                android:text="test"/>        
        </LinearLayout>    
    </android.support.v7.widget.CardView>
</LinearLayout>    
cs


3. Adapter 생성

CustomAdapter.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
 
 
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
 
    private Context mContext = null;
    private List<ItemVO> mDataList = null;
    private int mItemLayout;
    private RecyclerOnClickListener mListener = null;
 
    public interface RecyclerOnClickListener {
        void onItemClicked(ItemVO vo, int position);
    }
 
    public CustomAdapter(Context context, List<ItemVO> items, int itemLayout) {
        mContext = context;
        mDataList = items;
        mItemLayout = itemLayout;
    }
 
    public void setOnClickListener(RecyclerOnClickListener listener) {
        mListener = listener;
    }
 
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_general, parent, false);
 
        return new ViewHolder(v);
    }
 
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ItemVO item = mDataList.get(position);
        Drawable drawable = mContext.getResources().getDrawable(item.getImage());
 
        holder.image.setImageResource(item.getImage());
        holder.title.setText(item.getTitle());
        holder.cardView.setOnClickListener(mOnClickListener);
        holder.cardView.setTag(position);
    }
 
    @Override
    public int getItemCount() {
        return mDataList.size();
    }
 
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView title;
        CardView cardView;
 
        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView)itemView.findViewById(R.id.imgView);
            title = (TextView)itemView.findViewById(R.id.textView);
            cardView = (CardView)itemView.findViewById(R.id.cardView);
        }
    }
 
   private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = Integer.parseInt(v.getTag().toString());
 
            if (mListener != null) {
                mListener.onItemClicked(mDataList.get(position), position);
            }
        }
    };
}
 
cs


4. VO

ITemVO.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
 
 
public class ItemVO {
    int image;
    String title;
 
    public ItemVO(int image, String title) {
        this.image = image;
        this.title = title;
    }
 
    public int getImage() {
        return image;
    }
 
    public void setImage(int image) {
        this.image = image;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
}
 
 
 
cs


5.Activity 

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
public class MainActivity extends Activity {
 
    private RecyclerView recyclerView = null;
    private LinearLayoutManager layoutMgr = null;
 
    private List<ItemVO> mDataList = null;
    private CustomAdapter mAdapter = null;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        init();
        initLayout();
    }
 
    /**
     * 초기화
     */
    private void init() {
        mDataList = new ArrayList<>();
        mDataList.add(new ItemVO(R.drawable.ic_launcher,"test1"));
        mDataList.add(new ItemVO(R.drawable.ic_launcher,"test1"));
        mDataList.add(new ItemVO(R.drawable.ic_launcher,"test1"));
        mDataList.add(new ItemVO(R.drawable.ic_launcher,"test1"));
        mDataList.add(new ItemVO(R.drawable.ic_launcher,"test1"));
        mDataList.add(new ItemVO(R.drawable.ic_launcher,"test1"));
    }
 
    /**
     * 레이아웃 초기화
     */
    private void initLayout() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler);
        layoutMgr = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutMgr);
 
        mAdapter = new CustomAdapter(this, mDataList, 0);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnClickListener(mListener);
 
    }
 
    private CustomAdapter.RecyclerOnClickListener mListener = new CustomAdapter.RecyclerOnClickListener() {
        @Override
        public void onItemClicked(ItemVO vo, int position) {
            Log.d(""">>> onClick position : " + position);
        }
    };
}
 
 
 
cs