国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android解決ScrollView下嵌套ListView和GridView中內容顯示不全的問題

2019-12-12 00:24:37
字體:
來源:轉載
供稿:網友

最近為公司做的一個Demo里面用到了ScrollView嵌套了GridView和ListView,然而在嵌套的時候我發現GridView和ListView都是不能完全顯示,顯示的基本上都是單行的數據,最后查找資料和翻閱文檔看到原因是ListView和GridView的繪制過程中在ScrollView中無法準確的測量自身的高度,而且listVIew和GridView搶占了焦點,使得ListView和GrideView具有自身的顯示的效果,這樣就測量出顯示一行條目即可的距離,其他的條目根據自身的滑動顯示。

我的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="限時搶購 ,快點進入吧!"        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是可以滑動的就是顯示不全

用自己寫的方法之后才顯示出來了所有的條目 

那就不再廢話了 把我個人研究的代碼呈上

首先是關于ListView的 (注意此方法必須方到SetAdapter()方法之后執行)

這是控件的查找

list_home = (ListView) view.findViewById(R.id.list_home);list_home.setAdapter(new MyListViewAdapter(grideview_List));
 list_home.setFocusable(false);//詞句加不加像也沒有什么影響,我是加的 //setAdapter之后調用 getListViewSelfHeight(list_home);

這是getListViewSelfHeight(ListView youListview)

public void getListViewSelfHeight(ListView listView) {        // 獲取ListView對應的Adapter      ListAdapter listAdapter = listView.getAdapter();      //健壯性的判斷    if (listAdapter == null) {        return;      }     // 統計所有子項的總高度      int totalHeight = 0;      for (int i = 0, len = listAdapter.getCount(); i < len; i++) {        // listAdapter.getCount()返回數據項的數目        View listItem = listAdapter.getView(i, null, listView);        // 調用measure方法 傳0是測量默認的大小      listItem.measure(0, 0);        totalHeight += listItem.getMeasuredHeight();      }      //通過父控件進行高度的申請    ViewGroup.LayoutParams params = listView.getLayoutParams();      //listAdapter.getCount() - 1 從零開始 listView.getDividerHeight()獲取子項間分隔符占用的高度     params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));      listView.setLayoutParams(params);    }

下面是GridView的方法和ListView的測量的方法基本一樣  但是listView是單行條目的不用在擔心列的問題問GridView則是需要進行自己分行和自己分列的 所以要注意一下

        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)(這個方法能解決問題但是感覺不是很好靈活性太差 我用的獲取的列數始終獲取不到,有看神看到了 給我回復)

public void getGridViewSelfhetght(GridView gridView) {      // 獲取GridView對應的Adapter      ListAdapter adapter = gridView.getAdapter();      if (adapter == null) {        return;      }      int totalHeight = 0;      for (int i = 0, len = adapter.getCount(); i < len; i++) {        // gridView.getCount()返回數據項的數目        View listItem = adapter.getView(i, null, gridView);        // 計算子項View 的寬高        listItem.measure(0, 0);        //此處方法并不好      //5其中5是我們在Xml中的android:numColumns="5"      //FontDisplayUtil.dip2px(MyGlobalApplication.getContext(),3)設置下邊據      totalHeight += listItem.getMeasuredHeight()/5+FontDisplayUtil.dip2px(MyGlobalApplication.getContext(),3);      }      ViewGroup.LayoutParams params = gridView.getLayoutParams();     params.height = totalHeight;      gridView.setLayoutParams(params);    }

下面是FontDisplayUtil類

import android.content.Context;/** * dp、sp 、 px之間的相互轉化的工具類 */public class FontDisplayUtil { /** * 將px值轉換為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值轉換為px值,保證尺寸大小不變 */ public static int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } /** * 將px值轉換為sp值,保證文字大小不變 */ public static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); } /** * 將sp值轉換為px值,保證文字大小不變 */ public static int sp2px(Context context, float spValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); }}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。如果你想了解更多相關內容請查看下面相關鏈接

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 威海市| 泾源县| 西宁市| 寻甸| 平塘县| 玉林市| 新竹县| 新丰县| 仙居县| 丽水市| 利辛县| 鱼台县| 河间市| 廊坊市| 郸城县| 汶上县| 安吉县| 沂水县| 金溪县| 东乡县| 全椒县| 六枝特区| 漾濞| 新龙县| 多伦县| 汝阳县| 黑山县| 保山市| 股票| 镇平县| 白城市| 吉水县| 秀山| 红原县| 亳州市| 徐闻县| 兴隆县| 遵化市| 新和县| 德惠市| 襄垣县|