2012-02-09 68 views
0

我们可以在ListView中使用ExpandableListView,这样我就可以先拥有组级别,然后再有子项目。 只有ExpandableListView是组和孩子,我想有一个ExpandableListView里面的ListView。 尝试使用开源的TreeView,但无法获得ExpandableListView的所有功能。 期待您的回复。 谢谢。可扩展的ListView可以在ListView内部

+1

我不同意你的条款,你想ListView内的可扩展列表视图,因为**可扩展列表查看本身的列表视图**与特定项目展开每当你点击它。 – 2012-02-09 05:53:22

+1

最初使Expandalbe ListView的所有项目都折叠起来,每当用户点击[onChildClickListener](http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html)时展开它。 – 2012-02-09 05:54:48

+1

查看这个例子,我正在说什么:http://coderzheaven.com/2011/04/expandable-listview-in-android-using-simpleexpandablelistadapter-a-simple-example/ – 2012-02-09 05:57:43

回答

2

ExpandableListView本身的ListView,你不能使用ExpandableListView里面的ListView ...

0

但是你可以实现无ListView控件需要的相同的功能。这是相同的代码。

ExpandableListView适配器代码:

public class ExpandableListAdapter extends BaseExpandableListAdapter { 
    /** 
    * Holds the mapping details 
    */ 
    Mapdetail mMapDetials = new Mapdetail(); 
    /** 
    * Holds the contest 
    */ 
    private Context mContext; 
    /** 
    * Holds tehe xpandable list VIew 
    */ 
    private ExpandableListView mExpandableListView; 
    /** 
    * Holds the details for the cards 
    */ 
    private List<com.sourcebits.sfc.activity.GroupEntity> mGroupCollection; 
    /** 
    * Holds the status whether its expanded or not 
    */ 
    private int[] groupStatus; 

    public ExpandableListAdapter(Context pContext, 
      ExpandableListView pExpandableListView, 
      List<com.sourcebits.sfc.activity.GroupEntity> mGroupCollection2) { 
     mContext = pContext; 
     mGroupCollection = mGroupCollection2; 
     mExpandableListView = pExpandableListView; 
     groupStatus = new int[mGroupCollection.size()]; 
     setListEvent(); 
    } 

    /** 
    * Sets the event listeners 
    */ 
    private void setListEvent() { 

     mExpandableListView 
       .setOnGroupExpandListener(new OnGroupExpandListener() { 

        @Override 
        public void onGroupExpand(int arg0) { 
         groupStatus[arg0] = 1; 
        } 
       }); 

     mExpandableListView 
       .setOnGroupCollapseListener(new OnGroupCollapseListener() { 

        @Override 
        public void onGroupCollapse(int arg0) { 
         groupStatus[arg0] = 0; 
        } 
       }); 
    } 

    @Override 
    public String getChild(int arg0, int arg1) { 
     return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name; 
    } 

    @Override 
    public long getChildId(int arg0, int arg1) { 
     return 0; 
    } 

    @Override 
    public View getChildView(final int arg0, final int arg1, boolean arg2, 
      View arg3, ViewGroup arg4) { 

     ChildHolder childHolder; 
     if (arg3 == null) { 
      arg3 = LayoutInflater.from(mContext).inflate(
        R.layout.list_group_item, null); 

      childHolder = new ChildHolder(); 

      childHolder.title = (TextView) arg3.findViewById(R.id.item_title); 
      childHolder.background = (ImageView) arg3 
        .findViewById(R.id.background); 

      arg3.setTag(childHolder); 
     } else { 
      childHolder = (ChildHolder) arg3.getTag(); 
     } 

     if (arg1 == 0) { 
      childHolder.background.setBackgroundResource(R.drawable.list_top); 
     } else { 
      childHolder.background 
        .setBackgroundResource(R.drawable.list_bottom); 
     } 

     childHolder.title.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       mMapDetials.setmLanguageIndex(mGroupCollection.get(arg0).GroupItemCollection 
         .get(arg1).Name); 
       startNextActivity(); 
      } 
     }); 
     childHolder.title 
       .setText(mGroupCollection.get(arg0).GroupItemCollection 
         .get(arg1).Name); 

     return arg3; 
    } 

    @Override 
    public int getChildrenCount(int arg0) { 
     return mGroupCollection.get(arg0).GroupItemCollection.size(); 
    } 

    @Override 
    public Object getGroup(int arg0) { 
     return mGroupCollection.get(arg0); 
    } 

    @Override 
    public int getGroupCount() { 
     return mGroupCollection.size(); 
    } 

    @Override 
    public long getGroupId(int arg0) { 
     return arg0; 
    } 

    @Override 
    public View getGroupView(final int arg0, boolean arg1, View arg2, 
      ViewGroup arg3) { 

     GroupHolder groupHolder; 
     if (arg2 ==  null) { 
      arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group, 
        null); 
      groupHolder = new GroupHolder(); 
      groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img); 
      groupHolder.title = (TextView) arg2.findViewById(R.id.group_title); 
      arg2.setTag(groupHolder); 
     } else { 
      groupHolder = (GroupHolder) arg2.getTag(); 
     } 
     if (groupStatus[arg0] == 0) { 
      groupHolder.img.setImageResource(R.drawable.icon_expand); 
     } else { 

      mMapDetials.setTrainingPointIndex(mGroupCollection.get(arg0).Name); 
      groupHolder.img.setImageResource(R.drawable.icon_collapse); 
     } 
     groupHolder.img.setTag("NotClicked"); 
     if (mGroupCollection.get(arg0).GroupItemCollection.size() == 0) { 
      groupHolder.img.setImageResource(R.drawable.arrow); 
      mMapDetials.setmLanguageIndex("English"); 
      groupHolder.img.setTag("Clicked"); 
      groupHolder.img.setOnTouchListener(new OnTouchListener() { 

       @Override 
       public boolean onTouch(View v, MotionEvent event) { 
        if (v.getTag().toString().equals("Clicked")) { 
         mMapDetials.setTrainingPointIndex(mGroupCollection 
           .get(arg0).Name); 
         startNextActivity(); 
         return false; 
        } 
        return false; 
       } 
      }); 

     } 

     groupHolder.title.setText(mGroupCollection.get(arg0).Name); 

     return arg2; 
    } 

    /** 
    * Holds the view of the parent 
    * 
    * @author Gautam Balasubramanian 
    * 
    */ 
    class GroupHolder { 
     ImageView img; 
     TextView title; 
    } 

    /** 
    * ChildHolder class view of the child view 
    * 
    * @author Gautam Balasubramanian 
    * 
    */ 
    class ChildHolder { 
     TextView title; 
     ImageView background; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

    @Override 
    public boolean isChildSelectable(int arg0, int arg1) { 
     return true; 
    } 

    void startNextActivity() { 

     Intent intent = new Intent(mContext, GeneralInfo.class); 
     mMapDetials.toString(); 
     Bundle extras = new Bundle(); 
     extras.putSerializable("values", mMapDetials); 
     intent.putExtras(extras); 
     mContext.startActivity(intent); 

    } 
} 

代码从activty拨打:

private void initPage() { 
    mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView); 
    mExpandableListView.setCacheColorHint(0); 
    ExpandableListAdapterforProducts adapter = new ExpandableListAdapterforProducts(
      Products.this, mExpandableListView, mGroupCollection); 

    mExpandableListView.setAdapter(adapter); 
    ColorDrawable sage = new ColorDrawable(this.getResources().getColor(
      R.color.sage)); 
    mExpandableListView.setDivider(sage); 
    mExpandableListView.setDividerHeight(1); 
} 

Initailisation值,

mySQLiteAdapter = new SqliteAdapterForProducts(this); 
    mySQLiteAdapter.openToRead(); 
    mCursor = mySQLiteAdapter.queueProducts(); 
    Log.v("", mCursor.getCount() + "is the count"); 
    mCursor.moveToFirst(); 
    mGroupCollection = new ArrayList<GroupEntity>(); 

    for (int i = 0; i < mCursor.getCount(); i++) { 
     GroupEntity groupEntity = new GroupEntity(); 
     groupEntity.Name = mCursor.getString(1) + " " 
       + mCursor.getString(2); 

     String languages[] = mCursor.getString(3).toString().split(","); 
     if (languages.length > 1) { 
      for (int j = 0; j < languages.length; j++) { 
       GroupItemEntity groupItemEntity = groupEntity.new GroupItemEntity(); 
       groupItemEntity.Name = languages[j]; 
       groupEntity.GroupItemCollection.add(groupItemEntity); 
      } 
     } 

     mCursor.moveToNext(); 
     mGroupCollection.add(groupEntity); 
    } 

    mCursor.close(); 
    mySQLiteAdapter.close(); 

以上的东西为我工作。你可以尝试一下。