android之expandableListView+BaseExpandableListAdapter+ArrayList伸缩的用法:

package com.example.adapter;


import java.util.ArrayList;


import android.content.Context;
import android.content.Intent;
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.TextView;


import com.example.entity.TVchannel;
import com.example.entity.TVstation;
import com.example.utils.Consts;
import com.example.view.R;
import com.example.view.TVProgramActivity;


public class TVChannelAdapter extends BaseExpandableListAdapter {
private ArrayList<Object> stations;
private ArrayList<ArrayList<Object>> tvchannels;
private LayoutInflater inflater;
       private Context context;

public TVChannelAdapter(Context context, ArrayList<Object> stations,
ArrayList<ArrayList<Object>> tvchannels) {
this.context = context;
this.inflater = LayoutInflater.from(context);
setData(stations, tvchannels);
}


/**
* 更新数据

* @param stations
* @param tvchannels
*/
public void notifyDataChanged(ArrayList<Object> stations,
ArrayList<ArrayList<Object>> tvchannels) {
setData(stations, tvchannels);
this.notifyDataSetChanged();
}


private void setData(ArrayList<Object> stations,
ArrayList<ArrayList<Object>> tvchannels) {
if (stations == null) {
Log.i("info", "====station = " + stations);
this.stations = new ArrayList<Object>();
} else {
this.stations = stations;
}
if (tvchannels == null) {
// Log.i("info", "====tvchannels = "+tvchannels);
this.tvchannels = new ArrayList<ArrayList<Object>>();
} else {
this.tvchannels = tvchannels;
}
}


@Override
public int getGroupCount() {


Log.i("info", "==grouup Size = " + stations.size());
return stations.size();
}


/**
* 获取展开的子列个数:
*/
@Override
public int getChildrenCount(int groupPosition) {
Log.i("info", "===child size1 = " + tvchannels.size());
Log.i("info", "===child size2 = "
+ tvchannels.get(groupPosition).size());
return tvchannels.get(groupPosition).size();
}


@Override
public Object getGroup(int groupPosition) {
return stations.get(groupPosition);
}


@Override
public Object getChild(int groupPosition, int childPosition) {
return tvchannels.get(groupPosition).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;
}


@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
viewHolder holder = null;
if (convertView == null) {
holder = new viewHolder();
convertView = inflater.inflate(R.layout.eplv_main, null);
holder.tv_groupOrchild = (TextView) convertView
.findViewById(R.id.tv_station_eplv_main_stationname);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}


TVstation station = (TVstation) stations.get(groupPosition);
holder.tv_groupOrchild.setText(station.getTvName());


return convertView;
}


@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
viewHolder holder = null;
if (convertView == null) {
holder = new viewHolder();
convertView = inflater.inflate(R.layout.eplv_child, null);
holder.tv_groupOrchild = (TextView) convertView
.findViewById(R.id.tv_station_eplv_child_channelname);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}


final TVchannel tVchannel = (TVchannel) (tvchannels.get(groupPosition)
.get(childPosition));
holder.tv_groupOrchild.setText(tVchannel.getChnName());
//为每一项设置监听器
        holder.tv_groupOrchild.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(context,TVProgramActivity.class);
intent.putExtra(Consts.FOR_PROGRAM_ACTIVITY, tVchannel);
context.startActivity(intent);
}
});

return convertView;
}


class viewHolder {
TextView tv_groupOrchild;
}


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


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值