android的ListView點(diǎn)擊item使item展開的做法的實(shí)現(xiàn)代碼
本文介紹了android的ListView點(diǎn)擊item使item展開的做法的實(shí)現(xiàn)代碼,分享給大家,具體如下:
效果圖:
原理是點(diǎn)擊item的時(shí)候,重新measure list的各個(gè)item的高度
list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { MyData data = myList.get(position); if (oldPostion == position) { if (data.expand) { oldPostion = -1; } data.expand = !data.expand; }else{ oldPostion = position; data.expand = true; } int totalHeight = 0; for(int i=0;i<myAdapter.getCount();i++) { View viewItem = myAdapter.getView(i, null, list); viewItem.measure(0, 0); totalHeight += viewItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = list.getLayoutParams(); params.height = totalHeight + (list.getDividerHeight() * (list.getCount() - 1)); list.setLayoutParams(params); myAdapter.notifyDataSetChanged(); } });
adapter:
private class myAdapter extends BaseAdapter{ @Override public int getCount() { return myList.size(); } @Override public Object getItem(int position) { return myList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { MyTag tag = new MyTag(); MyData data = myList.get(position); if(convertView == null) { convertView = inflater.inflate(R.layout.expand_item_layout, null); tag.item1 = (TextView)convertView.findViewById(R.id.item1); tag.item2 = (RelativeLayout)convertView.findViewById(R.id.item2); convertView.setTag(tag); }else{ tag = (MyTag)convertView.getTag(); } if(data.expand) { tag.item2.setVisibility(View.VISIBLE); }else{ tag.item2.setVisibility(View.GONE); } tag.item1.setText(data.name); return convertView; } } private class MyTag{ private TextView item1; private RelativeLayout item2; } private class MyData{ boolean expand; String name; }
代碼在https://github.com/nickgao1986/StepSport
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android編程實(shí)現(xiàn)Listview點(diǎn)擊展開和隱藏的方法
- Android編程實(shí)現(xiàn)ListView中item部分區(qū)域添加點(diǎn)擊事件功能
- Android開發(fā)實(shí)現(xiàn)ListView點(diǎn)擊item改變顏色功能示例
- Android 實(shí)現(xiàn)ListView的點(diǎn)擊變色的實(shí)例
- Android ListView的Item點(diǎn)擊效果的定制
- Android實(shí)現(xiàn)為L(zhǎng)istView同時(shí)設(shè)置點(diǎn)擊時(shí)的背景和點(diǎn)擊松手之后的背景
- Android ListView的item背景色設(shè)置和item點(diǎn)擊無(wú)響應(yīng)的解決方法
- Android開發(fā)之ListView實(shí)現(xiàn)Item局部刷新
- android ListView內(nèi)數(shù)據(jù)的動(dòng)態(tài)添加與刪除實(shí)例代碼
- android ListView和GridView拖拽移位實(shí)現(xiàn)代碼
- Android開發(fā)實(shí)現(xiàn)ListView點(diǎn)擊展開收起效果示例
相關(guān)文章
學(xué)習(xí)使用Material Design控件(三)使用CardView實(shí)現(xiàn)卡片效果
這篇文章主要為大家介紹了學(xué)習(xí)使用Material Design控件的詳細(xì)教程,如何使用CardView實(shí)現(xiàn)卡片效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android GridView 滑動(dòng)條設(shè)置一直顯示狀態(tài)(推薦)
這篇文章主要介紹了Android GridView 滑動(dòng)條設(shè)置一直顯示狀態(tài)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12Android仿QQ消息提示實(shí)現(xiàn)彈出式對(duì)話框
這篇文章主要為大家詳細(xì)介紹了Android仿QQ消息提示實(shí)現(xiàn)彈出式對(duì)話框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android如何實(shí)現(xiàn)時(shí)間線效果
這篇文章主要介紹了?Android如何實(shí)現(xiàn)時(shí)間線效果,下面文章圍繞?Android如何實(shí)現(xiàn)時(shí)間線效果的相關(guān)資料展開詳細(xì)內(nèi)容,具有一定的參考價(jià)值?,需要的朋友可以參考一下2021-11-11Android實(shí)現(xiàn)獲取驗(yàn)證碼倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)獲取驗(yàn)證碼倒計(jì)時(shí)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12