我在ScrollView中有一个LinearLayout。我的布局如下:
<?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>
添加我的ListViews后,如果fillViewPort为true,它会扩展所有内容,但最后一个列表视图可见,&#39;第3年&#39;可以在自身内滚动,而不是整个线性布局本身。当我检查DDMS时,它显示滚动视图不可滚动。如果fillViewport为false,则LinearLayout中的所有列表视图都会缩小并成为一个高且可滚动的项目。我试图让我的列表完全展开,并且在溢出时,scrollview应滚动linearlayout而不是使列表视图中的各个项目滚动。有人能帮忙吗?
可在此处找到图片:http://i.stack.imgur.com/frU0P.png
在上面的屏幕截图中,fillViewPort = false,所有列表视图缩小为一个项目高,每个列表都可单独滚动,有3个列表视图。
这里是我的片段代码,为了简单起见,我已经用简单的字母数字加载了我的listview,动态添加listViews的方法是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();
}
}
}
答案 0 :(得分:1)
这里的问题是您在ScrollView中添加ListView。这是文档中的引用
你永远不应该使用带有ListView的ScrollView,因为ListView负责自己的垂直滚动。最重要的是,这样做会使ListView中的所有重要优化都无法处理大型列表,因为它有效地强制ListView显示其整个项目列表以填充ScrollView提供的无限容器。
因此,您只需添加没有ListView的项目。
答案 1 :(得分:0)
您可以改用RecyclerView
。请注意,如果您确实使用它,则必须对适配器进行更改,因为RecyclerView
需要RecyclerView.Adapter
作为适配器。
它滚动并感觉就像ListView一样。
编辑:我忘了提及你需要确保在notifyDataSetChanged()
运行时不要运行onBindViewHolder
。它一直在循环中运行。或者您可以使用notifyItemInserted(int position)
和notifyItemRemoved(int position)
通知适配器刷新它。
RecyclerView和notifyItemInserted。