编辑:不要将 ListView 放在 ExpandableView 中。这是第一个问题。第二个问题是我的图形高度始终为零。(详细查看http://developer.android.com/guide/topics/ui/how-android-draws.html)这些是问题。对于ListView 我只使用了 TextViews 并使用 for 循环将值放入其中。对于图形问题,我检查设备的像素值并设置布局参数,具体取决于它是 PORTRAIT 还是 LANDSCAPE。这是我用于它的代码。它在我将使用的设备上提供了很好的值,但可能有更好的解决方案。
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,它有两种不同类型的子视图和列表视图。我对两者都使用了两个不同的布局文件,所以我有 2 个子布局,它们是 child_list.xml 和 child_img.xml
问题是,如果我不设置布局宽度和高度,它会给出正常宽度但 0 高度来查看和正常宽度,并且列表视图的高度不够,所以我只能看到列表视图的一个元素。但是,如果我将 LayoutParameters 设置为( 1200,400)它显示所有列表视图元素。
如果不设置高度或宽度,如何显示我的所有列表元素?
主要布局
<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>
组.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;
}
}