編輯:關於Android編程
本文實例講述了Android中ExpandableListView的用法,ExpandableListView是android中可以實現下拉list的一個控件,具體的實現方法如下:
首先:在layout的xml文件中定義一個ExpandableListView
復制代碼 代碼如下:<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical"
>
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
定義兩個List,用來存放控件中Group/Child中的String
復制代碼 代碼如下:private List<String> groupArray;
private List<List<String>> childArray;
對這兩個List進行初始化,並插入一些數據
復制代碼 代碼如下:groupArray = new ArrayList<String>();
childArray = new ArrayList<List<String>>();
groupArray.add("第一行");
groupArray.add("第二行");
List<String> tempArray = new ArrayList<String>();
tempArray.add("第一條");
tempArray.add("第二條");
tempArray.add("第三條");
for(int index = 0; index <groupArray.size(); ++index)
{
childArray.add(tempArray);
}
定義ExpandableListView的Adapter
復制代碼 代碼如下://ExpandableListView的Adapter
public class ExpandableAdapter extends BaseExpandableListAdapter
{
Activity activity;
public ExpandableAdapter(Activity a)
{
activity = a;
}
public Object getChild(int groupPosition, int childPosition)
{
return childArray.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public int getChildrenCount(int groupPosition)
{
return childArray.get(groupPosition).size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
String string = childArray.get(groupPosition).get(childPosition);
return getGenericView(string);
}
// group method stub
public Object getGroup(int groupPosition)
{
return groupArray.get(groupPosition);
}
public int getGroupCount()
{
return groupArray.size();
}
public long getGroupId(int groupPosition)
{
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
String string = groupArray.get(groupPosition);
return getGenericView(string);
}
// View stub to create Group/Children 's View
public TextView getGenericView(String string)
{
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView text = new TextView(activity);
text.setLayoutParams(layoutParams);
// Center the text vertically
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
text.setPadding(36, 0, 0, 0);
text.setText(string);
return text;
}
public boolean hasStableIds()
{
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
最後,給定義好的ExpandableListView添加上Adapter
復制代碼 代碼如下:ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
expandableListView.setAdapter(new ExpandableAdapter(Main.this));
希望本文所述對大家的Android程序設計有所幫助。
之前對線程也寫過幾篇文章,不過倒是沒有針對android,因為java與android在線程方面大部分還是相同,不過本篇我們要介紹的是android的專屬類Handler
目前64bitandroid系統也慢慢的多了,看到也有apk聲稱支持64bitsystem,然後就往裡面打包搞了個arm64-v8a 目錄,放了個64bit的so,但是a
Android性能的優化主要分為兩點1、布局優化2、內存優化布局優化首先來看一下布局優化,系統在渲染UI的時候會消耗大量的資源,所以,對布局的優化就顯得尤為重要避免Ove
一、服務器端實現 (1)創建動態服務器項目 個部分代碼如下: package com.lc.dao; import java.sql.Connection; imp