Android 는 ScrollView 에 포 함 된 ListView 와 GridView 의 내용 이 불완전 하 게 표 시 된 문 제 를 해결 합 니 다.

최근 에 회 사 를 위해 만 든 데모 에는 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);
getListView SelfHeight(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);
다음은 getGridView SelfHeight(GridView you GrideView)입 니 다.

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);
 }
}
총결산
이상 은 이 글 의 모든 내용 입 니 다.본 고의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 참고 학습 가 치 를 가지 기 를 바 랍 니 다.여러분 의 저희 에 대한 지지 에 감 사 드 립 니 다.더 많은 내용 을 알 고 싶다 면 아래 링크 를 보 세 요.

좋은 웹페이지 즐겨찾기