Android開發(fā)實現ListView點擊展開收起效果示例
本文實例講述了Android開發(fā)實現ListView點擊展開收起效果。分享給大家供大家參考,具體如下:
廢話不說先上效果:

實際上這是采用一個ExpandableListView實現的
布局文件很簡單:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<!--提示框-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="請選擇您的類型:"
android:textSize="30sp"
android:textColor="#ffffffff"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#ff000000"/>
<!--定義一個ExpandableListView組件-->
<ExpandableListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
</LinearLayout>
然后就是具體實現:
這里主要是添加幾個必須的屬性 大多數方法不用重寫
參考我代碼中的位置稍加改動就行
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//創(chuàng)建一個ExpandableListAdapter對象
final ExpandableListAdapter adapter = new ExpandableListAdapter() {
int[] logos = new int[]{
R.drawable.human1st,
R.drawable.human1st,
R.drawable.human2nd,
R.drawable.human3rd
};
private String[] humanTypes = new String[]{
"不是人","聰明人","普通人","我這樣的人"
};
private String[][] humans = new String[][]{
{"上仙","大神","荷蘭豬"},
{"超人","一般聰明人","假的聰明人"},
{"努力的人","快樂的普通人","苦逼的普通人"},
{"天才","傻逼","蠢萌"}
};
//獲得制定組的位置、指定子列表項處的字列表項數據
private TextView getTextView(){
AbsListView.LayoutParams layoutParams =
new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,164);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(layoutParams);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36,0,0,0);
textView.setTextSize(30);
return textView;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public int getGroupCount() {
return humanTypes.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return humans[groupPosition].length;
}
//獲取制定組位置處的組數據
@Override
public Object getGroup(int groupPosition) {
return humanTypes[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return humans[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
//該方法決定每個組選項的外觀
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LinearLayout linearLayout = new LinearLayout(MainActivity.this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView logo = new ImageView(MainActivity.this);
logo.setImageResource(logos[groupPosition]);
linearLayout.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
linearLayout.addView(textView);
// linearLayout.setMinimumHeight(50);
return linearLayout;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPosition,childPosition).toString());
return textView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public void onGroupExpanded(int groupPosition) {
}
@Override
public void onGroupCollapsed(int groupPosition) {
}
@Override
public long getCombinedChildId(long groupId, long childId) {
return 0;
}
@Override
public long getCombinedGroupId(long groupId) {
return 0;
}
};
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this,"你是一個:" +
adapter.getChild(groupPosition,childPosition),Toast.LENGTH_SHORT).show();
return true;
}
});
expandableListView.setAdapter(adapter);
}
}
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android數據庫操作技巧總結》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android實現橫向無限循環(huán)滾動的單行彈幕效果
這篇文章主要為大家詳細介紹了Android實現橫向無限循環(huán)滾動的單行彈幕效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
Android開發(fā)使用Messenger及Handler進行通信的方法示例
這篇文章主要介紹了Android開發(fā)使用Messenger及Handler進行通信的方法,結合實例形式分析了Android使用Messenger及Handler定義客戶端與服務器端實現通信的相關操作技巧,需要的朋友可以參考下2017-12-12
Android上實現easyconfig(airkiss)方法
本篇文章主要給大家講解了在Android系統(tǒng)上實現easyconfig(airkiss)的方法,有這方面需要的朋友參考學習下吧。2018-01-01

