標籤:
很多時候我們需要使用listview與嵌套使用,那麼問題來了,listview裡面內建了
捲軸,而又嵌套在scrollview裡面。所以引發了一系列的衝突。listview顯示不全。
今天給出一個全新的上下拉重新整理與解決衝突的方案。
。點擊下載下拉重新整理上拉載入工具
下載完工具後我們複製粘貼到我們自己的工程裡面,使用方式和自訂view一樣。
下面貼出代碼
<framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/topLin"> <com example="" androidtool="" viewtool="" pulltorefreshview_all="" android:id="@+id/PullToRefreshView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F4F4F4" android:orientation="vertical"> <scrollview android:id="@+id/scrollview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F4F4F4"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 廣告欄目和描點 --> <framelayout android:id="@+id/framelayout" android:layout_width="fill_parent" android:layout_height="120dp" android:orientation="vertical"> <com example="" ysh="" view="" bannerview="" viewflow="" android:id="@+id/viewflow" android:layout_width="fill_parent" android:layout_height="110dp"> <framelayout android:layout_width="fill_parent" android:layout_height="35dp" android:layout_gravity="bottom" android:background="@color/transparent" android:gravity="center"> <com example="" ysh="" view="" bannerview="" circleflowindicator="" android:id="@+id/viewflowindic" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_gravity="center_horizontal|bottom" android:padding="1dip" app:activecolor="#ff0000" app:activetype="fill" app:circleseparation="20dip" app:inactivecolor="#ffffff" app:inactivetype="fill" app:radius="4dip"> </com></framelayout> </com></framelayout> <!-- 橫向的中間的四個按鈕 --> <linearlayout android:id="@+id/centerLayout" android:layout_width="match_parent" android:layout_height="68dp" android:layout_below="@+id/framelayout" android:background="#FFFFFF" android:orientation="horizontal"> <linearlayout android:id="@+id/mFindClinic" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:onclick="opeSelect" android:orientation="vertical"> <imageview android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/mfind1"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="***" android:textalignment="center" android:textcolor="#333333" android:textsize="15sp"> </textview></imageview></linearlayout> <linearlayout android:id="@+id/mFindDoctor" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:onclick="opeSelect" android:orientation="vertical"> <imageview android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/mfind2"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="***" android:textalignment="center" android:textcolor="#333333" android:textsize="15sp"> </textview></imageview></linearlayout> <linearlayout android:id="@+id/mAskDoctor" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:onclick="opeSelect" android:orientation="vertical"> <imageview android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/mfind3"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="***" android:textalignment="center" android:textcolor="#333333" android:textsize="15sp"> </textview></imageview></linearlayout> <linearlayout android:id="@+id/mCommunity" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:onclick="opeSelect" android:orientation="vertical"> <imageview android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/mfind4"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="社區" android:textalignment="center" android:textcolor="#333333" android:textsize="15sp"> </textview></imageview></linearlayout> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:layout_margintop="5dp"> <listview android:id="@+id/main_list" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none"> </listview> </linearlayout> </linearlayout> </scrollview> <progressbar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="gone"> </progressbar></com></framelayout>
看到代碼貼如也就這麼回事。但是我們注意了,這個時候的listview是包裹在scroll裡面的。那麼滾動起來肯定不行的...接下來解決這一個衝突問題。
如下代碼
//拿下適配器以及設定適配器 adpater = new Main_goos_adapter(this, slist);// 載入適配器main_list.setAdapter(adpater);// 設定適配器setListViewHeight(main_list);// 重設一下位置及高度//核心的自訂設定高度的方法/** * 動態設定ListView的高度 * * @param listView * 直接扔個listview 放裡面去,是不是感覺很簡單呢 */public static void setListViewHeight(ListView listView) {if (listView == null)return;ListAdapter listAdapter = listView.getAdapter();//擷取listview的適配器if (listAdapter == null) {return;}int totalHeight = 0;//定義高度for (int i = 0; i < listAdapter.getCount(); 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);//重繪高度} //看完後煥然大悟啊
那麼問題由來了,咱的上下拉重新整理砸來的啊。接下看代碼
private PullToRefreshView_all mPullToRefreshView;// 下拉重新整理框 先聲明再拿值 //當前activity實現它的兩個介面如:OnHeaderRefreshListener, OnFooterRefreshListener mPullToRefreshView = (PullToRefreshView_all) findViewById(R.id.PullToRefreshView); mPullToRefreshView.setOnHeaderRefreshListener(this);// 下拉載入mPullToRefreshView.setOnFooterRefreshListener(this);// 上拉重新整理 //再實現它的兩個方法,俺們的下拉的就可以玩起來了 /** * 下拉重新整理 */@Overridepublic void onHeaderRefresh(PullToRefreshView_all view) {doSomething();} /** * 上拉載入 */@Overridepublic void onFooterRefresh(PullToRefreshView_all view) {}</textarea >
那麼完事大吉了,一起學習一起分享!!!
android解決listview與scrollview的衝突、自訂listview的高度以及上下拉重新整理