ListView涵盖了它下面的所有内容

时间:2012-06-15 12:02:12

标签: android android-layout android-listview expandablelistview

我正在实现一个类,我在ListView中列出了一个国家/地区的语言。在下面,我有一个ExpandableListView,其中包含主要语言在组标题中,以及该语言的所有版本作为其子项。

目前正确填充数据,但列表视图显示在分隔符和ExpandableListView上。我希望用户能够滚动浏览两个列表中的所有项目,而不必静态定义两个列表高度。目前,当ListView中有更多项目,然后在一个屏幕上显示它只显示列表视图及其上方的内容并覆盖ExpandableListView。

language_list.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


 <EditText android:id="@+id/search_box" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="type to filter list" 
    android:inputType="text"
    android:layout_alignParentTop = "true"
    android:maxLines="1"/>

<TextView 
    android:id="@+id/languages" 
    android:text="Locatoin Languages" 
    android:background="#fff"
    android:textColor="#000"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" 
    android:layout_below="@id/search_box"
    android:textAppearance="?android:attr/textAppearanceSmall">
</TextView>

<ListView 
    android:id="@+id/expandableLanguage" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/languages"
    android:drawSelectorOnTop="false">
</ListView>

<TextView 
    android:id="@+id/majorLanguages" 
    android:text="Major Languages" 
    android:background="#fff"
    android:textColor="#000"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" 
    android:layout_below="@id/expandableLanguage"
    android:textAppearance="?android:attr/textAppearanceSmall">
</TextView>

<ExpandableListView 
    android:id="@+id/expandableMajorLanguage" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/majorLanguages">
</ExpandableListView>

<requestFocus></requestFocus>
</LinearLayout>

LanguageList.Java

public class LanguageList extends Activity {
private static LocalSQLiteHelper dbHelper;
private static SQLiteDatabase db;
private static String BASE_LANGUAGE_SELECTOR = "";
private static String GET_MAJOR_LANGUAGES_BY_MACRO = "";
private static String GET_MAJOR_LANGUAGES_BY_ISO = "";
private static String GET_MAJOR_LANGUAGES_TITLES = "";

ExpandableListAdapter adapterExpandable;
ListAdapter adapterList;

String locationName, locationID, languageName;
private ArrayList<LanguageDataType> LocationLanguages = new ArrayList<LanguageDataType>();
private ArrayList<LanguageDataType> majorLanguagesTitles = new ArrayList<LanguageDataType>();
private ArrayList<ArrayList<LanguageDataType>> majorLanguages = new ArrayList<ArrayList<LanguageDataType>>();
private ExpandableListView languageLV ; 
private ListView locationLV;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.language_list);
    setTitle("Select Languages");
    dbHelper = new LocalSQLiteHelper(this.getBaseContext());
    db = dbHelper.getReadableDatabase();

    Bundle locationData = getIntent().getExtras();

    locationName = locationData.getString("locationName");
    locationID = locationData.getString("locationID");


    getLocationData();
    getMajorLanguageData();




    languageLV=(ExpandableListView) findViewById(R.id.expandableMajorLanguage);
    languageLV.getAdapter();
    adapterExpandable=new LanguageExpandableAdapter(this, majorLanguagesTitles, majorLanguages);
    languageLV.setAdapter(adapterExpandable);


    locationLV= (ListView) findViewById(R.id.expandableLanguage);
    locationLV.getAdapter();
    adapterList=new LanguageListAdapter(this, LocationLanguages);
    locationLV.setAdapter( adapterList);

}

private void getLocationData(){
    //locationTitle.clear();
    TextView text = (TextView) findViewById(R.id.languages);
    text.setText("Languages spoken in " +locationName);

    LocationLanguages.clear();
    //locationTitle.add(new LanguageDataType("Languages spoken in " +locationName, null, null));
    Cursor row = null;
    row = db.rawQuery(BASE_LANGUAGE_SELECTOR + locationID + "';", null);

    if((row.getCount() > 0) && (!row.isClosed())){
        row.moveToFirst();
        do{ 
            LocationLanguages.add(new LanguageDataType(row.getString(0), row.getString(1),row.getString(2)));
        }while(row.moveToNext());
        row.close();            
    } 
}



private void getMajorLanguageData(){
    majorLanguagesTitles.clear();
    majorLanguages.clear();

    Cursor row = null;
    row = db.rawQuery(GET_MAJOR_LANGUAGES_TITLES, null);

    if((row.getCount() > 0) && (!row.isClosed())){
        row.moveToFirst();
        do{ 
            majorLanguagesTitles.add(new LanguageDataType(row.getString(0), row.getString(1),row.getString(2)));

            if(row.getString(2)==null){
                queryLanguages(GET_MAJOR_LANGUAGES_BY_ISO + row.getString(1) + "';", majorLanguages);
            } else {
                queryLanguages(GET_MAJOR_LANGUAGES_BY_MACRO + row.getString(2) + "';", majorLanguages);
            }           
        }while(row.moveToNext());
        row.close();            
    } 
}

private void queryLanguages(String sql,ArrayList<ArrayList<LanguageDataType>> inputList ) {     

    Cursor row = null;
    row = db.rawQuery(sql, null);
    ArrayList<LanguageDataType> languageList = new ArrayList<LanguageDataType>();

    if((row.getCount() > 0) && (!row.isClosed())){
        row.moveToFirst();
        do{ 
            languageList.add(new LanguageDataType(row.getString(0), row.getString(1),row.getString(2)));
        }while(row.moveToNext());
        row.close();            
    } 
    inputList.add(languageList);
}

我有两个适配器用于ListView,另一个适用于ExpandableListView

LanguageExpandableAdapter.java

public class LanguageExpandableAdapter extends BaseExpandableListAdapter{
Context mContext;
private ArrayList<LanguageDataType> titles = new ArrayList<LanguageDataType>();
private ArrayList<ArrayList<LanguageDataType>> languages = new ArrayList<ArrayList<LanguageDataType>>();


public LanguageExpandableAdapter(Context context, ArrayList<LanguageDataType> inputTitles, ArrayList<ArrayList<LanguageDataType>> inputLanguages) {
    mContext = context;

    for (LanguageDataType value: inputTitles) {
        titles.add(value);
    }

    for (ArrayList<LanguageDataType> value: inputLanguages) {
        languages.add(value);
    }

}

public Object getChild(int groupPosition, int childPosition) {
    return languages.get(groupPosition).get(childPosition).getLocationName();
}

public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

public int getChildrenCount(int groupPosition) {
    return languages.get(groupPosition).size();
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
        View convertView, ViewGroup parent) {
    TextView childeTitle = null;
    LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = null;
    v = layoutInflater.inflate(R.layout.custom_language_child_row, null);
    childeTitle = (TextView) v.findViewById(R.id.text_element);
    String myText = this.getChild(groupPosition, childPosition).toString();
    childeTitle.setText(myText);

    return v;

}

public Object getGroup(int groupPosition) {
    return titles.get(groupPosition).getLocationName();
}

public int getGroupCount() {
    return titles.size();
}

public long getGroupId(int groupPosition) {
    return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
        ViewGroup parent) {

    TextView groupTitle = null;
    LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = null;
    v = layoutInflater.inflate(R.layout.custom_language_group_row, null);
    groupTitle = (TextView) v.findViewById(R.id.text_element);
    String myText = this.getGroup(groupPosition).toString();
    groupTitle.setText(myText);

    return v;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

public boolean hasStableIds() {
    return true;
}

LanguageListAdapter.java

public class LanguageListAdapter extends BaseAdapter {

private Activity activity;
List<LanguageDataType> language = new ArrayList<LanguageDataType>();
List<LanguageDataType> origLocation = new ArrayList<LanguageDataType>();

private static LayoutInflater inflater=null;



public LanguageListAdapter(Activity listActivity, List<LanguageDataType> LanguageList) {
    activity = listActivity;

    for (LanguageDataType value: LanguageList) {
        language.add(value);
    }

    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


public int getCount() {
    return language.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}


public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null){
        vi = inflater.inflate(R.layout.custom_language_child_row, null);
    }

    TextView text=(TextView)vi.findViewById(R.id.text_element);
    text.setText(language.get(position).getLocationName());

    return vi;
}

public Filter getFilter() {
    return null;
}

如果有人有任何建议我可以如何连续滚动浏览ListView和我的extendableListView我会非常感激。

2 个答案:

答案 0 :(得分:0)

首先尝试定义ExpandableListView,在屏幕底部对齐,然后将Listview定义为在ExpandableListView上方。

答案 1 :(得分:0)

我看到的唯一方法是在两者之间拆分屏幕...我不知道滚动两个可滚动项目的方法。

如果您有两个常规列表视图,我可以指向您的解决方案,但列表视图和可扩展列表视图将不适用于该解决方案。

我能想到的最好的方法是更改​​xml中的两个listview定义以包含:

android:layout_height="0dp"
android:layout_weight="1"

这会导致他们在屏幕之间拼接屏幕,并将它们放在屏幕上。