課程
/移動開發(fā)
/Android
/Android必學(xué)-BaseAdapter的使用與優(yōu)化
BaseAdapter怎么添加點擊事件 請詳細(xì)些 在那個里面添加點擊事件呢
2016-08-16
源自:Android必學(xué)-BaseAdapter的使用與優(yōu)化 5-2
正在回答
兄弟所見略同
一般是通過回調(diào)函數(shù)
static final class MyAdapter extends BaseAdapter { ? ?@Override ? ?public View getView(final int position, View convertView, ViewGroup parent) { ? ? ? ?ViewHolder holder; ? ? ? ?if (convertView == null) { ? ? ? ? ? ?// inflate the view for row from xml file ? ? ? ? ? ?// keep a reference to each widget on the row. ? ? ? ? ? ?// here I only care about the button ? ? ? ? ? ?holder = new ViewHolder(); ? ? ? ? ? ?holder.mButton = (Button)convertView.findViewById(R.id.button); ? ? ? ? ? ?convertView.setTag(holder); ? ? ? ?} else { ? ? ? ? ? ?holder = (ViewHolder)convertView.getTag(); ? ? ? ?} ? ? ? ?// redefine the action for the button corresponding to the row ? ? ? ?holder.mButton.setOnClickListener(new OnClickListener() { ? ? ? ? ? ?@Override ? ? ? ? ? ?public void onClick(View v) { ? ? ? ? ? ? ? ?// do something depending on position ? ? ? ? ? ? ? ?performSomeAction(position); ? ? ? ? ? ? ? ?// mark data as changed ? ? ? ? ? ? ? ?MyAdapter.this.notifyDatasetChanged(); ? ? ? ? ? ?} ? ? ? ?} ? ?} ? ?static final class ViewHolder { ? ? ? ?// references to widgets ? ? ? ?Button mButton; ? ?} }
在內(nèi)部類中定義成員變量button,接著利用ViewHolder優(yōu)化代碼,創(chuàng)建與ConvertView之間的聯(lián)系,利用單擊事件setOnClickListener方法,就ok了
java王中王 提問者
舉報
了解數(shù)據(jù)適配器的使用方法,寫出高效、文藝的BaseAdapter
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2020-08-03
兄弟所見略同
2016-09-09
一般是通過回調(diào)函數(shù)
2016-08-17
static final class MyAdapter extends BaseAdapter {
? ?@Override
? ?public View getView(final int position, View convertView, ViewGroup parent) {
? ? ? ?ViewHolder holder;
? ? ? ?if (convertView == null) {
? ? ? ? ? ?// inflate the view for row from xml file
? ? ? ? ? ?// keep a reference to each widget on the row.
? ? ? ? ? ?// here I only care about the button
? ? ? ? ? ?holder = new ViewHolder();
? ? ? ? ? ?holder.mButton = (Button)convertView.findViewById(R.id.button);
? ? ? ? ? ?convertView.setTag(holder);
? ? ? ?} else {
? ? ? ? ? ?holder = (ViewHolder)convertView.getTag();
? ? ? ?}
? ? ? ?// redefine the action for the button corresponding to the row
? ? ? ?holder.mButton.setOnClickListener(new OnClickListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onClick(View v) {
? ? ? ? ? ? ? ?// do something depending on position
? ? ? ? ? ? ? ?performSomeAction(position);
? ? ? ? ? ? ? ?// mark data as changed
? ? ? ? ? ? ? ?MyAdapter.this.notifyDatasetChanged();
? ? ? ? ? ?}
? ? ? ?}
? ?}
? ?static final class ViewHolder {
? ? ? ?// references to widgets
? ? ? ?Button mButton;
? ?}
}
在內(nèi)部類中定義成員變量button,接著利用ViewHolder優(yōu)化代碼,創(chuàng)建與ConvertView之間的聯(lián)系,利用單擊事件setOnClickListener方法,就ok了