2

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

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

语言列表.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>

语言列表.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将不胜感激。

4

2 回答 2

0

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

于 2012-06-15T13:07:33.883 回答
0

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

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

我能想到的最好的方法是将 xml 中的两个列表视图定义更改为包含:

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

这将导致他们在它们之间分割屏幕并将它们都保留在屏幕上。

于 2012-06-15T15:54:27.970 回答