3

プロジェクトに exapndablelistview を実装しようとしています。問題は、画像をクリックしたときにのみ展開したいだけですが、リストビュー行のどこかをクリックするとリストが展開されます。この問題を解決するために私を助けてください。

前もって感謝します。

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;
     }
}
4

1 に答える 1

0

特定のボタンまたは他のビューをクリックしてグループを展開/折りたたむ場合は、アダプタ クラスの getGroupView メソッドでそのボタンを取得する必要があります。次に、ボタンの onClick メソッドで、親を ExpandableListView にキャストするか、アダプターを作成するときにコンストラクターでリストの参照を渡します。

私は最初のアプローチを好みます。1 つの TextView と矢印である ImageView があると仮定した場合のコードを次に示します。矢印の状態の変更も追加しました。@Override public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, final ViewGroup parent) {

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;

}

于 2015-06-22T11:38:42.200 に答える