밥벌이1 - S/W
[안드로이드] Iconified TextList - The making of
행복가득
2007. 12. 17. 17:04
Iconified TextList - The making of
여기서 배울수 있는 것 : 어떻게 자신만의 ListAdpater를 만들어 낼 수 있는가? 샘플 : IconifiedListAdapter
난이도 : 1.5 of 5
코드 실행시 볼 수 있는 화면 :
full 소스:
Icons (res/drawable/*.png) used in Demo App
'src/your_package_structure/TestLayout.java'
'src/your_package_structure/IconifiedText.java'
'src/your_package_structure/IconifiedTextView.java'
'src/your_package_structure/IconifiedTextListAdapter.java'
_________________
| Android Development Community / Tutorials
여기서 배울수 있는 것 : 어떻게 자신만의 ListAdpater를 만들어 낼 수 있는가? 샘플 : IconifiedListAdapter
난이도 : 1.5 of 5

코드 실행시 볼 수 있는 화면 :

설명: 위의 애플리케이션은 아래의 간단한 코드로 생성된다.
Java: |
IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this); // Add four items itla.addItem(new IconifiedText( "Surf Web", getResources().getDrawable(R.drawable.favicon))); itla.addItem(new IconifiedText( "Report Bug", getResources().getDrawable(R.drawable.bug))); itla.addItem(new IconifiedText( "Speak Spanish", getResources().getDrawable(R.drawable.locale))); itla.addItem(new IconifiedText( "Vidoop", getResources().getDrawable(R.drawable.vidoop))); // Display it setListAdapter(itla); |
우리는 간단하게 IconifiedTextListAdapter를 생성하고 여기에 String과 Drawable의 조합으로 이루어진 것을 화면에 나타내본다. 이것을 위해 아래의 몇가지 작업을 이해하면 된다.
1. 가장 기본부터 살펴보자면, IconifiedText.java 는 굉장히 작은 클래스다. 이것은 생성자만을 포함하고 있으며, 몇가지 getter와 setter 만으로 이루어져 있다. 그것은 String과 Drawable(Icon)을 결합시키기만 한다.
Java: |
public class IconifiedText{ private String mText = ""; private Drawable mIcon; private boolean mSelectable = true; public IconifiedText(String text, Drawable bullet) { mIcon = bullet; mText = text; } // ... } |
2. 이제 우리는 50라인 정도로 이루어진 IconifiedTextListAdapter.java 파일을 살펴보자. 이것은 기본적으로 IconifiedText들을 저장한다.
Java: |
/** @param convertView The old view to overwrite, if one is passed * @returns a IconifiedTextView that holds wraps around an IconifiedText */ public View getView(int position, View convertView, ViewGroup parent) { IconifiedTextView btv; if (convertView == null) { btv = new IconifiedTextView(mContext, mItems.get(position)); } else { // Reuse/Overwrite the View passed // We are assuming(!) that it is castable! btv = (IconifiedTextView) convertView; btv.setText(mItems.get(position).getText()); btv.setIcon(mItems.get(position).getIcon()); } return btv; } |
3.IconifiedTextView 역시 이해하기에 어렵지 않다..
Java: |
public IconifiedTextView(Context context, IconifiedText aIconifiedText) { super(context); /* First Icon and the Text to the right (horizontal), * not above and below (vertical) */ this.setOrientation(HORIZONTAL); mIcon = new ImageView(context); mIcon.setImageDrawable(aIconifiedText.getIcon()); // left, top, right, bottom mIcon.setPadding(0, 2, 5, 0); // 2px up, 5px to the right /* At first, add the Icon to ourself * (! we are extending LinearLayout) */ addView(mIcon, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mText = new TextView(context); mText.setText(aIconifiedText.getText()); /* Now the text (after the icon) */ addView(mText, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } |
full 소스:



'src/your_package_structure/TestLayout.java'
Java: |
/* $Id: TestLayout.java 57 2007-11-21 18:31:52Z steven $ * * Copyright 2007 Steven Osborn * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.anddev.android.testproject; import android.app.ListActivity; import android.os.Bundle; public class TestLayout extends ListActivity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this); // Add four items itla.addItem(new IconifiedText( "Surf Web", getResources().getDrawable(R.drawable.favicon))); itla.addItem(new IconifiedText( "Report Bug", getResources().getDrawable(R.drawable.bug))); itla.addItem(new IconifiedText( "Speak Spanish", getResources().getDrawable(R.drawable.locale))); itla.addItem(new IconifiedText( "Vidoop", getResources().getDrawable(R.drawable.vidoop))); // Display it setListAdapter(itla); } } |
'src/your_package_structure/IconifiedText.java'
Java: |
/* * Copyright 2007 Steven Osborn * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.anddev.android.testproject; import android.graphics.drawable.Drawable; /** @author Steven Osborn - http://steven.bitsetters.com */ public class IconifiedText{ private String mText = ""; private Drawable mIcon; private boolean mSelectable = true; public IconifiedText(String text, Drawable bullet) { mIcon = bullet; mText = text; } public boolean isSelectable() { return mSelectable; } public void setSelectable(boolean selectable) { mSelectable = selectable; } public String getText() { return mText; } public void setText(String text) { mText = text; } public void setIcon(Drawable icon) { mIcon = icon; } public Drawable getIcon() { return mIcon; } /** Make IconifiedText comparable by its name */ @Override public int compareTo(IconifiedText other) { if(this.mText != null) return this.mText.compareTo(other.getText()); else throw new IllegalArgumentException(); } } |
'src/your_package_structure/IconifiedTextView.java'
Java: |
/* $Id: BulletedTextView.java 57 2007-11-21 18:31:52Z steven $ * * Copyright 2007 Steven Osborn * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.anddev.android.testproject; import android.content.Context; import android.graphics.drawable.Drawable; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class IconifiedTextView extends LinearLayout { private TextView mText; private ImageView mIcon; public IconifiedTextView(Context context, IconifiedText aIconifiedText) { super(context); /* First Icon and the Text to the right (horizontal), * not above and below (vertical) */ this.setOrientation(HORIZONTAL); mIcon = new ImageView(context); mIcon.setImageDrawable(aIconifiedText.getIcon()); // left, top, right, bottom mIcon.setPadding(0, 2, 5, 0); // 5px to the right /* At first, add the Icon to ourself * (! we are extending LinearLayout) */ addView(mIcon, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mText = new TextView(context); mText.setText(aIconifiedText.getText()); /* Now the text (after the icon) */ addView(mText, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } public void setText(String words) { mText.setText(words); } public void setIcon(Drawable bullet) { mIcon.setImageDrawable(bullet); } } |
'src/your_package_structure/IconifiedTextListAdapter.java'
Java: |
/* $Id: BulletedTextListAdapter.java 57 2007-11-21 18:31:52Z steven $ * * Copyright 2007 Steven Osborn * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.anddev.android.testproject; 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 Steven Osborn - http://steven.bitsetters.com */ public class IconifiedTextListAdapter extends BaseAdapter { /** Remember our context so we can use it when constructing views. */ private Context mContext; private List<IconifiedText> mItems = new ArrayList<IconifiedText>(); public IconifiedTextListAdapter(Context context) { mContext = context; } public void addItem(IconifiedText it) { mItems.add(it); } public void setListItems(List<IconifiedText> lit) { mItems = lit; } /** @return The number of items in the */ 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 aioobe){ return super.isSelectable(position); } } /** Use the array index as a unique id. */ public long getItemId(int position) { return position; } /** @param convertView The old view to overwrite, if one is passed * @returns a IconifiedTextView that holds wraps around an IconifiedText */ public View getView(int position, View convertView, ViewGroup parent) { IconifiedTextView btv; if (convertView == null) { btv = new IconifiedTextView(mContext, mItems.get(position)); } else { // Reuse/Overwrite the View passed // We are assuming(!) that it is castable! btv = (IconifiedTextView) convertView; btv.setText(mItems.get(position).getText()); btv.setIcon(mItems.get(position).getIcon()); } return btv; } } |
Regards,
plusminus
_________________

=============================================================================================================
그리 어렵지 않게 실행시켜볼 수 있는 코드다.