0

我有一個帶有標題面板和列表視圖的佈局。從其父視圖組中刪除視圖時禁止在列表視圖中重新加載項目

這是佈局。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:id="@+id/mainFragmentLayout"> 

<RelativeLayout 
     android:id="@+id/header" 
     android:background="@drawable/upper_panel" 
     android:layout_height="40dp" 
     android:layout_width="fill_parent"> 

    <TextView android:id="@+id/news_label" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_centerVertical="true" 
       android:gravity="left" 
       android:text="@string/news_label" 
       android:textSize="15sp" 
       android:textStyle="bold" 
       android:layout_marginLeft="54dp" 
       android:textColor="@color/white" 
      /> 

    <Button android:id="@+id/refreshButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/btm_refresh" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="8dp" 
      /> 
</RelativeLayout> 

<ListView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="120" 
     android:id="@+id/feedMeMore" 
     android:fastScrollEnabled="false" 
     android:layout_gravity="center"/> 

在onTouchListener予刪除和添加RelativeLayot報頭。

public boolean onTouch(View view, MotionEvent motionEvent) { 

int action=motionEvent.getAction(); 
switch (action) { 
    case MotionEvent.ACTION_DOWN: 
     startY = motionEvent.getY(); 
     break; 
    case MotionEvent.ACTION_MOVE: 
     float nowY = motionEvent.getY(); 
     if (startY - nowY > 10) 
     { 
      if (mainLayout.indexOfChild(headerView) >= 0) 
       mainLayout.removeView(headerView); 
     } 
     else if (startY - nowY < - 100) 
     { 
        if (mainLayout.indexOfChild(headerView) < 0) 
        mainLayout.addView(headerView, 0);    
     } 
    } 
    break; 
} 
return false; 
} 

當標題被刪除時,listview中的所有顯示項都被重新加載。 我有一個圖像和文本,這是從網站加載,每次它重新加載文本和圖像。

如何關閉此重新加載?

回答

1

根據我的說法,這是因爲您的headerView在頂部(即使在linearlayout上)和列表視圖在它的下方對齊。 所以,當你刪除你的標題視圖時,它會使listview改變它的高度,所以所有的視圖都被重繪。

作爲一種解決方案,您可以在headerview背後使用虛擬視圖(需要將父視圖更改爲RelativeLayout),其高度與原始標題視圖相同,現在您的列表視圖將位於虛擬標題視圖之下,不是原始的標題視圖。

就是這樣。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/mainFragmentLayout"> 

<RelativeLayout 
    android:id="@+id/header" 
    android:background="@drawable/upper_panel" 
    android:layout_height="40dp" 
    android:layout_alignParentTop="true" 
    android:layout_width="fill_parent"> 

<TextView android:id="@+id/news_label" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_centerVertical="true" 
      android:gravity="left" 
      android:text="@string/news_label" 
      android:textSize="15sp" 
      android:textStyle="bold" 
      android:layout_marginLeft="54dp" 
      android:textColor="@color/white" 
     /> 

<Button android:id="@+id/refreshButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/btm_refresh" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="8dp" 
     /> 
</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/dummyHeader" 
    android:background="@drawable/upper_panel" 
    android:layout_height="40dp" 
    android:layout_alignParentTop="true" 
    android:layout_width="fill_parent"/> 

<ListView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="120" 
     android:id="@+id/feedMeMore" 
     android:fastScrollEnabled="false" 
     android:layout_gravity="center"/> 
+0

謝謝。但是當我刪除標題時,我需要將listview擴展到全屏。 – TpoM6oH

相關問題