2010年10月26日火曜日

Android ListView で区切り行をつくる

Q. ListView の 区切り行 * はどうやってつくるんですか?

A. isEnabled を Override して、 getView で 区切り行のときと、そうでないときで返す View を変えます

* 区切り行とは、Android Market アプリの 緑の区切りのようなやつのことです。ListView の1行ですが、タップしても反応しません。







public class ExtendsArrayAdapterSample2 extends ListActivity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

List<bindData> data = new ArrayList<bindData>();
data.add(new bindData("Clock", R.drawable.alermclock));
data.add(new bindData("-", 0));
data.add(new bindData("Camera", R.drawable.camera));
data.add(new bindData("-", 0));
data.add(new bindData("Book", R.drawable.bookopen));
data.add(new bindData("-", 0));
data.add(new bindData("Calculator", R.drawable.calculator));
data.add(new bindData("-", 0));

MyAdapter adapter = new MyAdapter(this, R.layout.list_item5, data);
setListAdapter(adapter);
}

class bindData {
String text;
int imageResourceId;

public bindData(String text, int imageResourceId) {
this.text = text;
this.imageResourceId = imageResourceId;
}
}

static class ViewHolder {
TextView textView;
ImageView imageView;
}

public class MyAdapter extends ArrayAdapter<bindData> {
private LayoutInflater inflater;
private int layoutId;

public MyAdapter(Context context, int layoutId, List<bindData> objects) {
super(context, 0, objects);
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.layoutId = layoutId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {
convertView = inflater.inflate(layoutId, parent, false);
holder = new ViewHolder();
holder.textView = (TextView) convertView
.findViewById(R.id.textview);
holder.imageView = (ImageView) convertView
.findViewById(R.id.imageview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

if(!isEnabled(position))
convertView.setBackgroundColor(Color.BLACK);
else {
bindData data = getItem(position);
holder.textView.setText(data.text);
holder.imageView.setImageResource(data.imageResourceId);
}

return convertView;
}

@Override
public boolean isEnabled(int position) {
return !(getItem(position).text.startsWith("-"));
}
}
}





 

1 件のコメント:

  1. QUANTUM BINARY SIGNALS

    Get professional trading signals sent to your mobile phone every day.

    Start following our trades right now & make up to 270% per day.

    返信削除