新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > android ListView中給Item的每個(gè)控件設(shè)置單擊事件

        android ListView中給Item的每個(gè)控件設(shè)置單擊事件

        作者: 時(shí)間:2016-10-08 來源:網(wǎng)絡(luò) 收藏

        最近做了一個(gè)android項(xiàng)目,在每一個(gè)ListView的Item中還有兩個(gè)RelativeLayout控件,需要對(duì)這兩個(gè)RelativeLayout控件設(shè)置單擊事件,解決如下:

        本文引用地址:http://www.104case.com/article/201610/305649.htm

        只貼關(guān)鍵代碼了

        //ListView變量

        private ListView lv_pcorder = null;

        //需要傳遞的變量

        List list1 = new ArrayList();

        List list2 = new ArrayList();

        Map pics = Util.getAnecdoteSeven();

        Map picid = new HashMap();

        //配置BasicAdapter

        lv_pcorder = (ListView) findViewById(R.id.lv_pcorder);

        lv_pcorder.setAdapter(new PicCenterListApdapter(PicCenterList.this, list1, list2, pics, picid));

        //BaseAdapter實(shí)現(xiàn)類

        public class PicCenterListApdapter extends BaseAdapter {

        private Context context;

        private List list1;

        private List list2;

        private Map map;

        private Map picid;

        public PicCenterListApdapter(Context context, List list1, List list2, Map map, Map picid) {

        this.context = context;

        this.list1 = list1;

        this.list2 = list2;

        this.map = map;

        this.picid = picid;

        }

        @Override

        public int getCount() {

        return list1.size();

        }

        @Override

        public Object getItem(int arg0) {

        return null;

        }

        @Override

        public long getItemId(int position) {

        return 0;

        }

        @Override

        public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder vh = null;

        if (convertView == null) {

        vh = new ViewHolder();

        LayoutInflater inflater = LayoutInflater.from(context);

        convertView = inflater.inflate(R.layout.pcorderlist, null);

        vh.lin0 = (RelativeLayout) convertView.findViewById(R.id.lin0);

        vh.lin0.setOnClickListener(new PCount(position)); //設(shè)置單擊事件

        vh.lin2 = (RelativeLayout) convertView.findViewById(R.id.lin2); //設(shè)置單擊事件

        vh.lin2.setOnClickListener(new COrder(position)); //設(shè)置單擊事件

        vh.lin3 = (RelativeLayout) convertView.findViewById(R.id.lin3);

        vh.lin3.setOnClickListener(new COrder(position));

        vh.timeitem = (TextView) convertView.findViewById(R.id.timeitem);

        vh.timeitem.setText(list1.get(position));

        vh.countitem = (TextView) convertView.findViewById(R.id.countitem);

        vh.countitem.setText(list2.get(position));

        if (map.containsKey(list1.get(position))) {

        vh.smallpic = (ImageView) convertView.findViewById(R.id.smallpic);

        vh.smallpic.setImageResource(picid.get(map.get(list1.get(position)).getAddress()));

        vh.title = (TextView) convertView.findViewById(R.id.title);

        vh.title.setText(map.get(list1.get(position)).getTitle());

        vh.picintro = (TextView) convertView.findViewById(R.id.picintro);

        vh.picintro.setText( +Util.getString(map.get(list1.get(position)).getContent())+....);

        }

        convertView.setTag(vh);

        } else {

        vh = (ViewHolder) convertView.getTag();

        }

        return convertView;

        }

        //單擊事件實(shí)現(xiàn)

        class COrder implements OnClickListener {

        public int position;

        public COrder(int p) {

        position = p;

        }

        @Override

        public void onClick(View v) {

        Intent intent = new Intent();

        intent.setClass(context, CountOrder.class);

        Bundle b = new Bundle();

        b.putString(datatime, list1.get(position));

        intent.putExtras(b);

        context.startActivity(intent);

        }

        }

        //單擊事件實(shí)現(xiàn)

        class PCount implements OnClickListener {

        public int position;

        public PCount(int p) {

        position = p;

        }

        public void onClick(View v) {

        Intent intent = new Intent();

        intent.setClass(context, PicCenterItem.class);

        Bundle b = new Bundle();

        b.putString(datatime, list1.get(position));

        intent.putExtras(b);

        context.startActivity(intent);

        }

        }

        public final class ViewHolder {

        public RelativeLayout lin0;

        public RelativeLayout lin2;

        public RelativeLayout lin3;

        public TextView timeitem;

        public TextView countitem;

        public ImageView smallpic;

        public TextView title;

        public TextView picintro;

        }

        }



        關(guān)鍵詞:

        評(píng)論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 依安县| 玛多县| 潮州市| 临朐县| 岳西县| 都昌县| 昌江| 玉山县| 华蓥市| 洪泽县| 道真| 伊金霍洛旗| 锡林郭勒盟| 波密县| 新绛县| 庐江县| 新余市| 邳州市| 留坝县| 武功县| 从化市| 桂平市| 九江市| 龙海市| 三都| 丹寨县| 新安县| 贵溪市| 图木舒克市| 北宁市| 若尔盖县| 宁化县| 仁布县| 漳浦县| 贵南县| 黑山县| 凤庆县| 迁安市| 灯塔市| 霍山县| 湘潭市|