티스토리 뷰
배열리소스(string-array)를 활용한 GridView
Dexx 2016. 3. 24. 12:36가변적이지 않은 목록을 여지껏 노가다(?) xml로 배치해 오다가 이제야 방법을 알아 냈다.
문득 GridLayout을 쓰면 어떨까 해서 확인해 보니...GridView를 활용한 방법이 딱!
[String 내 배열리소스]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <string-array name="area"> <item>서울</item> <item>경기</item> <item>인천</item> <item>대전</item> <item>세종</item> <item>충남</item> <item>충북</item> <item>광주</item> <item>전남</item> <item>전북</item> <item>대구</item> <item>경북</item> <item>부산</item> <item>울산</item> <item>경남</item> <item>강원</item> <item>제주</item> <item>해외</item> </string-array> | cs |
[activity_area.xml 원하는 위치에 GridView배치]
1 2 3 4 5 6 7 8 | <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="wrap_content" android:numColumns="4" android:verticalSpacing="2dp" android:horizontalSpacing="2dp" android:gravity="center"/> | cs |
[activity_area_layout_item.xml GridView내에 보여지 레이아웃 아이템(난 버튼으로 처리)]
1 2 3 4 5 6 7 8 9 | <Button style="@style/DefaultLightButton" android:id="@+id/btnItem" android:layout_width="match_parent" android:layout_height="56dp" android:background="@drawable/btn_default" android:textColor="#ffffffff" android:textSize="16sp" xmlns:android="http://schemas.android.com/apk/res/android" /> | cs |
[아답터 소스]
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 | public class AreaActivity extends AppCompatActivity { private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_area); GridView gridView = (GridView) findViewById(R.id.gridView); //String-array를 ArrayList로 변환 ArrayList<String> items = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.area))); gridView.setAdapter(new GridAdapter(items)); } private class GridAdapter extends BaseAdapter { ArrayList<String> mItems; int mCount; public GridAdapter(ArrayList<String> items) { mCount = items.size(); mItems = new ArrayList<String>(mCount); for (String item : items) { String[] parts = item.split(","); for (String part : parts) { part.replace(" ", ""); mItems.add(part); } } } @Override public int getCount() { return mCount; } @Override public Object getItem(int position) { return mItems.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_area_layout_item, parent, false); } Button button = (Button) view.findViewById(R.id.btnItem); button.setText(mItems.get(position)); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 링크 } }); return view; } } } | cs |
끄읕~
'Android 개발 메모' 카테고리의 다른 글
ImageView의 높이를 가변적인 높이에 맞추고 싶을 때 (0) | 2016.03.31 |
---|---|
Floating Labels 관련 잘 정리 내용 (0) | 2016.03.17 |
구글플레이에서 앱 버전 체크 (7) | 2016.03.16 |
댓글