Android中有很多滑动的控件,但是他们之间存在着滑动冲突的问题,
下面我就为大家推荐两种方法来解其中ListView和ExpandableListView
的冲突的问题:
1、ListView和ScrollView冲突问题
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
把上面的方法粘贴到类中 写到添加适配器的下面:
listView.setAdapter(adapter2);
setListViewHeightBasedOnChildren(lv);
这个控件设计到双层下拉框 这就需要用到一个工具类,把这个工具类沾到你的java包中
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;
public class MyExpandableListView extends ExpandableListView {
public MyExpandableListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyExpandableListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
XML布局中:
<com.beicai.zhongji_programe.tools.MyExpandableListView
android:id="@+id/MyExpandableListView1"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</com.beicai.zhongji_programe.tools.MyExpandableListView>