2012-05-31 6 views
3

내 프로젝트에서 exapndablelistview를 구현하려고합니다. 나는 listView를 확장 할 수 있는데 문제는 단지 이미지를 클릭 할 때만 그것을 확장하려고하지만 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> 

하고 내 자바 클래스를 사용하면 특정 버튼이나 다른보기를 클릭하여/축소 그룹을 확장합니다

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

, 당신은 얻을 수있을 것입니다 Adapter 클래스의 getGroupView 메소드에있는 Button. 그런 다음, 부모가 ExpandableListView로 캐스팅하거나 어댑터를 생성 할 때 생성자에서 List의 참조를 전달하기 위해 사용하는 Button의 onClick 메서드에서.

나는 첫 번째 방법을 선호합니다. 여기에 코드가 있습니다. 하나의 TextView와 ImageView가 화살표라고 가정합니다. 나는 화살표 상태를 변경하는 것을 추가했다. @Override 공공보기 getGroupView (최종 INT의 groupPosition, 최종 부울는 true, 보기 convertView, 최종 뷰 그룹의 부모) {

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; 

}

관련 문제