编辑:不要将ListView放在ExpandableView中。这是第一个问题。第二个问题是我的图表的高度总是为零。(详见http://developer.android.com/guide/topics/ui/how-android-draws.html) 这些是问题。对于ListView,我只使用TextViews并使用for循环将值放入其中。 对于图形问题,我检查设备的像素值和setLayoutParameters,具体取决于它是PORTRAIT还是LANDSCAPE.Here是我用于它的代码。它给出了我将使用的设备的良好值,但可能有更好的解决方案。
int orientation = getResources().getConfiguration().orientation;
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height=metrics.heightPixels;
int width = metrics.widthPixels;
if (orientation == 1) {
SGV.setLayoutParams(new LayoutParams(width, height/4));
} else {
SGV.setLayoutParams(new LayoutParams(width, height/2));
}
我正在实现一个ExpandableListView,它有两个不同类型的子节点,它们是一个视图和一个listview。我使用两个不同的布局文件,所以我有2个子布局,它们是child_list.xml和child_img.xml
问题在于,如果我没有设置布局宽度和高度,它会给出正常的宽度,但是要查看高度和正常宽度,并且列表视图的高度不够,所以我只能看到listview的一个元素。但是如果我设置了LayoutParameters为(1200,400)它显示所有listview元素。
如果没有设置高度或宽度,我如何显示所有列表元素?
主要布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/distance_fragment_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/expandableList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="@null" >
</ExpandableListView>
</LinearLayout>
Group.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/groupName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="50sp" />
</LinearLayout>
children_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/child_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
children_list_txt
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/distance_list_home"
android:layout_width="200dp"
android:layout_height="50dp"
android:gravity="right|center_vertical"
android:textColor="#ffffff"
android:textSize="18sp" />
<TextView
android:id="@+id/distance_list_away"
android:layout_width="200dp"
android:layout_height="50dp"
android:gravity="left|center_vertical"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
MyExpandableListViewAdapter
public class ExpandableListItems extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> mParent;
public ExpandableListItems(Context context, ArrayList<String> parent) {
mParent = parent;
this.context = context;
}
@Override
public int getGroupCount() {
return mParent.size();
}
@Override
public int getChildrenCount(int i) {
return 1;
}
@Override
public String getGroup(int i) {
return mParent.get(i);
}
@Override
public Object getChild(int i, int i1) {
return mParent.get(i);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
super.registerDataSetObserver(observer);
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(groupPosition == 1){
convertView = inflater.inflate(R.layout.distance_child_img, null);
ViewGroup graphArea = (ViewGroup)convertView.findViewById(R.id.testw);
SpeedGraphView SGV = new SpeedGraphView(getActivity().getBaseContext());
SGV.setArrays(homefirstHalfSpeedArray,homesecondHalfSpeedArray,Global.app.match.homeTeam.getTeamName());
SGV.setPlayerArrays(awayfirstHalfSpeedArray,awaysecondHalfSpeedArray,Global.app.match.awayTeam.getTeamName());
SGV.setLayoutParams(new LayoutParams(1200, 400));
graphArea.addView(SGV);
}else{
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.distance_child_list, null);
ListView listV = (ListView) convertView.findViewById(R.id.child_listView);
ListAdapter adp = new ListAdapter(getActivity());
adp.add(new ListItem("Deneme", "Away", 200, 100));
adp.add(new ListItem("asdas", "Away", 200, 100));
listV.setAdapter(adp);
// listV.setLayoutParams(new LinearLayout.LayoutParams(1200,500));
}
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.distance_group, null);
TextView tx = (TextView)convertView.findViewById(R.id.groupName);
tx.setText(ExpListItems.get(groupPosition));
return convertView;
}
}