自定义ExpandableListView及ExpandableListViewAdapter使二级项可独立滚动显示



项目要求要实现类似下图效果:ExpandableListView点击打开二级项是一个可以独立滚动的listview,这里借助李刚疯狂android的例子做修改做为demo


为了能让listview可以独立滚动,这里重写了ExpandableListView,继承ExpandableListView,并重写了onInterceptTouchEvent()方法返回fasle,使其二级项可以独立滚动。
代码:MyExpandableListView

<span style="color:#000000;">import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ExpandableListView;
import android.widget.ListView;
public class MyExpandableListView extends ExpandableListView {
 public MyExpandableListView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
 }
 public MyExpandableListView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
 }
 public MyExpandableListView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }
 @Override //重写该方法使二级项可以滚动,而ExpandableListView一级菜单不随之滚动
 public boolean onInterceptTouchEvent(MotionEvent ev) {
  return false;
 }

}</span>

下面main.xml 只是一个我们重写的ExpandableListView




<span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<org.crazyit.listview.MyExpandableListView
 android:id="@+id/list"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
</LinearLayout></span>


二级项布局文件child.xml,在一个LinearLayout中定义一个用来显示数据的ListView

<span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <ListView
  android:id="@+id/mylistview"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />
</LinearLayout></span>


<span style="color:#000000;">下面重点来了,为ExpandablieListView 写适配器</span>
<span style="color:#000000;">
</span>

<span style="color:#000000;">ExpandableListAdapter adapter = new BaseExpandableListAdapter()
  {
   int[] logos = new int[]
   {
    R.drawable.p,
    R.drawable.z,
    R.drawable.t
   };
   //获取指定组位置、指定子列表项处的子列表项数据
   @Override
   public Object getChild(int groupPosition, int childPosition)
   {
    //这里返回子项的view,即R.layout.child的view
    return ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.child, null);
   }
   @Override
   public long getChildId(int groupPosition, int childPosition)
   {
    //这里返回布局id
    return R.layout.child;
   }
   @Override
   public int getChildrenCount(int groupPosition)
   {
    //这里返回1,表示每个ExpandableListView一级项下面只有一个二级项,即布局中的LinearLayout
    return 1;
   }
   private TextView getTextView()
   {
    AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
      ViewGroup.LayoutParams.FILL_PARENT, 64);
    TextView textView = new TextView(ExpandableListViewTest.this);
    textView.setLayoutParams(lp);
    textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
    textView.setPadding(36, 0, 0, 0);
    textView.setTextSize(20);
    return textView;
   }
   //该方法决定每个子选项的外观
   @Override //这个方法是返回二级项的view,在该view中用一个listview用来显示数据
   public View getChildView(int groupPosition, int childPosition,
     boolean isLastChild, View convertView, ViewGroup parent)
   {
    View view = null ;
    if(view == null){
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     view = inflater.inflate(R.layout.child, null);
     ListView listview =(ListView)view.findViewById(R.id.mylistview);
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_list_item_1,arms);
     listview.setAdapter(adapter);
    }
    return view;
   }
   //获取指定组位置处的组数据
   @Override
   public Object getGroup(int groupPosition)
   {
    return armTypes[groupPosition];
   }
   @Override
   public int getGroupCount()
   {
    return armTypes.length;
   }
   @Override
   public long getGroupId(int groupPosition)
   {
    return groupPosition;
   }
   //该方法决定每个组选项的外观
   @Override //该方法与android原ExpandableListViewAdapter中一样,即获得一级项的view及显示
   public View getGroupView(int groupPosition, boolean isExpanded,
     View convertView, ViewGroup parent)
   {
    LinearLayout ll = new LinearLayout(ExpandableListViewTest.this);
    ll.setOrientation(0);
    ImageView logo = new ImageView(ExpandableListViewTest.this);
    logo.setImageResource(logos[groupPosition]);
    ll.addView(logo);
    TextView textView = getTextView();
    textView.setText(getGroup(groupPosition).toString());
    ll.addView(textView);
    return ll;
   }
   @Override
   public boolean isChildSelectable(int groupPosition, int childPosition)
   {
    return true;
   }
   @Override
   public boolean hasStableIds()
   {
    return true;
   }
  };
</span>

最后在activity中显示

public class ExpandableListViewTest extends Activity
{
 private MyExpandableListView myExpanableListView;
 private String[] armTypes = new String[]
   { "神族兵种", "虫族兵种", "人族兵种"};
 
 private String[] arms = new String[]
   { "狂战士", "龙骑士", "黑暗圣堂", "电兵"};
 myExpanableListView = (MyExpandableListView)findViewById(R.id.list);
   myExpanableListView.setAdapter(adapter);


到此功能已实现,具体优化美观数据什么的自己修改。

第一次写CSDN博客,请多多谅解。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值