http://stackoverflow.com/questions/4549746/custom-row-in-a-listpreference
안드로이드에서 기본적으로 제공하는 Listpreference는 textView 하나의 RadionButton이 들어가 있는 형태이다.
만약 이 안에 들어가는 List 한 줄에 ImageView를 넣고 싶을 경우, 다음과 같이 하면 된다.
CustomListPreference.java
import java.util.ArrayList;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
public class CustomListPreference extends ListPreference
{
public final static String PREPERENCE_KEY_LIST = "preList";
private CustomListPreferenceAdapter customListPreferenceAdapter = null;
private Context mContext;
private LayoutInflater mInflater;
private CharSequence[] entries;
private CharSequence[] entryValues;
private final int[] entryImgId = {
R.raw.testimg1,
R.raw.testimg2,
R.raw.testimg3,
};
private ArrayList<RadioButton> rButtonList;
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
public CustomListPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
mContext = context;
mInflater = LayoutInflater.from(context);
rButtonList = new ArrayList<RadioButton>();
prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
editor = prefs.edit();
}
@Override
protected void onPrepareDialogBuilder(Builder builder)
{
entries = getEntries();
entryValues = getEntryValues();
if (entries == null || entryValues == null || entries.length != entryValues.length )
{
throw new IllegalStateException(
"ListPreference requires an entries array and an entryValues array which are both the same length");
}
customListPreferenceAdapter = new CustomListPreferenceAdapter(mContext);
builder.setAdapter(customListPreferenceAdapter, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
});
}
private class CustomListPreferenceAdapter extends BaseAdapter
{
public CustomListPreferenceAdapter(Context context)
{
}
public int getCount()
{
return entries.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
CustomHolder holder = null;
if(row == null)
{
row = mInflater.inflate(R.layout.list_preference_row, parent, false);
holder = new CustomHolder(row, position);
row.setTag(holder);
// do whatever you need here, for me I wanted the last item to be greyed out and unclickable
row.setClickable(true);
row.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
for(RadioButton rb : rButtonList)
{
if(rb.getId() != position)
rb.setChecked(false);
else
rb.setChecked(true);
}
int index = position;
int value = Integer.valueOf((String) entryValues[index]);
editor.putString(PREPERENCE_KEY_LIST , value+"");
editor.commit();
Dialog mDialog = getDialog();
mDialog.dismiss();
}
});
}
return row;
}
class CustomHolder
{
private ImageView img = null;
private TextView text = null;
private RadioButton rButton = null;
CustomHolder(View row, final int position)
{
img = (ImageView)row.findViewById(R.id.list_view_row_img);
img.setImageResource(entryImgId[position]);
text = (TextView)row.findViewById(R.id.list_view_row_text);
text.setText(entries[position]);
rButton = (RadioButton)row.findViewById(R.id.list_view_row_radio_btn);
rButton.setId(position);
int selectedPos = Integer.parseInt(prefs.getString(PREPERENCE_KEY_LIST , "0"));
if(position == selectedPos )
rButton.setChecked(true);
// also need to do something to check your preference and set the right button as checked
rButtonList.add(rButton);
rButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
for(RadioButton rb : rButtonList)
{
if(rb != buttonView)
rb.setChecked(false);
}
int index = buttonView.getId();
int value = Integer.valueOf((String) entryValues[index]);
editor.putString(Const.PREPERENCE_KEY_SHAPE, value+"");
editor.commit();
Dialog mDialog = getDialog();
mDialog.dismiss();
}
}
});
}
}
}
}
list_preference_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="8dip"
android:paddingTop="8dip"
android:paddingLeft="10dip"
android:paddingRight="10dip">
<TableLayout
android:id="@+id/list_view_row_table_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0">
<TableRow
android:id="@+id/list_view_row_table_row"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/list_view_row_img"
android:gravity="center_vertical"
android:layout_width="40dip"
android:layout_height="40dip" />
<TextView
android:id="@+id/list_view_row_text"
android:textSize="22sp"
android:textColor="#000000"
android:gravity="center_vertical"
android:layout_width="160dip"
android:layout_height="40dip" />
<RadioButton
android:checked="false"
android:id="@+id/list_view_row_radio_btn"/>
</TableRow>
</TableLayout>
</LinearLayout>
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motiveflux="http://motiveflux.com"
android:key="preference_screen" >
<PreferenceCategory android:title="Image Custom">
<com.package.CustomeListPreference
android:key="preList"
android:title="Select"
android:dialogTitle="Img Select"
android:summary="Select Img"
android:defaultValue="0"
android:entries="@array/list_preference_entries"
android:entryValues="@array/list_preference_entryvalues"/>
</PreferenceCategory>
</PreferenceScreen>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="list_preference_entries">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
</string-array>
<string-array name="list_preference_entryvalues">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources>
그럼 오늘도 즐코딩^^
'나의 플랫폼 > 안드로이드' 카테고리의 다른 글
[ Android ] Receiver not registered (0) | 2011.12.19 |
---|---|
[ Android Opengl es 2.0 ] 여러 Texture를 사용할 때 (3) | 2011.12.13 |
[ Android ] 전원 버튼 여부 확인 (0) | 2011.12.09 |
[ Android ] Keyguard 상태 확인 (0) | 2011.12.09 |
[ Android Opengl es 2.0 ] 나머지 구하기!! (2) | 2011.11.23 |