2013-07-16 137 views
1

我在ScrollView中有一個LinearLayout。我的佈局如下:ScrollView不滾動LinearLayout,其中包含Android中的動態內容

<?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" > 

    <TextView 
     android:id="@+id/degree_planner_step2_title" 
     style="@style/standard_text_view_appearance" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <View 
     android:id="@+id/degree_planner_divider" 
     android:layout_width="match_parent" 
     android:layout_height="2dp" 
     android:background="@color/Science" /> 

    <ScrollView 
     android:id="@+id/sv" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/Commerce" 
     android:fillViewport="true"> 

     <LinearLayout 
      android:id="@+id/degreeplanner2_lin_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

在添加我的列表視圖,如果fillViewPort是真的,那一切擴展,但是最後的ListView可見,「今年3」是在自身內滾動,而不是整個線性佈局本身。當我檢查DDMS時,它顯示滾動視圖不可滾動。如果fillViewport爲false,則LinearLayout中的所有列表視圖會縮小併成爲一個高度可滾動的項目。我試圖讓我的列表完全展開,溢出時滾動視圖應滾動linearlayout,而不是使列表視圖中的單個項目滾動。有人能夠幫助嗎?

的圖像可以在這裏找到:http://i.stack.imgur.com/frU0P.png

在上述截圖fillViewPort =假,所有列表視圖收縮到一個項目高大各自獨立地滾動時,則不存在3名列表視圖。

這是我的片段代碼,爲簡單起見我已加載我的列表視圖以字母表字母的簡單的數據,其中,所述列表視圖動態地添加的方法是notifyCourseDataSetChange():

import java.io.IOException; 
import java.util.ArrayList; 

import org.holoeverywhere.widget.Toast; 

import android.content.SharedPreferences; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.RelativeLayout; 
import android.widget.ScrollView; 
import android.widget.TextView; 

import com.actionbarsherlock.app.SherlockFragment; 
import com.example.u2.R; 
import com.example.u2.constantsandprocesses.Course; 
import com.example.u2.constantsandprocesses.DatabaseHelper; 
import com.example.u2.constantsandprocesses.DegreeYear; 
import com.example.u2.constantsandprocesses.ScienceDegreePlan; 
import com.example.u2.constantsandprocesses.UCT_Constants; 
import com.example.u2.constantsandprocesses.DegreePlan; 

public class DegreePlannerStep2Fragment extends SherlockFragment { 
    private LinearLayout baseLayout; 
    private DatabaseHelper DBHelper; 
    private String faculty; 
    private String degree; 
    private String degreeAbbrev; 
    private String major1; 
    private String major2; 
    private DegreePlan plan; 
    private TextView title; 
    private View divider; 
    private int facultyColour; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     SharedPreferences sp = getActivity().getSharedPreferences(UCT_Constants.PREFS_DEGREE, 0); 
     faculty = sp.getString("faculty", null); 
     degree = sp.getString("degree", null); 
     degreeAbbrev = sp.getString("degreeAbbrev", null); 
     major1 = sp.getString("major1", null); 
     major2 = sp.getString("major2", null); 
     setRetainInstance(true); 
     if (savedInstanceState == null){ 
      DBHelper = new DatabaseHelper(getActivity()); 
      try { 
       Log.d("DPS2", "connecting to db"); 
       DBHelper.connectDataBase(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      new GetAllCourses().execute(new String [] {major1, major2, faculty}); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.degree_planner_step_2, container, false); 
     if (faculty!=null){ 
      facultyColour = selectColour(faculty); 
      baseLayout = (LinearLayout) view.findViewById(R.id.degreeplanner2_lin_layout); 
      title = (TextView) view.findViewById(R.id.degree_planner_step2_title); 
      title.setText(degree); 
      divider = view.findViewById(R.id.degree_planner_divider); 
      divider.setBackgroundResource(facultyColour); 
     } 
     return view; 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     DBHelper.close(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     //open database connection 
     try { 
      Log.d("DPS2", "opening db"); 
      DBHelper.connectDataBase(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    /* 
    * returns the id (int) of the colour variable for the faculty 
    */ 
    private int selectColour(String facultyName) { 
     String [] faculties = getActivity().getResources().getStringArray(R.array.faculties); 
     if (facultyName.equals(faculties[0])){ 
      return R.color.Commerce; 
     }else if(facultyName.equals(faculties[1])){ 
      return R.color.EBE; 
     }else if(facultyName.equals(faculties[2])){ 
      return R.color.Health_Sciences; 
     }else if(facultyName.equals(faculties[3])){ 
      return R.color.Humanities; 
     }else if (facultyName.equals(faculties[4])){ 
      return R.color.Law; 
     }else{ 
      return R.color.Science; 
     } 
    } 

    public void notifyCourseDataSetChange(){ 
     for (DegreeYear dy : plan.getDegreeYears()){ 
      if (dy.isActive()){ 
       LayoutInflater inflater = getActivity().getLayoutInflater(); 
       View sectionHeader = inflater.inflate(R.layout.listview_section_header, null); 
       ((TextView)sectionHeader.findViewById(R.id.section_header_title)).setText("Year "+dy.getYear());  //section header title 
       sectionHeader.findViewById(R.id.section_header_divider).setBackgroundResource(facultyColour); //section header divider 
       ListView lv = new ListView(getActivity()); 
       lv.setHeaderDividersEnabled(false); 
       lv.addHeaderView(sectionHeader); 
       lv.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.drawer_list_item,new String [] {"a","b","c","d"})); 
       baseLayout.addView(lv); 
      } 
     } 
    } 

    private class GetAllCourses extends AsyncTask<String, Void, ArrayList<Course>>{ 
     String facultyName = ""; 

     @Override 
     protected ArrayList<Course> doInBackground(String... params) { 
      facultyName = params[2]; 
      return DBHelper.getCoursesForMajor(params[0], params[1]); 
     } 

     @Override 
     protected void onPostExecute(ArrayList<Course> result) { 
      super.onPostExecute(result); 
      String [] faculties = getActivity().getResources().getStringArray(R.array.faculties); 
      if (facultyName.equals(faculties[0])){   //COM 
       //TODO create corresponding degree 
      }else if(facultyName.equals(faculties[1])){  //EBE 
       //TODO create corresponding degree 
      }else if(facultyName.equals(faculties[2])){  //Health 
       //TODO create corresponding degree 
      }else if(facultyName.equals(faculties[3])){  //HUM 
       //TODO create corresponding degree 
      }else if (facultyName.equals(faculties[4])){ //LAW 
       //TODO create corresponding degree 
      }else{           //SCI 
       plan = new ScienceDegreePlan(result); 
      } 
      notifyCourseDataSetChange(); 
     } 

    } 
} 

回答

1

這裏的問題是,您正在ScrollView中添加ListView。以下是來自文檔的報價

您絕對不應該將ScrollView與ListView一起使用,因爲ListView會負責自己的垂直滾動。最重要的是,這樣做會挫敗ListView中用於處理大型列表的所有重要優化,因爲它有效地強制ListView顯示其整個項目列表以填充由ScrollView提供的無限容器。

所以你只需要添加沒有ListView的項目。

+0

我想我將不得不將它添加爲按鈕,但是如果數據集需要動態更改它將是非常成問題的。 – user1950087

+0

但是,在您當前的版本中,您僅將ListView添加爲包裝。實際上你也可以嘗試使用ListView的頁腳和頁眉,然後你會在頂部和底部有一些靜態內容,並且在列表視圖中有動態的內容。 – Desert

+0

謝謝。我決定使用AmazingListView來創建多個列表的外觀。 – user1950087