本文介绍了ExpandableListview如何自动groupExpand - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能请帮助我如何自动扩展该集团在我的列表视图扩展,我只有一个组。有5个项目。我想自动扩展它,所以我没有点击它。我用groupExpand(0),但它不工作:(。这里是我的全code。

 公共类ExpandActivity扩展ExpandableListActivity {//的String [] =行动新的String [] {
//,
    //设置,
    // 关于,    //};
 ListView控件的ListView;
 TextView的txtView;
@燮pressLint(的SimpleDateFormat)
@燮pressWarnings({未登记,德precation})
公共无效的onCreate(捆绑savedInstanceState){
    尝试{
         super.onCreate(savedInstanceState);
         的setContentView(R.layout.expand);
            ((ExpandableListView)的ListView).expandGroup(0);    SimpleExpandableListAdapter expListAdapter =
        新SimpleExpandableListAdapter(
                这个,
                createGroupList()//创建组列表。
                R.layout.group_row,//集团项目布局XML。
                新的String [] {组项目} //组项目的关键。
                新的INT [] {} R.id.row_name,//每组item.-数据项下的ID进入这个TextView的。
                createChildList(),// childData描述的第二级条目。
                R.layout.child_row,//布局分层次项(第二级)。
                新的String [] {子项} //在childData键映射到相应显示。
                新的INT [] {R.id.grp_child} //数据在按键上方进入这些TextViews。
            );
        setListAdapter(expListAdapter); //设置在列表中的适配器。    }赶上(例外五){
        的System.out.println(Errrr ++++ e.getMessage());
    }
}/ *创建HashMap中的行* /
@燮pressWarnings(未登记)
私人列表createGroupList(){
      ArrayList的结果=新的ArrayList();
      的for(int i = 0; I< 1; ++ I){// 15组........
        HashMap的M =新的HashMap();
        m.put(「本集团项目,更新); //键和它的价值。        result.add(米);
        ((ExpandableListView)的ListView).expandGroup(0);      }
      回报(列表)的结果;}/ *肌酸HashMap中为孩子* /
@燮pressWarnings(未登记)
私人列表createChildList(){    ArrayList的结果=新的ArrayList();
    对(INT I = 0; I&。1 ++ⅰ){//这-15是(这是十五年)基团的数目
      / *每组需要彼此的HashMap,在这里,每个组我们有3个亚组* /
      ArrayList的secList =新的ArrayList();
      对于(INT N = 0; N< 1; N ++){
        HashMap的孩子=新的HashMap();
        child.put(子项,DILC);
        secList.add(小孩);
      }
     result.add(secList);
     ArrayList的secList2 =新的ArrayList();
  对于(INT N = 0; N< 1; N ++){
    HashMap的孩子=新的HashMap();
    child.put(子项,OVRCD);
    secList.add(小孩);
  }
 result.add(secList2);
ArrayList的secList3 =新的ArrayList();
  对于(INT N = 0; N< 1; N ++){
    HashMap的孩子=新的HashMap();
    child.put(子项,UPD信息);
    secList.add(小孩);
  }
 result.add(secList3);ArrayList的secList4 =新的ArrayList();
  对于(INT N = 0; N< 1; N ++){
    HashMap的孩子=新的HashMap();
    child.put(子项,UP系统);
    secList.add(小孩);
  }
 result.add(secList4); ArrayList的secList5 =新的ArrayList();
  对于(INT N = 0; N< 1; N ++){
    HashMap的孩子=新的HashMap();
    child.put(子项,所有更新);
    secList.add(小孩);
  }
 result.add(secList5);    }
    返回结果;
}
公共无效onContentChanged(){
    的System.out.println(onContentChanged);
    super.onContentChanged();
}/ *调用此函数对每个孩子的点击* /
@燮pressLint(NewApi)
公共布尔onChildClick(ExpandableListView父视图V,INT groupPosition,诠释childPosition,长ID){
     ExpandableListAdapter ADAP = parent.getExpandableListAdapter();
     INT GP =(int)的adap.getGroupId(groupPosition);
     INT CP =(int)的adap.getChildId(groupPosition,childPosition);
     如果(GP == 0){
         开关(CP){
         情况下0:
             意向意图=新意图(getApplicationContext()
                 RssTabsActivity.class);
              startActivity(意向);
              overridePendingTransition(R.anim.right_slide_in,R.anim.right_slide_in);
                 打破;            情况1:
             意图int​​ent1 =新意图(getApplicationContext()
                     OVRCD.class);
             startActivity(intent1);
             overridePendingTransition(R.anim.right_slide_in,R.anim.right_slide_in);
                打破;            案例2:
                 意图int​​ent2 =新意图(getApplicationContext()
                         UPDInfo.class);
                 startActivity(intent2);
                 overridePendingTransition(R.anim.right_slide_in,R.anim.right_slide_in);
                 打破;
            案例3:
             意图int​​ent3 =新意图(getApplicationContext()
                     UPSystem.class);
                startActivity(intent3);
                overridePendingTransition(R.anim.right_slide_in,R.anim.right_slide_in);
                打破;            情况4:
                 意图int​​ent4 =新意图(getApplicationContext()
                         ALLUPD.class);
                   startActivity(intent4);
                   overridePendingTransition(R.anim.right_slide_in,R.anim.right_slide_in);
                   打破;             }
         }否则如果(GP == 1){
             开关(CP){
             情况下0:
             意向意图=新意图(getApplicationContext()
                     RssTabsActivity.class);
            startActivity(意向);
            overridePendingTransition(R.anim.right_slide_in,R.anim.right_slide_in);
                 打破;
             情况1:
             意图int​​ent1 =新意图(getApplicationContext()
                     RssTabsActivity.class);
            startActivity(intent1);
            overridePendingTransition(R.anim.right_slide_in,R.anim.right_slide_in);
                 打破;
             案例2:
                 意图int​​ent2 =新意图(getApplicationContext()
                         RssTabsActivity.class);
                startActivity(intent2);
                overridePendingTransition(R.anim.right_slide_in,R.anim.right_slide_in);
                     打破;
             案例3:
                 意图int​​ent3 =新意图(getApplicationContext()
                         RssTabsActivity.class);
                startActivity(intent3);
                overridePendingTransition(R.anim.right_slide_in,R.anim.right_slide_in);
                     打破;
             情况4:
                 意图int​​ent4 =新意图(getApplicationContext()
                         RssTabsActivity.class);
                startActivity(intent4);
                overridePendingTransition(R.anim.right_slide_in,R.anim.right_slide_in);
                     打破;
             }
         }         返回true;
     };
     @燮pressWarnings(空)
    公共查看getChildView(INT groupPosition,诠释childPosition,布尔isLastChild,查看convertView,父母的ViewGroup){         ExpandableListView expView = getExpandableListView();
        ContextThemeWrapper语境= NULL;
        expView.setChildIndicator(context.getResources()。getDrawable(
                 R.drawable.arrow_right));
        expView.setChildDivider(getResources()getDrawable(R.color.grey));         expView.setDividerHeight(2);
         回到父母;     }
/ *调用此函数上展开组* /
公共无效onGroupExpand(INT groupPosition){
    尝试{
         的System.out.println(本集团exapanding监听=> groupPosition =+ groupPosition);
    }赶上(例外五){
        的System.out.println(groupPosition Errrr ++++ e.getMessage());
    }}


解决方案

做的是这样的:

  expList.setAdapter(mNewAdapter);                的for(int i = 0; I< mNewAdapter.getGroupCount();我++)
                    expList.expandGroup(ⅰ);
                expList.setOnGroupClickListener(新OnGroupClickListener(){
                    @覆盖
                    公共布尔onGroupClick(ExpandableListView父母,
                            视图V,INT groupPosition,长ID){
                        返回true; //这样,膨胀器不能
                                        //倒塌
                    }
                });

 < ExpandableListView
        机器人:ID =@ + ID / e_lodinfo
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:滚动条=无
        机器人:groupIndicator =@空
         >
    < / ExpandableListView>

Can anyone please help me how to automatically expand the group in my Expandable Listview, I only have one group. with 5 items. I want to expand it automatically so I don't have to click it. I used groupExpand(0), But its not working :( .Here is my full code.

public class ExpandActivity extends ExpandableListActivity {

//String[] actions = new String[] {
//      "",
    //  "Settings",
    //  "About",

    //};
 ListView listView ;
 TextView txtView;


@SuppressLint("SimpleDateFormat")
@SuppressWarnings({ "unchecked", "deprecation" })
public void onCreate(Bundle savedInstanceState) {
    try{
         super.onCreate(savedInstanceState);
         setContentView(R.layout.expand);


            ((ExpandableListView) listView).expandGroup(0);

    SimpleExpandableListAdapter expListAdapter =
        new SimpleExpandableListAdapter(
                this,
                createGroupList(),              // Creating group List.
                R.layout.group_row,             // Group item layout XML.
                new String[] { "Group Item" },  // the key of group item.
                new int[] { R.id.row_name },    // ID of each group item.-Data under the key goes into this TextView.
                createChildList(),              // childData describes second-level entries.
                R.layout.child_row,             // Layout for sub-level entries(second level).
                new String[] {"Sub Item"},      // Keys in childData maps to display.
                new int[] { R.id.grp_child}     // Data under the keys above go into these TextViews.
            );
        setListAdapter( expListAdapter );       // setting the adapter in the list.

    }catch(Exception e){
        System.out.println("Errrr +++ " + e.getMessage());
    }
}

/* Creating the Hashmap for the row */
@SuppressWarnings("unchecked")
private List createGroupList() {
      ArrayList result = new ArrayList();
      for( int i = 0 ; i < 1 ; ++i ) { // 15 groups........
        HashMap m = new HashMap();
        m.put( "Group Item","Updates"); // the key and it's value.

        result.add( m );
        ((ExpandableListView) listView).expandGroup(0);

      }
      return (List)result;



}

/* creatin the HashMap for the children */
@SuppressWarnings("unchecked")
private List createChildList() {

    ArrayList result = new ArrayList();
    for( int i = 0 ; i < 1 ; ++i ) { // this -15 is the number of groups(Here it's fifteen)
      /* each group need each HashMap-Here for each group we have 3 subgroups */
      ArrayList secList = new ArrayList();
      for( int n = 0 ; n < 1 ; n++ ) {
        HashMap child = new HashMap();
        child.put( "Sub Item", "DILC");
        secList.add( child );
      }
     result.add( secList );


     ArrayList secList2 = new ArrayList();
  for( int n = 0 ; n < 1 ; n++ ) {
    HashMap child = new HashMap();
    child.put( "Sub Item", "OVRCD");
    secList.add( child );
  }
 result.add( secList2 );


ArrayList secList3 = new ArrayList();
  for( int n = 0 ; n < 1 ; n++ ) {
    HashMap child = new HashMap();
    child.put( "Sub Item", "UPD Information");
    secList.add( child );
  }
 result.add( secList3 );

ArrayList secList4 = new ArrayList();
  for( int n = 0 ; n < 1 ; n++ ) {
    HashMap child = new HashMap();
    child.put( "Sub Item", "UP System");
    secList.add( child );
  }
 result.add( secList4 );

 ArrayList secList5 = new ArrayList();
  for( int n = 0 ; n < 1 ; n++ ) {
    HashMap child = new HashMap();
    child.put( "Sub Item", "All Updates");
    secList.add( child );
  }
 result.add( secList5 );

    }
    return result;
}
public void  onContentChanged  () {
    System.out.println("onContentChanged");
    super.onContentChanged();
}



/* This function is called on each child click */
@SuppressLint("NewApi")
public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
     ExpandableListAdapter adap = parent.getExpandableListAdapter();
     int gp = (int) adap.getGroupId(groupPosition);
     int cp = (int) adap.getChildId(groupPosition, childPosition);
     if (gp == 0) {
         switch (cp) {
         case 0:
             Intent intent = new Intent(getApplicationContext(),
                 RssTabsActivity.class);
              startActivity(intent);
              overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_in);
                 break;

            case 1:
             Intent intent1 = new Intent(getApplicationContext(),
                     OVRCD.class);
             startActivity(intent1);
             overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_in);
                break;

            case 2:
                 Intent intent2 = new Intent(getApplicationContext(),
                         UPDInfo.class);
                 startActivity(intent2);
                 overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_in);
                 break;
            case 3:
             Intent intent3 = new Intent(getApplicationContext(),
                     UPSystem.class);
                startActivity(intent3);
                overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_in);
                break;

            case 4:
                 Intent intent4 = new Intent(getApplicationContext(),
                         ALLUPD.class);
                   startActivity(intent4);
                   overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_in);
                   break;

             }
         } else if (gp == 1) {
             switch (cp) {
             case 0:
             Intent intent = new Intent(getApplicationContext(),
                     RssTabsActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_in);
                 break;
             case 1:
             Intent intent1 = new Intent(getApplicationContext(),
                     RssTabsActivity.class);
            startActivity(intent1);
            overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_in);
                 break;
             case 2:
                 Intent intent2 = new Intent(getApplicationContext(),
                         RssTabsActivity.class);
                startActivity(intent2);
                overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_in);
                     break;
             case 3:
                 Intent intent3 = new Intent(getApplicationContext(),
                         RssTabsActivity.class);
                startActivity(intent3);
                overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_in);
                     break;
             case 4:
                 Intent intent4 = new Intent(getApplicationContext(),
                         RssTabsActivity.class);
                startActivity(intent4);
                overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_in);
                     break;
             }
         }

         return true;
     };




     @SuppressWarnings("null")
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent){

         ExpandableListView expView = getExpandableListView();


        ContextThemeWrapper context = null;
        expView.setChildIndicator(context.getResources().getDrawable(
                 R.drawable.arrow_right));
        expView.setChildDivider(getResources().getDrawable(R.color.grey));

         expView.setDividerHeight(2);


         return parent;

     }




/* This function is called on expansion of the group */
public void  onGroupExpand  (int groupPosition) {
    try{
         System.out.println("Group exapanding Listener => groupPosition = " + groupPosition);
    }catch(Exception e){
        System.out.println(" groupPosition Errrr +++ " + e.getMessage());
    }

}
解决方案

do this way:

                expList.setAdapter(mNewAdapter);

                for (int i = 0; i < mNewAdapter.getGroupCount(); i++)
                    expList.expandGroup(i);


                expList.setOnGroupClickListener(new OnGroupClickListener() {
                    @Override
                    public boolean onGroupClick(ExpandableListView parent,
                            View v, int groupPosition, long id) {
                        return true; // This way the expander cannot be
                                        // collapsed
                    }
                });

and:

<ExpandableListView
        android:id="@+id/e_lodinfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:groupIndicator="@null"
         >
    </ExpandableListView>

这篇关于ExpandableListview如何自动groupExpand - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:15