編輯:關於Android編程
前幾篇文章介紹了Listview,但在實際開發中也經常會用到多層的Listview來展示數據,比如qq中的好友展示,所以這張來了解一下ExpandableListview,基本思想與Listview大致是相同的,所以用起來會比較方便。
實現效果圖:
程序代碼:
布局文件:
activity_main.xml:
<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PHByZSBjbGFzcz0="brush:java;">
group和child共同使用的布局(當然也可以使用不同的布局來實現),這的布局比較簡單,只有一個Textview:
list_item:
package com.listviewdemo_expandablelistview; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ExpandableListView; public class MainActivity extends Activity { private ExpandableListView listView; private Listgroup; private List > child; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ExpandableListView) findViewById(R.id.expandableListView); /** * 初始化數據 */ initData(); adapter = new MyAdapter(this,group,child); listView.setAdapter(adapter); } private void initData() { group = new ArrayList
(); child = new ArrayList >(); addInfo("北京",new String[]{"朝陽","海澱","東城區","西城區"}); addInfo("河北", new String[]{"邯鄲","石家莊","邢台"}); addInfo("廣東", new String[]{"廣州","深圳","珠海"}); } /** * 添加數據信息 * @param g * @param c */ private void addInfo(String g,String[] c) { group.add(g); List
list = new ArrayList (); for (int i = 0; i < c.length; i++) { list.add(c[i]); } child.add(list); } }
package com.listviewdemo_expandablelistview; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; /** * expandableListView適配器 * */ public class MyAdapter extends BaseExpandableListAdapter { private Context context; private Listgroup; private List > child; public MyAdapter(Context context, List
group, List > child) { this.context = context; this.group = group; this.child = child; } @Override public int getGroupCount() { return group.size(); } @Override public int getChildrenCount(int groupPosition) { return child.size(); } @Override public Object getGroup(int groupPosition) { return group.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return child.get(childPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } /** * 顯示:group */ @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(context).inflate( R.layout.list_item, null); holder = new ViewHolder(); holder.textView = (TextView) convertView .findViewById(R.id.textView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.textView.setText(group.get(groupPosition)); holder.textView.setTextSize(25); holder.textView.setPadding(36, 10, 0, 10); return convertView; } /** * 顯示:child */ @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(context).inflate( R.layout.list_item, null); holder = new ViewHolder(); holder.textView = (TextView) convertView .findViewById(R.id.textView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.textView.setText(child.get(groupPosition).get(childPosition)); holder.textView.setTextSize(20); holder.textView.setPadding(72, 10, 0, 10); return convertView; } class ViewHolder { TextView textView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } }
一、前言又是過了好長時間,沒寫文章的雙手都有點難受了。今天是聖誕節,還是得上班。因為前幾天有一個之前的同事,在申請微信SDK的時候,遇到簽名的問題,問了我一下,結果把我難
對於側滑刪除已經是見慣不慣的了,我也一直有寫類似QQ那樣的側滑刪除控件的想法,雖然研究一段時間的自定義View,然對自定義ViewGroup實戰還是較少,並且側滑刪除還要
之前參加了一個課程,裡面有一節講到了用視頻作為啟動界面。講師用的是自定義VideoView,重寫onMeasure方法,因為原生的VideoView在那情況下不能實現全屏
本篇文章講的是Android 自定義ViewGroup之實現標簽流式布局-FlowLayout,開發中我們會經常需要實現類似於熱門標簽等自動換行的流式布局的功能,網上也有