2012-10-12 40 views
0

我有一个自定义的ExpandableListView适配器:的RelativeLayout在ExpandableListView不工作

public class MyExpandableListView extends BaseExpandableListAdapter { 

private Context context; 
private List<? extends List<? extends Map<String, ?>>> mChildData; 
private List<? extends Map<String, ?>> mGroupData; 

public MyExpandableListView(Context context,List<? extends Map<String, ?>> GroupData, 
     List<? extends List<? extends Map<String, ?>>> ChildData){ 
    this.context = context; 
    this.mChildData=ChildData; 
    this.mGroupData=GroupData; 
} 

@Override 
public Object getChild(int groupPosition, int childPosition) { 
    // TODO Auto-generated method stub 
    return mChildData.get(groupPosition).get(childPosition); 
} 

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

@Override 
public View getChildView(int groupPosition, int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

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

    TextView childtxt = (TextView) convertView.findViewById(R.id.textView1); 

    childtxt.setText((CharSequence) mChildData.get(groupPosition).get(childPosition).get("body")); 

    return convertView; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    // TODO Auto-generated method stub 
    return mChildData.get(groupPosition).size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 
    // TODO Auto-generated method stub 
    return mGroupData.get(groupPosition); 
} 

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

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

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    if(convertView==null){ 
     LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.group_list_layout, null); 
    } 

    TextView time = (TextView)convertView.findViewById(R.id.item1); 
    TextView title = (TextView)convertView.findViewById(R.id.item2); 

    time.setText((CharSequence) mGroupData.get(groupPosition).get("time")); 
    title.setText((CharSequence) mGroupData.get(groupPosition).get("titel")); 
    View view2 = (View)convertView.findViewById(R.id.view2); 
    view2.bringToFront(); 
    return convertView; 
} 

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

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

} 

我group_list_layout.xml是以下几点:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<View 
     android:id="@+id/view2" 
     android:layout_width="2dp" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="5dp" 
     android:background="#ff000000" /> 
<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="15dp" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/item1" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:text="row_id" 
     android:textSize="18sp" 
     android:width="100dip" /> 

    <TextView 
     android:id="@+id/item2" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:text="col_1" 
     android:textSize="18sp" 
     android:width="300dip" /> 
</LinearLayout> 
</RelativeLayout> 

我想显示的“时间前一条垂直线“的组元素。

但我的看法(view2)没有显示。为什么?

回答

1

您的LinearLayout定位在View元件的顶部的顶部。而是在组织布局中将View元素移动到,LinearLayout之后,以便将其放置在其上方。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="15dp" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/item1" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="row_id" 
      android:textSize="18sp" 
      android:width="100dip" /> 

     <TextView 
      android:id="@+id/item2" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:text="col_1" 
      android:textSize="18sp" 
      android:width="300dip" /> 
    </LinearLayout> 

    <View 
     android:id="@+id/view2" 
     android:layout_width="2dp" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="5dp" 
     android:background="#ff000000" /> 

</RelativeLayout> 

此外,膨胀你的布局是这样的:

convertView = infalInflater.inflate(R.layout.group_list_layout, parent, false); 
1

由于它是一个相对布局添加机器人:layout_toLeftOf = “@ ID /视图2” 到的LinearLayout应该工作。

相关问题