2014-10-30 86 views
0

你能告訴我爲什麼當我改變我的展開和摺疊列表視圖時,我的圖像沒有改變。這兩次我得到相同的圖像..當我展開它顯示(+)圖像時,我會顯示在下面的圖像。爲什麼?爲什麼圖像在展開和摺疊時不會改變。

這是我的代碼。

主要業務

public class MainActivity extends Activity { 

    ExpandableListAdapter listAdapter; 
    ExpandableListView expListView; 
    List<String> listDataHeader; 
    HashMap<String, List<String>> listDataChild; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // get the listview 
     expListView = (ExpandableListView) findViewById(R.id.lvExp); 

     // preparing list data 
     prepareListData(); 

     listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); 

     // setting list adapter 
     expListView.setAdapter(listAdapter); 

     // Listview Group click listener 
     expListView.setOnGroupClickListener(new OnGroupClickListener() { 

      @Override 
      public boolean onGroupClick(ExpandableListView parent, View v, 
        int groupPosition, long id) { 
       // Toast.makeText(getApplicationContext(), 
       // "Group Clicked " + listDataHeader.get(groupPosition), 
       // Toast.LENGTH_SHORT).show(); 
       return false; 
      } 
     }); 

     // Listview Group expanded listener 
     expListView.setOnGroupExpandListener(new OnGroupExpandListener() { 

      @Override 
      public void onGroupExpand(int groupPosition) { 
       if(listDataHeader.get(groupPosition) != null) 
       Toast.makeText(getApplicationContext(), 
         listDataHeader.get(groupPosition) + " Expanded", 
         Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     // Listview Group collasped listener 
     expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() { 

      @Override 
      public void onGroupCollapse(int groupPosition) { 
       Toast.makeText(getApplicationContext(), 
         listDataHeader.get(groupPosition) + " Collapsed", 
         Toast.LENGTH_SHORT).show(); 

      } 
     }); 

     // Listview on child click listener 
     expListView.setOnChildClickListener(new OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
        int groupPosition, int childPosition, long id) { 
       // TODO Auto-generated method stub 
       Toast.makeText(
         getApplicationContext(), 
         listDataHeader.get(groupPosition) 
           + " : " 
           + listDataChild.get(
             listDataHeader.get(groupPosition)).get(
             childPosition), Toast.LENGTH_SHORT) 
         .show(); 
       return false; 
      } 
     }); 
    } 

    /* 
    * Preparing the list data 
    */ 
    private void prepareListData() { 
     listDataHeader = new ArrayList<String>(); 
     listDataChild = new HashMap<String, List<String>>(); 

     // Adding child data 
     listDataHeader.add("Top 250"); 
     listDataHeader.add("Now Showing"); 
     listDataHeader.add("Coming Soon.."); 

     // Adding child data 
     List<String> top250 = new ArrayList<String>(); 
     top250.add("The Shawshank Redemption"); 
     top250.add("The Godfather"); 
     top250.add("The Godfather: Part II"); 
     top250.add("Pulp Fiction"); 
     top250.add("The Good, the Bad and the Ugly"); 
     top250.add("The Dark Knight"); 
     top250.add("12 Angry Men"); 

     List<String> nowShowing = new ArrayList<String>(); 
     nowShowing.add("The Conjuring"); 
     nowShowing.add("Despicable Me 2"); 
     nowShowing.add("Turbo"); 
     nowShowing.add("Grown Ups 2"); 
     nowShowing.add("Red 2"); 
     nowShowing.add("The Wolverine"); 

     /*List<String> comingSoon = new ArrayList<String>(); 
     comingSoon.add("2 Guns"); 
     comingSoon.add("The Smurfs 2"); 
     comingSoon.add("The Spectacular Now"); 
     comingSoon.add("The Canyons"); 
     comingSoon.add("Europa Report");*/ 

     listDataChild.put(listDataHeader.get(0), top250); // Header, Child data 
     listDataChild.put(listDataHeader.get(1), nowShowing); 
     listDataChild.put(listDataHeader.get(2), null); 
    } 
} 

適配器

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context _context; 
    private List<String> _listDataHeader; // header titles 
    // child data in format of header title, child title 
    private HashMap<String, List<String>> _listDataChild; 

    public ExpandableListAdapter(Context context, List<String> listDataHeader, 
      HashMap<String, List<String>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(int groupPosition, final int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 

     final String childText = (String) getChild(groupPosition, childPosition); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_item, null); 
     } 

     TextView txtListChild = (TextView) convertView 
       .findViewById(R.id.lblListItem); 

     txtListChild.setText(childText); 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     if(this._listDataChild.get(this._listDataHeader.get(groupPosition))!=null) 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size(); 
     return 0; 
    } 

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

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

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     String headerTitle = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_group, null); 
     } 

     TextView lblListHeader = (TextView) convertView 
       .findViewById(R.id.lblListHeader); 
     lblListHeader.setTypeface(null, Typeface.BOLD); 
     lblListHeader.setText(headerTitle); 
     View ind = convertView.findViewById(R.id.explist_indicator); 
     if(ind != null) { 
      ImageView indicator = (ImageView)ind; 
      if(getChildrenCount(groupPosition) == 0) { 
       indicator.setVisibility(View.INVISIBLE); 
      } else { 
       indicator.setVisibility(View.VISIBLE); 
       int stateSetIndex = (isExpanded ? 1 : 0) ; 
       Drawable drawable = indicator.getDrawable(); 
      } 
     } 

     return convertView; 
    } 

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

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

} 

activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#f4f4f4" > 

      <ExpandableListView 
       android:id="@+id/lvExp" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:cacheColorHint="#00000000" 
       android:groupIndicator="@android:color/transparent" 
       /> 

</LinearLayout> 

list_group.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="8dp" 
    android:background="#000000"> 

<ImageView android:src="@drawable/test" 
    android:id="@+id/explist_indicator" 
    android:layout_width="20px" 
    android:layout_height="20px"/> 
    <TextView 
     android:id="@+id/lblListHeader" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" 
     android:textSize="17dp" 
     android:textColor="#f9f93d" /> 

</LinearLayout> 

list_item.xml

![<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="55dip" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/lblListItem" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="17dip" 
     android:paddingTop="5dp" 
     android:paddingBottom="5dp" 
     android:textColor="#000000" 
     android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" /> 

</LinearLayout>][1] 

enter image description here

+1

是不是很明顯? ...因爲它是groupview中的靜態imageview ...不是groupIndicator ... **您必須使用groupIndicator和StateListDrawable ** ...當您展開/摺疊groupview時,getGroupView不會被調用,因此代碼裏面的if( ind!= null){...}'不要敏感......你可以將這段代碼放在OnGroupExpandListener/OnGroupCollapseListener中,但是,爲了FSM,爲什麼要重新發明輪子... – Selvin 2014-10-30 12:32:03

+0

爲什麼你不會在開始時從哪個博客你得到了代碼?你如何適應你的需求? 'Drawable drawable = indicator.getDrawable();'。你對原始代碼所做的任何事情都沒有做任何事情。 – greenapps 2014-10-30 12:52:09

+0

@Selvin我得到了解決方案..謝謝你的幫助 – user944513 2014-10-30 12:53:37

回答

0

我通過自己的。像解決這一問題的是

@Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     String headerTitle = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_group, null); 
     } 

     TextView lblListHeader = (TextView) convertView 
       .findViewById(R.id.lblListHeader); 
     lblListHeader.setTypeface(null, Typeface.BOLD); 
     lblListHeader.setText(headerTitle); 
     View ind = convertView.findViewById(R.id.explist_indicator); 
     if (ind != null) { 
      ImageView indicator = (ImageView) ind; 

      if (isExpanded) { 
       indicator.setImageResource(R.drawable.icon_3); 
      } else { 
       indicator.setImageResource(R.drawable.icon_4); 
      } 

      if (getChildrenCount(groupPosition) == 0) { 
       indicator.setVisibility(View.INVISIBLE); 
      } else { 
       indicator.setVisibility(View.VISIBLE); 
      } 
     } 

     return convertView; 
    } 

那樣變化圖像視圖

<ImageView 
     android:id="@+id/explist_indicator" 
     android:layout_width="20px" 
     android:layout_height="20px" 
     android:src="@drawable/icon_4" />