메뉴 건너뛰기

조회 수 7478 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

 

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
<LinearLayout 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"
     android:orientation="horizontal"
     >
     
     <ImageView 
         android:id="@+id/iconItem"  
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content"
          android:padding="8dp"
          android:layout_gravity="center_vertical"
          />
     
      <LinearLayout
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"  
         android:orientation="vertical"
         >
         <TextView  
             android:id="@+id/dataItem01"  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="16dp"
            android:padding="4dp"
            />
        <RelativeLayout
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:padding="4dp"
             >
             <TextView  
                 android:id="@+id/dataItem02"  
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                />
            <TextView  
                 android:id="@+id/dataItem03"  
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:textColor="#ccf88107"
                android:textSize="16dp"
                android:textStyle="bold"
                android:paddingRight="4dp"
                />    
         </RelativeLayout>   
    </LinearLayout>    
    
</LinearLayout>  
 

 

 

 

 

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
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.androidtown.ui.listview;
 
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
 
/**
 * 아이템으로 보여줄 뷰 정의
 * 
 * @author Mike
 *
 */
public class IconTextView extends LinearLayout {
 
    /**
     * Icon
     */
    private ImageView mIcon;
 
    /**
     * TextView 01
     */
    private TextView mText01;
 
    /**
     * TextView 02
     */
    private TextView mText02;
 
    /**
     * TextView 03
     */
    private TextView mText03;
 
    public IconTextView(Context context, IconTextItem aItem) {
        super(context);
 
        // Layout Inflation
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.listitem, thistrue);
 
        // Set Icon
        mIcon = (ImageView) findViewById(R.id.iconItem);
        mIcon.setImageDrawable(aItem.getIcon());
 
        // Set Text 01
        mText01 = (TextView) findViewById(R.id.dataItem01);
        mText01.setText(aItem.getData(0));
 
        // Set Text 02
        mText02 = (TextView) findViewById(R.id.dataItem02);
        mText02.setText(aItem.getData(1));
 
        // Set Text 03
        mText03 = (TextView) findViewById(R.id.dataItem03);
        mText03.setText(aItem.getData(2));
 
    }
 
    /**
     * set Text
     *
     * @param index
     * @param data
     */
    public void setText(int index, String data) {
        if (index == 0) {
            mText01.setText(data);
        } else if (index == 1) {
            mText02.setText(data);
        } else if (index == 2) {
            mText03.setText(data);
        } else {
            throw new IllegalArgumentException();
        }
    }
 
    /**
     * set Icon
     *
     * @param icon
     */
    public void setIcon(Drawable icon) {
        mIcon.setImageDrawable(icon);
    }
 
}
 

 

 

 

 보여지는 리스트에대한 뷰를 리니어 레이아웃으로 만들어서 레이아웃 통체로 리스트에 뿌려줌

 

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package org.androidtown.ui.listview;
 
import android.graphics.drawable.Drawable;
 
/**
 * 데이터를 담고 있을 아이템 정의
 * 
 * @author Mike
 *
 */
public class IconTextItem {
 
    /**
     * Icon
     */
    private Drawable mIcon;
    
    /**
     * Data array
     */
    private String[] mData;
 
    /**
     * True if this item is selectable
     */
    private boolean mSelectable = true;
 
    /**
     * Initialize with icon and data array
     * 
     * @param icon
     * @param obj
     */
    public IconTextItem(Drawable icon, String[] obj) {
        mIcon = icon;
        mData = obj;
    }
 
    /**
     * Initialize with icon and strings
     * 
     * @param icon
     * @param obj01
     * @param obj02
     * @param obj03
     */
    public IconTextItem(Drawable icon, String obj01, String obj02, String obj03) {
        mIcon = icon;
        
        mData = new String[3];
        mData[0] = obj01;
        mData[1] = obj02;
        mData[2] = obj03;
    }
    
    /**
     * True if this item is selectable
     */
    public boolean isSelectable() {
        return mSelectable;
    }
 
    /**
     * Set selectable flag
     */
    public void setSelectable(boolean selectable) {
        mSelectable = selectable;
    }
 
    /**
     * Get data array
     * 
     * @return
     */
    public String[] getData() {
        return mData;
    }
 
    /**
     * Get data
     */
    public String getData(int index) {
        if (mData == null || index >= mData.length) {
            return null;
        }
        
        return mData[index];
    }
    
    /**
     * Set data array
     * 
     * @param obj
     */
    public void setData(String[] obj) {
        mData = obj;
    }
 
    /**
     * Set icon
     * 
     * @param icon
     */
    public void setIcon(Drawable icon) {
        mIcon = icon;
    }
 
    /**
     * Get icon
     * 
     * @return
     */
    public Drawable getIcon() {
        return mIcon;
    }
 
    /**
     * Compare with the input object
     * 
     * @param other
     * @return
     */
    public int compareTo(IconTextItem other) {
        if (mData != null) {
            String[] otherData = other.getData();
            if (mData.length == otherData.length) {
                for (int i = 0; i < mData.length; i++) {
                    if (!mData[i].equals(otherData[i])) {
                        return -1;
                    }
                }
            } else {
                return -1;
            }
        } else {
            throw new IllegalArgumentException();
        }
        
        return 0;
    }
 
}
 

 

 

 실질적으로 보여지는 뷰의 데이터를 가지고있는 클래스

 

 

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package org.androidtown.ui.listview;
 
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
 
/**
 * 리스트뷰를 상속하여 새로 정의한 리스트뷰 클래스
 * 
 * @author Mike
 *
 */
public class DataListView extends ListView {
 
    /**
     * 설정한 어댑터 객체
     */
    private IconTextListAdapter adapter;
    
    /**
     * 설정한 리스너 객체
     */
    private OnDataSelectionListener selectionListener;
    
    public DataListView(Context context) {
        super(context);
 
        init();
    }
 
    public DataListView(Context context, AttributeSet attrs) {
        super(context, attrs);
 
        init();
    }
    
    /**
     * 초기화
     */
    private void init() {
        setOnItemClickListener(new OnItemClickAdapter());
    }
 
    /**
     * 어댑터 설정
     * 
     * @param adapter
     */
    public void setAdapter(BaseAdapter adapter) {
        super.setAdapter(adapter);
 
    }
 
    /**
     * 어댑터 객체 리턴
     * 
     * @return
     */
    public BaseAdapter getAdapter() {
        return (BaseAdapter)super.getAdapter();
    }
    
    /**
     * 리스너 설정
     * 
     * @param listener
     */
    public void setOnDataSelectionListener(OnDataSelectionListener listener) {
        this.selectionListener = listener;
    }
 
    /**
     * 리스너 객체 리턴
     * 
     * @return
     */
    public OnDataSelectionListener getOnDataSelectionListener() {
        return selectionListener;
    }
    
    class OnItemClickAdapter implements OnItemClickListener {
        
        public OnItemClickAdapter() {
            
        }
 
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            
            if (selectionListener == null) {
                return;
            }
            
            // 이벤트 전달
            selectionListener.onDataSelected(parent, v, position, id);
            
        }
        
    }
    
}

 

 

 

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
package org.androidtown.ui.listview;
 
import java.util.ArrayList;
import java.util.List;
 
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
 
/**
 * 어댑터 클래스 정의
 * 
 * @author Mike
 *
 */
public class IconTextListAdapter extends BaseAdapter {
 
    private Context mContext;
 
    private List<IconTextItem> mItems = new ArrayList<IconTextItem>();
 
    public IconTextListAdapter(Context context) {
        mContext = context;
    }
 
    public void addItem(IconTextItem it) {
        mItems.add(it);
    }
 
    public void setListItems(List<IconTextItem> lit) {
        mItems = lit;
    }
 
    public int getCount() {
        return mItems.size();
    }
 
    public Object getItem(int position) {
        return mItems.get(position);
    }
 
    public boolean areAllItemsSelectable() {
        return false;
    }
 
    public boolean isSelectable(int position) {
        try {
            return mItems.get(position).isSelectable();
        } catch (IndexOutOfBoundsException ex) {
            return false;
        }
    }
 
    public long getItemId(int position) {
        return position;
    }
 
    public View getView(int position, View convertView, ViewGroup parent) {
        IconTextView itemView;
        if (convertView == null) {
            itemView = new IconTextView(mContext, mItems.get(position));
        } else {
            itemView = (IconTextView) convertView;
            
            itemView.setIcon(mItems.get(position).getIcon());
            itemView.setText(0, mItems.get(position).getData(0));
            itemView.setText(1, mItems.get(position).getData(1));
            itemView.setText(2, mItems.get(position).getData(2));
        }
 
        return itemView;
    }
 
}
 

 

 

  리스트뷰와 어댑터를 각각 상속한 클래스로 오버라이딩을 사용해 새로운 메소드를 추가하기위해 만듬.

 

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
79
80
81
82
83
84
85
86
package org.androidtown.ui.listview;
 
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Toast;
 
/**
 * 리스트뷰를 사용하는 방법에 대해 알 수 있습니다.
 * 
 * @author Mike
 *
 */
public class MainActivity extends Activity {
 
    /**
     * 리스트뷰 객체
     */
    DataListView list;
    
    /**
     * 어댑터 객체
     */
    IconTextListAdapter adapter;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
 
        // 타이틀 없애기
        requestWindowFeature(Window.FEATURE_NO_TITLE);
 
        // 리스트뷰 객체 생성
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
        list = new DataListView(this);
 
        // 어댑터 객체 생성
        adapter = new IconTextListAdapter(this);
 
        // 아이템 데이터 만들기
        Resources res = getResources();
        adapter.addItem(new IconTextItem(res.getDrawable(R.drawable.icon05), "추억의 테트리스""30,000 다운로드""900 원"));
        adapter.addItem(new IconTextItem(res.getDrawable(R.drawable.icon06), "고스톱 - 강호동 버전""26,000 다운로드""1500 원"));
        adapter.addItem(new IconTextItem(res.getDrawable(R.drawable.icon05), "친구찾기 (Friends Seeker)""300,000 다운로드""900 원"));
        adapter.addItem(new IconTextItem(res.getDrawable(R.drawable.icon06), "강좌 검색""120,000 다운로드""900 원"));
        adapter.addItem(new IconTextItem(res.getDrawable(R.drawable.icon05), "지하철 노선도 - 서울""4,000 다운로드""1500 원"));
        adapter.addItem(new IconTextItem(res.getDrawable(R.drawable.icon06), "지하철 노선도 - 도쿄""6,000 다운로드""1500 원"));
        adapter.addItem(new IconTextItem(res.getDrawable(R.drawable.icon05), "지하철 노선도 - LA""8,000 다운로드""1500 원"));
        adapter.addItem(new IconTextItem(res.getDrawable(R.drawable.icon06), "지하철 노선도 - 워싱턴""7,000 다운로드""1500 원"));
        adapter.addItem(new IconTextItem(res.getDrawable(R.drawable.icon05), "지하철 노선도 - 파리""9,000 다운로드""1500 원"));
        adapter.addItem(new IconTextItem(res.getDrawable(R.drawable.icon06), "지하철 노선도 - 베를린""38,000 다운로드""1500 원"));
 
        // 리스트뷰에 어댑터 설정
        list.setAdapter(adapter);
 
        // 새로 정의한 리스너로 객체를 만들어 설정
        list.setOnDataSelectionListener(new OnDataSelectionListener() {
            public void onDataSelected(AdapterView parent, View v, int position, long id) {
                IconTextItem curItem = (IconTextItem) adapter.getItem(position);
                String[] curData = curItem.getData();
 
                Toast.makeText(getApplicationContext(), "Selected : " + curData[0], 2000).show();
            }
        });
 
 
        // 화면을 리스트뷰 객체로 채움
        setContentView(list, params);
    }
 
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
 

 

  이것은 메인함수로 어댑터와 리스트뷰를 상속한 클래스를 불러와서 데이터 하나씩 집어넣고 어댑터 연결하고 끝 


List of Articles
번호 제목 날짜 조회 수
77 안드로이드 스튜디오 actionbar(액션바) 사라짐 file 2020.12.14 606
76 패키지명을 한꺼번에 변경하기 (Refactor) file 2020.12.14 295
75 하이브리드앱 기본 - WebView로 웹페이지 띄우기 file 2020.12.14 1025
74 ListView 리스트뷰 연습3 - 커스텀 리스트뷰 (Custom ListView) file 2020.12.14 906
73 초기화면 페이지를 만들어보자. splash 페이지 제작 file 2020.12.14 285
72 안드로이드 앱배포하기 apk 만들기 file 2020.12.14 326
71 안드로이드 입문 연습문제 3문항 - CheckBox, RadioButton, EditText, Spinner, 이벤트연습 file 2020.12.14 480
70 Apk manager 이용해 Decompile (디컴파일) 하기 file 2021.03.16 1622
69 안드로이드 arrayList 를 Json으로 변환 / jsonarry file 2021.03.29 326
68 안드로이드 unescape /escape [StringEscapeUtils로 해결] file 2021.03.29 344
67 안드로이드 스튜디오 - 싱글톤 패턴 (SingleTon Pattenr) 클래스 자동 생성 file 2021.03.29 614
66 안드로이드 스튜디오 - getter/setter 메소드 자동생성 file 2021.03.29 583
65 안드로이드 스튜디오 - 필수 재정의 함수 자동 코드 추가 file 2021.03.29 194
64 안드로이드 스튜디오 - 코드 자동 들여쓰기 file 2021.03.29 420
63 안드로이드 - 리니어 레이아웃 (Linear Layout) file 2021.03.29 319
62 안드로이드 - 랠러티브 레이아웃(Relative Layout) file 2021.03.29 239
61 안드로이드 - 프레임레이아웃 (FrameLayout) file 2021.03.29 517
60 안드로이드 - 에디트텍스트(EditText) 사용법 정리 file 2021.03.29 2755
59 버튼 생성, 이벤트 처리 file 2021.03.31 236
58 Java에서 XML 불러와서 동적 변화 주기 file 2021.03.31 288
Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved