當ScrollView 嵌套 ListView時,會引起衝突,爲解決這個衝突以下:html
新建一個自定義View,集成ListView,重寫OnMeasure方法,從新計算ListView的高度,不過這樣的話會致使ListView沒法滑動,還好有ScrollView,能夠彌補這一缺點;java
我建立一個HempListView的自定義類android
代碼以下:ide
/** * 做者:jaume * <p/> * 建立於:2016/9/8 * <p/> * 描述:解決ScrollView與ListView之間的衝突 */ public class HempListView extends ListView { private Paint paint; public HempListView(Context context, AttributeSet attrs) { super(context, attrs); } //重寫該方法後,致使listView沒法滑動 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec( Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
XML代碼:佈局
中間是我在後來加的註釋,你能夠刪掉,直接使用優化
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <!-- ScrollView中只能放一個控件,我選擇放一個佈局控件,這樣能夠在佈局中添加本身想添加的控件 --!> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 我定義的一個ViewPager,總佈局是上面ViewPager,下面ListView顯示 --!> <android.support.v4.view.ViewPager android:id="@+id/mViewPager" android:layout_width="fill_parent" android:layout_height="150dp" /> <!-- 這個就是自定義的ListView,將他調出來使用 --!> <com.test.bwie.jamlive.view.HempListView android:id="@+id/mListView_Remeng" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fadingEdgeLength="0dp" android:padding="10dp" android:scrollbars="none"></com.test.bwie.jamlive.view.HempListView> </LinearLayout> </ScrollView>
在Java代碼中調用:code
//獲取控件 private HempListView mHempListView; //初始化控件 mHempListView = (HempListView) view.findViewById(R.id.mHempListView); //配置優化 mHempListView.setAdapter(new MyBaseAdapter()); //new MyBaseAdapter() 這個是優化的類
代碼不是很全,能夠相應的複製拿來用,這樣就能夠解決ScrollView與ListView的嵌套問題了。htm
忽然想到,能夠用如下方法來模仿上拉加載,下拉刷新,能夠參考看下,有什麼不足但願告知get
// 建立一個header 頭部,想要建立一個什麼類型的就建立一個 TextView header = new TextView(getActivity()); header.setText("刷新"); header.setTextColor(Color.WHITE); header.setBackgroundColor(Color.RED); header.setGravity(Gravity.CENTER); header.setPadding(15, 15, 15, 15); // 建立一個header TextView footer= new TextView(getActivity()); footer.setText("加載更多"); footer.setTextColor(Color.WHITE); footer.setBackgroundColor(Color.RED); footer.setGravity(Gravity.CENTER); footer.setPadding(15, 15, 15, 15); //以後添加到listView中 listView.addHeaderView(header); //添加結尾的 listView.addFooterView(footer); /*************最後才能配置Adapter,不然出錯****/ listView.setAdapter(new MyPagerAdapteer); //運行,就會顯示效果