可扩展列表视图,垂直子项

时间:2012-04-18 04:29:03

标签: java android expandablelistview

我制作了一个可扩展的列表,例如此链接中的列表:http://www.techienjoy.com/android-expandable-list-dynamically-created-example.php,但我想让孩子看到垂直而不是水平。

我只有1 xml:

<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"/>

其他一切都在课堂上。如何将它们设置为垂直? 我的java代码与链接中的代码相同。

1 个答案:

答案 0 :(得分: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>