2012-04-18 6 views
0

http://www.techienjoy.com/android-expandable-list-dynamically-created-example.php과 같은 확장 가능한 목록을 만들었지 만, 아이가 가로로 보이지 않게 세로로 보게하고 싶습니다. 다른확장 가능 목록 뷰, 수직 하위

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<ExpandableListView android:id="@android:id/list" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:groupIndicator="@null"/> 

모든 클래스에 있습니다

난 단지 1 XML을 가지고있다. 어떻게 수직으로 설정할 수 있습니까? 내 자바 코드는 링크와 동일합니다.

답변

0

이 예를 살펴보십시오. 희망이 당신을 도울 것입니다.

public class ExpandListViewActivity extends ExpandableListActivity 
{ 
/** 
    * strings for group elements 
    */ 
    static final String arrGroupelements[] = 
    { 
    "India", 
    "Australia", 
    "England", 
    "South Africa" 
}; 

    /** 
    * strings for child elements 
    */ 
static final String arrChildelements[][] = 
{ 
    { 
    "Sachin Tendulkar", 
    "Raina", 
    "Dhoni", 
    "Yuvi" 
    }, 
    { 
    "Ponting", 
    "Adam Gilchrist", 
    "Michael Clarke" 
    }, 
    { 
    "Andrew Strauss", 
    "kevin Peterson", 
    "Nasser Hussain" 
    }, 
    { 
    "Graeme Smith", 
    "AB de villiers", 
    "Jacques Kallis" 
    } 
    }; 
static final int arrChildelementsim[][] = 
{ 
    { 
    R.drawable.ic_launcher, 
    R.drawable.aqua, 
    R.drawable.pin_red, 
    R.drawable.aqua, 
    }, 
    { 
     R.drawable.ic_launcher, 
     R.drawable.ic_launcher, 
     R.drawable.ic_launcher, 

    }, 
    { 
     R.drawable.ic_launcher, 
     R.drawable.ic_launcher, 
     R.drawable.ic_launcher, 

    }, 
    { 
     R.drawable.ic_launcher, 
     R.drawable.ic_launcher, 
     R.drawable.ic_launcher, 

    } 
    }; 
DisplayMetrics metrics; 
int width; 
ExpandableListView expList; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.main); 

     expList = getExpandableListView(); 
     metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     width = metrics.widthPixels; 
     //this code for adjusting the group indicator into right side of the view 
     expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10)); 
     expList.setAdapter(new ExpAdapter(this)); 

    expList.setOnGroupExpandListener(new OnGroupExpandListener() 
    { 
    @Override 
    public void onGroupExpand(int groupPosition) 
    { 
    Log.e("onGroupExpand", "OK"); 
    } 
    }); 

    expList.setOnGroupCollapseListener(new OnGroupCollapseListener() 
    { 
    @Override 
    public void onGroupCollapse(int groupPosition) 
    { 
    Log.e("onGroupCollapse", "OK"); 
    } 
    }); 

    expList.setOnChildClickListener(new OnChildClickListener() 
    { 

@Override 
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, 
     int arg3, long arg4) { 
    Log.e("OnChildClickListener", "OK"); 
    // TODO Auto-generated method stub 
    return false; 
} 
    }); 
    } 

    public int GetDipsFromPixel(float pixels) 
    { 
    // Get the screen's density scale 
    final float scale = getResources().getDisplayMetrics().density; 
    // Convert the dps to pixels, based on density scale 
    return (int) (pixels * scale + 0.5f); 
    } 
    public class ExpAdapter extends BaseExpandableListAdapter { 

      private Context myContext; 
      public ExpAdapter(Context context) { 
      myContext = context; 
      } 
      @Override 
      public Object getChild(int groupPosition, int childPosition) { 
      return null; 
      } 

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

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

      if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.child_row, null); 
      } 

      TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName); 
      ImageView tvPlayerimage = (ImageView) convertView.findViewById(R.id.tvPlayerImage); 
      tvPlayerName.setText(arrChildelements[groupPosition][childPosition]); 
      tvPlayerimage.setImageResource(arrChildelementsim[groupPosition][childPosition]); 
      return convertView; 
      } 

      @Override 
      public int getChildrenCount(int groupPosition) { 
      return arrChildelements[groupPosition].length; 
      } 

      @Override 
      public Object getGroup(int groupPosition) { 
      return null; 
      } 

      @Override 
      public int getGroupCount() { 
      return arrGroupelements.length; 
      } 

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

      @Override 
      public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 

      if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.group_row, null); 
      } 

      TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvGroupName); 
      tvGroupName.setText(arrGroupelements[groupPosition]); 

      return convertView; 
      } 

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

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

Child_row.xml 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="40dip" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" > 
<ImageView 
     android:id="@+id/tvPlayerImage" 
     android:layout_width="wrap_content" 
     android:layout_height="30dip" 
     android:gravity="center_vertical" 
     android:layout_marginLeft="50dip" 

     > 
    </ImageView> 
    <TextView 
     android:id="@+id/tvPlayerName" 
     android:layout_width="wrap_content" 
     android:layout_height="30dip" 
     android:gravity="center_vertical" 
     android:layout_marginLeft="10dip" 
     android:textSize="14sp" > 
    </TextView> 

</LinearLayout> 

group_row.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" > 

    <TextView 
     android:id="@+id/tvGroupName" 
     android:layout_width="wrap_content" 
     android:layout_height="40dip" 
     android:gravity="center_vertical" 
     android:paddingLeft="30dip" 
     android:textSize="16sp" 
     android:textStyle="bold" 
     > 
    </TextView> 

</LinearLayout> 


main.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" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

    <ExpandableListView 
     android:id="@+id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:groupIndicator="@drawable/group_indicator" > 

     <TextView 
      android:id="@+id/android:empty" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="hello" > 
     </TextView> 
    </ExpandableListView> 

</LinearLayout> 
+0

작동하지 않음 모든 OnChildClickListener에서 Java 코드 오류가 발생했습니다. –

관련 문제