2012-02-09 5 views
0

мы можем иметь ExpandableListView внутри ListView, чтобы у меня мог быть первый, а затем дочерний элементы уровня группы. Только ExpandableListView является группой и дочерним, я хочу иметь расширенный список ListView ListView. Пробовал с открытым исходным кодом TreeView, но не смог получить конкретно все те функции ExpandableListView. Ждем вашего ответа. спасибо.Может ли расширяемый ListView быть внутри ListView

+1

Я не согласен с вашими условиями, что вы хотите расширить список ListView в ListView, потому что ** Расширяемое listview само listview ** с определенным элементом расширяется всякий раз, когда вы нажимаете на него. –

+1

Изначально создавайте все элементы Expandalbe ListView как спящие, разворачивайте его, когда пользователь нажимает на него [onChildClickListener] (http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html). –

+1

Проверьте этот пример на то, что я говорю: http://coderzheaven.com/2011/04/expandable-listview-in-android-using-simpleexpandablelistadapter-a-simple-example/ –

ответ

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(); 

выше материал работал для меня. Можешь попробовать.

Смежные вопросы