2012-05-31 74 views
3

我試圖在我的項目中實現exapndablelistview。我能夠擴大listView,問題是我只想當我點擊圖片時展開它,但是當我點擊列表視圖行的任何地方時,列表就被展開。 請幫我解決這個問題。單擊按鈕時展開EpandableListView

在此先感謝。

我的XML代碼

<ExpandableListView 
     android:id="@+id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_weight="1" 
     android:drawSelectorOnTop="false" 
     android:groupIndicator="@android:color/transparent"> 
    </ExpandableListView> 

和我的java類是

public class MainActivity extends ExpandableListActivity{ 
    TextView tv; 
    ExpandableListView lv; 
    Button b1; 
    Integer imgk[]={R.drawable.dhoni,R.drawable.ganguly,R.drawable.irfan,R.drawable.rahul,R.drawable.sachin,R.drawable.sehwag,R.drawable.singh,R.drawable.sri,R.drawable.uthapa,R.drawable.yuvi}; 
    String[] names={"MS Dhoni","Sorav Ganguly","Irfan Pathan","Rahul Dravid","Sachin Tendulkar","Virender Sehwag","Harbajan Singh","Sreeshanth","Robin Uthapa","Yuvraj Singh"}; 
    String matches[]={"21","12","13","15","35","22","25","18","21","31"}; 
    String[] fifty={"12","21","16","10","16","18","10","20","19","22"}; 
    String hund[]={"21","12","17","18","13","21","23","10","21","14"}; 
    String[] team={"India","pakistan","bangladesh","australia","new zealand","south Africa","england","zimbabwe","west indies","sri lanka"}; 
    String[][] childs={{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"},{"India","pakistan"}}; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.myscreen); 

     tv = (TextView) this.findViewById(R.id.textView1); 
     tv.setSelected(true); 

     lv=getExpandableListView(); 

    CustomAdapter ca=new CustomAdapter(MainActivity.this); 
     lv.setAdapter(ca); 
    } 





    public class CustomAdapter extends BaseExpandableListAdapter{ 
    Context con; 
    private LayoutInflater myInflater; 
    private Bitmap bm; 
    int count=0; 
    public CustomAdapter(Context con) { 
     // TODO Auto-generated constructor stub 
     this.con=con; 
     myInflater=LayoutInflater.from(con); 
    } 

    @Override 
    public int getGroupCount() { 
     // TODO Auto-generated method stub 
     return names.length; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     // TODO Auto-generated method stub 
     return childs[groupPosition].length; 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     // TODO Auto-generated method stub 
     return names[groupPosition]; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     // TODO Auto-generated method stub 
     return childs[groupPosition][childPosition]; 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     // TODO Auto-generated method stub 
     return groupPosition; 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     // TODO Auto-generated method stub 
     return childPosition; 
    } 

    @Override 
    public boolean hasStableIds() { 
     // TODO Auto-generated method stub 
     return true; 
    } 

    @Override 
    public View getGroupView(final int groupPosition, final boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     final ViewHolder holder; 
      if (convertView == null) { 
      convertView = myInflater.inflate(R.layout.inflatexml, null); 
      holder = new ViewHolder(); 

      holder.txtName = (TextView) convertView.findViewById(R.id.textView1); 

      holder.img=(ImageView)convertView.findViewById(R.id.imageView1); 
      holder.img1= (ImageView)convertView.findViewById(R.id.imageView2); 
      holder.down=(ImageView)convertView.findViewById(R.id.imageView4); 
      convertView.setTag(holder); 
      } 
      else { 
      holder = (ViewHolder) convertView.getTag(); 
      } 

      holder.txtName.setText(names[groupPosition]); 
      holder.img.setImageResource(imgk[groupPosition]); 


      holder.img1.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Bitmap bm=BitmapFactory.decodeResource(MainActivity.this.getResources(), 
         imgk[groupPosition]); 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos); 
        byte[] b = baos.toByteArray(); 
        Intent in=new Intent(MainActivity.this,Details.class); 
        in.putExtra("img", b); 
        in.putExtra("names", names[groupPosition]); 
        in.putExtra("matches", matches[groupPosition]); 
        in.putExtra("fifty", fifty[groupPosition]); 
        in.putExtra("hund", hund[groupPosition]); 
        in.putExtra("team", team[groupPosition]); 
        startActivity(in); 
       } 
      }); 
      holder.down.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        count++; 
        if(isExpanded){ 
         holder.down.setFocusable(false); 
         lv.collapseGroup(groupPosition); 
         System.out.println("collapsed....."); 
        } 

        else{ 
         holder.down.setFocusable(false); 
         lv.expandGroup(groupPosition); 
         System.out.println("Expanded....."); 
        } 
       } 
      }); 

      return convertView; 

    } 

    @Override 
    public View getChildView(int groupPosition, int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     TextView tv=getGenericView(); 
     tv.setText(""+childs[groupPosition][childPosition]); 
     return tv; 
    } 

    public TextView getGenericView() { 
     // TODO Auto-generated method stub 
     AbsListView.LayoutParams lp=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT); 
     TextView textView = new TextView(MainActivity.this); 
     textView.setLayoutParams(lp); 
     return textView; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     // TODO Auto-generated method stub 
     return true; 
    } 

} 
    static class ViewHolder { 
     TextView txtName; 
     ImageView img,img1,down; 
    } 
} 

回答

0

如果你想通過點擊特定的按鈕或其他一些視圖展開/摺疊組,你必須得到該Button中的getGroupView方法在您的Adapter類中。然後,在您的Button的onClick方法中,您可以將父項轉換爲ExpandableListView,也可以在創建適配器時在構造函數中傳遞List的引用。

我更喜歡第一種方法。這裏是代碼,假設你有一個TextView和一個ImageView是箭頭。我也添加了改變箭頭狀態。 @Override 公共視圖getGroupView(最終詮釋groupPosition,最終布爾isExpanded, 視圖convertView,最終的ViewGroup父){

String headerTitle = (String) getGroup(groupPosition); 

if (convertView == null) { 
    LayoutInflater infalInflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = infalInflater.inflate(R.layout.left_drawer_list_group, parent, false); 
} 

TextView listHeaderText = (TextView) convertView 
     .findViewById(R.id.left_menu_list_header_text); 
ImageView listHeaderArrow = (ImageView) convertView.findViewById(R.id.left_menu_list_header_arrow); 

listHeaderText.setText(headerTitle); 

//Set the arrow programatically, so we can control it 
int imageResourceId = isExpanded ? android.R.drawable.arrow_up_float : android.R.drawable.arrow_down_float; 
listHeaderArrow.setImageResource(imageResourceId); 

listHeaderArrow.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 

     if(isExpanded) ((ExpandableListView) parent).collapseGroup(groupPosition); 
     else ((ExpandableListView) parent).expandGroup(groupPosition, true); 

    } 
}); 

return convertView; 

}