改变listview的子组件的visibility的属性后,通过listview获取子组件,却发现其子组件的属性仍然不变,为什么?

carlos 发布于 2013/11/12 19:17
阅读 798
收藏 0

//@Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
   // TODO Auto-generated method stub
   Log.d("onItemClick", "onItemClick  arg0= "+arg0);
   Log.d("onItemClick", "onItemClick  arg1= "+arg1);
   Log.d("onItemClick", "onItemClick  arg2= "+arg2);
   Log.d("onItemClick", "onItemClick  arg3= "+arg3);
   
   userGuider = (LinearLayout)arg1.findViewById(R.id.UserGuider);    
   
   if(userGuider.getVisibility()!=View.VISIBLE)
   {
    userGuider.setVisibility(View.VISIBLE);
   }else{
    userGuider.setVisibility(View.GONE);   
   } 
   
   Log.d("onItemClick", "onItemClick "+arg1.getMeasuredHeight());
   arg1.requestLayout(); 
   
   setHeightBasedOnChildTest((ListView)arg1.getParent());
  } 

在 setHeightBasedOnChildTest中设置listview的高度,因为无法获取listview子组件userGuider的visibility属性,获取到的总是xml文件中的属性,所以listview的高度总是不正确,有人知道为什么吗?

 

public  void setHeightBasedOnChildTest(ListView listView) {
   if(listView == null)
    return;
   
   int totalHeight = 0;
   Adapter adapter = listView.getAdapter();
   for (int i = 0; i < adapter.getCount(); i++) {   
    View listItem = adapter.getView(i, null, listView);   
    Log.d("onItemClick", "onItemClick listItem.findViewById(R.id.UserGuider).getVisibility()"+listItem.findViewById(R.id.UserGuider).getVisibility());
    listItem.measure(

                     MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),

                     MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

    totalHeight += listItem.getMeasuredHeight();   
   }   

   ViewGroup.LayoutParams params = listView.getLayoutParams();   
   params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
   //Logs.d("onItemClick", "onItemClick   params.height"+params.height);
   //Logs.d("onItemClick", "onItemClick   adapter.getCount()"+adapter.getCount());
   listView.setLayoutParams(params);
  }

xml文件如下:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"  
    android:descendantFocusability="blocksDescendants"    
    >
   
    <LinearLayout
        android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
        >
     <ImageView
         android:id="@+id/img"
         android:layout_width="36dip"
         android:layout_height="36dip"
         android:layout_marginLeft="5dp" 
         android:layout_marginRight="10dp" 
         android:layout_marginTop="1dp"
         android:layout_marginBottom="1dp"
         android:layout_gravity="center_vertical"    
         />
  <LinearLayout
      android:orientation="vertical"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:layout_gravity="center_vertical"
      android:layout_weight="1.0"  
      >
     <TextView android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FF000000"
    android:textSize="18sp" />
   <TextView android:id="@+id/info"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FF000000"   
    android:textSize="13sp" />    
  </LinearLayout>
 
  <LinearLayout  
      android:orientation="horizontal"   
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:layout_gravity="center_vertical"
      android:layout_marginRight="10dp"    
      >
   <TextView android:id="@+id/text"
       android:text=">"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FF000000"
    android:textSize="18sp" />    
  </LinearLayout> 
    </LinearLayout>
    
    <LinearLayout
        android:id="@+id/UserGuider"
        android:orientation="vertical"   
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:layout_gravity="center_vertical"
     android:layout_margin="10dp"
     android:visibility="gone"
        >
        <TextView android:id="@+id/textGuide"
       android:text="@string/help_tip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FF000000"
    android:textSize="20sp" /> 
  <TextView android:id="@+id/textGuide1"       
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FF000000"
    android:textSize="20sp" />
       
    </LinearLayout>

</LinearLayout>

效果图如下:

 

 

加载中
0
carlos
carlos

    View listItem = adapter.getView(i, null, listView);

问题就在第二个参数,不能是null,应该是listView.getChildAt(i)

这样就解决此问题了。已经解决很久了,现在才想起来,就回复下

0
carlos
carlos

引用来自“carlos”的评论

    View listItem = adapter.getView(i, null, listView);

问题就在第二个参数,不能是null,应该是listView.getChildAt(i)

这样就解决此问题了。已经解决很久了,现在才想起来,就回复下

 View listItem = adapter.getView(i, null, listView);

问题就在第二个参数,不能是null,应该是listView.getChildAt(i)

这样就解决此问题了。已经解决很久了,现在才想起来,就回复下

0
yzsunlight
yzsunlight
原因是什么 知道吗?
返回顶部
顶部