Android解決ScrollView下嵌套ListView和GridView中內(nèi)容顯示不全的問(wèn)題
最近為公司做的一個(gè)Demo里面用到了ScrollView嵌套了GridView和ListView,然而在嵌套的時(shí)候我發(fā)現(xiàn)GridView和ListView都是不能完全顯示,顯示的基本上都是單行的數(shù)據(jù),最后查找資料和翻閱文檔看到原因是ListView和GridView的繪制過(guò)程中在ScrollView中無(wú)法準(zhǔn)確的測(cè)量自身的高度,而且listVIew和GridView搶占了焦點(diǎn),使得ListView和GrideView具有自身的顯示的效果,這樣就測(cè)量出顯示一行條目即可的距離,其他的條目根據(jù)自身的滑動(dòng)顯示。
我的XMl的部分代碼如下:
<ScrollView
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:scrollbars="none"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#23000000"
android:orientation="vertical"
android:paddingTop="-1dp" >
<android.support.v4.view.ViewPager
android:id="@+id/home_pager"
android:layout_width="fill_parent"
android:layout_height="100dp" >
</android.support.v4.view.ViewPager>
<GridView
android:id="@+id/gv_home"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="5"
android:paddingBottom="10dp"
android:paddingTop="13dp" >
</GridView>
<LinearLayout
android:id="@+id/ll_clickin"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/shape_home"
android:orientation="horizontal" >
<ImageView
android:layout_width="85dp"
android:layout_height="wrap_content"
android:src="@drawable/click_in" />
<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:singleLine="true"
android:text="限時(shí)搶購(gòu) ,快點(diǎn)進(jìn)入吧!"
android:textSize="18sp" />
</LinearLayout>
<ListView
android:id="@+id/list_home"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
</ListView>
</LinearLayout>
</ScrollView>
顯示的效果是這樣的其中的Listview和GridView是可以滑動(dòng)的就是顯示不全

用自己寫(xiě)的方法之后才顯示出來(lái)了所有的條目

那就不再?gòu)U話(huà)了 把我個(gè)人研究的代碼呈上
首先是關(guān)于ListView的 (注意此方法必須方到SetAdapter()方法之后執(zhí)行)
這是控件的查找
list_home = (ListView) view.findViewById(R.id.list_home); list_home.setAdapter(new MyListViewAdapter(grideview_List));
list_home.setFocusable(false);//詞句加不加像也沒(méi)有什么影響,我是加的 //setAdapter之后調(diào)用 getListViewSelfHeight(list_home);
這是getListViewSelfHeight(ListView youListview)
public void getListViewSelfHeight(ListView listView) {
// 獲取ListView對(duì)應(yīng)的Adapter
ListAdapter listAdapter = listView.getAdapter();
//健壯性的判斷
if (listAdapter == null) {
return;
}
// 統(tǒng)計(jì)所有子項(xiàng)的總高度
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
// listAdapter.getCount()返回?cái)?shù)據(jù)項(xiàng)的數(shù)目
View listItem = listAdapter.getView(i, null, listView);
// 調(diào)用measure方法 傳0是測(cè)量默認(rèn)的大小
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//通過(guò)父控件進(jìn)行高度的申請(qǐng)
ViewGroup.LayoutParams params = listView.getLayoutParams();
//listAdapter.getCount() - 1 從零開(kāi)始 listView.getDividerHeight()獲取子項(xiàng)間分隔符占用的高度
params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
下面是GridView的方法和ListView的測(cè)量的方法基本一樣 但是listView是單行條目的不用在擔(dān)心列的問(wèn)題問(wèn)GridView則是需要進(jìn)行自己分行和自己分列的 所以要注意一下
gv_home = (GridView) view.findViewById(R.id.gv_home);
gv_home.setSelector(new ColorDrawable(Color.TRANSPARENT));
home_pager.setFocusable(false);
gv_home.setAdapter(new MyGrigeViewAdapter(grideview_List));
getGridViewSelfHeight(gv_home);
下面是getGridViewSelfHeight(GridView youGrideView)(這個(gè)方法能解決問(wèn)題但是感覺(jué)不是很好靈活性太差 我用的獲取的列數(shù)始終獲取不到,有看神看到了 給我回復(fù))
public void getGridViewSelfhetght(GridView gridView) {
// 獲取GridView對(duì)應(yīng)的Adapter
ListAdapter adapter = gridView.getAdapter();
if (adapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0, len = adapter.getCount(); i < len; i++) {
// gridView.getCount()返回?cái)?shù)據(jù)項(xiàng)的數(shù)目
View listItem = adapter.getView(i, null, gridView);
// 計(jì)算子項(xiàng)View 的寬高
listItem.measure(0, 0);
//此處方法并不好
//5其中5是我們?cè)赬ml中的android:numColumns="5"
//FontDisplayUtil.dip2px(MyGlobalApplication.getContext(),3)設(shè)置下邊據(jù)
totalHeight += listItem.getMeasuredHeight()/5+FontDisplayUtil.dip2px(MyGlobalApplication.getContext(),3);
}
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight;
gridView.setLayoutParams(params);
}
下面是FontDisplayUtil類(lèi)
import android.content.Context;
/**
* dp、sp 、 px之間的相互轉(zhuǎn)化的工具類(lèi)
*/
public class FontDisplayUtil {
/**
* 將px值轉(zhuǎn)換為dip或dp值,保證尺寸大小不變
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 將dip或dp值轉(zhuǎn)換為px值,保證尺寸大小不變
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
/**
* 將px值轉(zhuǎn)換為sp值,保證文字大小不變
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 將sp值轉(zhuǎn)換為px值,保證文字大小不變
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Android多線(xiàn)程斷點(diǎn)續(xù)傳下載示例詳解
這篇文章主要為大家詳細(xì)介紹了Android多線(xiàn)程斷點(diǎn)續(xù)傳下載示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android MQTT與WebSocket協(xié)議詳細(xì)講解
MQTT(消息隊(duì)列遙測(cè)傳輸)是ISO 標(biāo)準(zhǔn)(ISO/IEC PRF 20922)下基于發(fā)布/訂閱范式的消息協(xié)議。它工作在TCP/IP協(xié)議族上,是為硬件性能低下的遠(yuǎn)程設(shè)備以及網(wǎng)絡(luò)狀況糟糕的情況下而設(shè)計(jì)的發(fā)布/訂閱型消息協(xié)議2022-11-11
Android實(shí)現(xiàn)自定義圓形進(jìn)度條
這篇文章主要介紹了Android自定義圓形進(jìn)度條實(shí)現(xiàn)代碼,進(jìn)度條在Android中教程經(jīng)常使用到,本文向大家分享了Android實(shí)現(xiàn)自定義圓形進(jìn)度條的代碼,感興趣的小伙伴們可以參考一下2016-03-03
Android打包上傳AAR文件到Maven倉(cāng)庫(kù)的示例
這篇文章主要介紹了Android打包上傳AAR文件到Maven倉(cāng)庫(kù)的示例,幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下2021-03-03
Android Service開(kāi)發(fā)應(yīng)用實(shí)例
Android的服務(wù)是開(kāi)發(fā)Android應(yīng)用程序的重要組成部分。不同于活動(dòng)Activity,服務(wù)是在后臺(tái)運(yùn)行,服務(wù)沒(méi)有接口,生命周期也與活動(dòng)Activity非常不同。通過(guò)使用服務(wù)我們可以實(shí)現(xiàn)一些后臺(tái)操作,比如想從遠(yuǎn)程服務(wù)器加載一個(gè)網(wǎng)頁(yè)等,下面來(lái)看看詳細(xì)內(nèi)容,需要的朋友可以參考下2022-12-12
android編程判斷應(yīng)用是否具有某個(gè)權(quán)限的方法
這篇文章主要介紹了android編程判斷應(yīng)用是否具有某個(gè)權(quán)限的方法,涉及Android進(jìn)程操作及權(quán)限控制的相關(guān)使用技巧,需要的朋友可以參考下2015-10-10

