如何在ListView中點擊item控制item中的控件變化(ExpandableListView篇) -开发者知识库

如何在ListView中點擊item控制item中的控件變化(ExpandableListView篇) -开发者知识库,第1张

之前一篇講了在ListView中如何在點擊item時改變item中的控件狀態,並且也講了如何消除item布局復用帶來的負面效果。這一篇就講講ListView的進階版ExpandableListView。先上效果圖和代碼:

如何在ListView中點擊item控制item中的控件變化(ExpandableListView篇) -开发者知识库,第2张

MyActivity.java:

package com.test.listviewitemclicktest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ExpandableListView;
import android.widget.ListView;

public class MainActivity extends Activity {
//private ListView list;
//private MyAdapter adapter;
private MyExAdapter adapter2;
private ExpandableListView list2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//list = (ListView) findViewById(R.id.listView);
//adapter = new MyAdapter(this);
//list.setAdapter(adapter);
list2 = (ExpandableListView) findViewById(R.id.listView);
adapter2 = new MyExAdapter(this);
list2.setAdapter(adapter2);
for (int i = 0; i < adapter2.getGroupCount(); i ) {
list2.expandGroup(i);
}
}
}

MyExAdapter.java:

package com.test.listviewitemclicktest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MyExAdapter extends BaseExpandableListAdapter {
private Context context;
private List<ExBean> data;
// 存儲所有章中的所有ImageView
private HashMap<String, HashMap<String, ImageView>> chapterMap;
// 存儲一章中所有的ImageView
private HashMap<String, ImageView> sectionMap;
// 判斷是否點擊的標識
private int[][] tags = new int[50][50];

public MyExAdapter(Context context) {
this.context = context;
chapterMap = new HashMap<String, HashMap<String, ImageView>>();
sectionMap = new HashMap<String, ImageView>();
for (int i = 0; i < 50; i ) {
for (int j = 0; j < 50; j ) {
tags[i][j] = 0;
}
}
initData();
}

public void initData() {
data = new ArrayList<ExBean>();
for (int i = 0; i < 5; i ) {
ExBean bean = new ExBean();
bean.setCharpterName("第" i "章");
String[] str = new String[] { "第一節", "第二節", "第三節", "第四節" };
bean.setSectionNames(str);
data.add(bean);
}
}

@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return data.size();
}

@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return data.get(groupPosition).getSectionNames().length;
}

@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return data;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return data;
}

@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}

@Override
// 章名
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.listview_chapter_item, parent, false);
holder = new ViewHolder();
holder.chaptername = (TextView) convertView.findViewById(R.id.tv_chaptername);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.chaptername.setText(data.get(groupPosition).getCharpterName());
return convertView;
}

@Override
// 節名
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.listview_item, parent, false);
holder = new ViewHolder();
holder.sectionname = (Button) convertView.findViewById(R.id.bt_item);
holder.sectionVideo = (ImageView) convertView.findViewById(R.id.iv_item);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(!chapterMap.containsKey(groupPosition "")){
chapterMap.put(groupPosition "", new HashMap<String, ImageView>());
}
chapterMap.get(groupPosition "").put(childPosition "", holder.sectionVideo);
Log.i("info", "childP>" childPosition "groupP>" groupPosition "sectionMap>" sectionMap.size());
// 判斷圖標亮暗
if (tags[groupPosition][childPosition] == 1) {
chapterMap.get(groupPosition "").get(childPosition "").setBackgroundResource(R.drawable.video);
} else {
chapterMap.get(groupPosition "").get(childPosition "").setBackgroundResource(R.drawable.playvideo);
}
holder.sectionname.setText(data.get(groupPosition).getSectionNames()[childPosition]);
// 點擊事件,控制圖標亮暗
holder.sectionname.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
resetImageView();
chapterMap.get(groupPosition "").get(childPosition "").setBackgroundResource(R.drawable.video);
tags[groupPosition][childPosition] = 1;
}

public void resetImageView() {
for (int i = 0; i < chapterMap.size(); i ) {
for (int j = 0; j < chapterMap.get(i "").size(); j ) {
chapterMap.get(i "").get(j "").setBackgroundResource(R.drawable.playvideo);
tags[i][j] = 0;
}
}
}
});
return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}

public class ViewHolder {
TextView chaptername;
Button sectionname;
ImageView sectionVideo;
}

}


ExBean.java:

package com.test.listviewitemclicktest;

import java.util.List;

public class ExBean {
private String charpterName;
private String[] sectionNames;

public String getCharpterName() {
return charpterName;
}

public void setCharpterName(String charpterName) {
this.charpterName = charpterName;
}

public String[] getSectionNames() {
return sectionNames;
}

public void setSectionNames(String[] sectionNames) {
this.sectionNames = sectionNames;
}

}

activity_main.xml:

<RelativeLayout 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"
tools:context=".MainActivity" >

<ExpandableListView
android:id="@ id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="@null"
/>

</RelativeLayout>

剩下的兩個item的xml文件就不放了。ExpandableListView和ListView里面最大的區別就是使用了一個嵌套的Map來存儲數據,並且要判斷某一章是否剛開始繪制,如果剛開始繪制,則在chapterMap中增加相應字段並添加一個sectionMap。



最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复