ParallaxScrollView中ExpandableListView的问题

时间:2014-09-29 19:02:23

标签: android expandablelistview

我想用自定义适配器填充ExpandableListView。

这是我填写它的方式,在我的活动中:

 private void fillSeasons(Video video) {
        if(video instanceof Series) {
            Series series = (Series) video;
            HashMap<Season, List<Episode>> map = new HashMap<Season, List<Episode>>();
            for(Season season:series.getSeasons()) {
                map.put(season, season.getEpisodes());
            }
            SeasonAdapter adapter = new SeasonAdapter(this,series.getSeasons(),map);
            testSeason.setText(adapter.getGroupCount() + " seasons"); //A TextView to test adapter
            listSeason.setAdapter(adapter);
        }
        else {
            layoutSeasons.setVisibility(View.INVISIBLE);
        }
    }

这是我的自定义适配器:

public class SeasonAdapter extends BaseExpandableListAdapter {

    private static final String LOG = SeasonAdapter.class.getSimpleName();
    private static String PREFIX_IMAGE = TmdbService.PREFIX_IMAGE+TmdbService.POSTER_SIZES[3];

    private Context context;
    private List<Season> seasons; // header titles
    // child data in format of header title, child title
    private HashMap<Season, List<Episode>> episodes;

    public SeasonAdapter(Context context, List<Season> seasons,
                                 HashMap<Season, List<Episode>> episodes) {
        this.context = context;
        this.seasons = seasons;
        this.episodes = episodes;
        String msg = "Episodes:\n";
        for(Season season:episodes.keySet()) {
            msg += "Season "+season.getNumber()+" ("+season.getId()+")\n";
            for(Episode episode:episodes.get(season)) {
                msg+=episode.getNumber()+"-"+episode.getTitle()+"\n";
            }
        }
        Log.d(LOG,msg);
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        Log.d(LOG,"getChild at position "+childPosititon+" of group "+groupPosition);
        return this.episodes.get(this.seasons.get(groupPosition)).get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        Log.d(LOG,"getChildId: "+childPosition);
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_episode_item, null);
        }
        final Episode episode = (Episode) getChild(groupPosition, childPosition);
        Log.d(LOG,"childView:"+ episode.getTitle());

        TextView txtListChild = (TextView) convertView.findViewById(R.id.season_text);

        txtListChild.setText(episode.getTitle());
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        Log.d(LOG,"getChildrenCount: "+this.episodes.get(this.seasons.get(groupPosition)).size());
        return this.episodes.get(this.seasons.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        Log.d(LOG,"getGroup at position "+groupPosition);
        return this.seasons.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        Log.d(LOG,"getGroupCount: "+this.seasons.size());
        return this.seasons.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        Log.d(LOG,"getGroupId: "+groupPosition);
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_season_group, null);
        }
        final Season season = (Season) getGroup(groupPosition);
        Log.d(LOG,"groupView:"+ context.getString(R.string.season)+" "+season.getNumber());

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.season_title);
        lblListHeader.setText(context.getString(R.string.season)+" "+season.getNumber());

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        Log.d(LOG,"hasStableIds: false");
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        Log.d(LOG,"isChildSelectable: true");
        return true;
    }


}

我的问题是:参数&#34; groupPosition&#34;在方法getGroupView始终&#34; 0&#34;,但getGroupCount返回正确的值(在我的情况下为6)。

这是输出:

09-29 21:38:38.070    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ Episodes:
    Season 1 (3572)
    1-Chute libre
    2-Le choix
    3-Dérapage
    4-Retour aux sources
    5-Vivre ou survivre
    6-Bluff
    7-Le fruit défendu
    Season 2 (3573)
    1-Traqués
    2-Chasse à l'homme
    3-Alibi
    4-Au fond du gouffre
    5-Nouveau départ
    6-Règlements de comptes
    7-Poisson lune
    8-Appelez donc Saule
    9-Seuls au monde
    10-Introspection
    11-Nouvelle donne
    12-Vie et mort
    13-Effet papillon
    Season 3 (3575)
    1-No Mas
    2-Caballo Sin Nombre
    3-I.F.T.
    4-Green Light
    5-Retour aux affaires
    6-Le Camping-Car
    7-Vendetta
    8-Prise de pouvoir
    9-Kafkaïen
    10-La Mouche
    11-Société écran
    12-Demi-mesures
    13-Pleine mesure
    Season 4 (3576)
    1-Le Cutter
    2-Snub 38
    3-Motivations
    4-Les Points importants
    5-Nouveau Job
    6-Guerre froide
    7-Négociations
    8-Frères et Partenaires
    9-Incontrôlables
    10-Salud
    11-Seul contre tous
    12-Échec
    13-Mat
    Season 0 (3577)
    1-
    2-
    3-
    4-
    5-
    6-
    Season 5 (3578)
    1-Vivre libre ou mourir
    2-Madrigal
    3-Nouveaux labos
    4-Cinquante et un
    5-Un plan presque parfait
    6-Divergence
    7-Heisenberg
    8-Un nouveau jour se lève
    9-Le prix du sang
    10-Enterré
    11-Confessions
    12-Comme un chien enragé
    13-Règlement de compte à Tohajiilee
    14-Seul au monde
    15-L'origine du mal
    16-Revenir et mourir
09-29 21:38:38.070    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroupCount: 6
09-29 21:38:38.070    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ hasStableIds: false
09-29 21:38:38.070    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroupCount: 6
09-29 21:38:38.070    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroupCount: 6
09-29 21:38:38.080    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroupCount: 6
09-29 21:38:38.080    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroup at position 0
09-29 21:38:38.080    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ groupView:Season 0
09-29 21:38:38.080    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroupCount: 6
09-29 21:38:38.080    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroup at position 0
09-29 21:38:38.090    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ groupView:Season 0
09-29 21:38:38.130    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroupCount: 6
09-29 21:38:38.130    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroup at position 0
09-29 21:38:38.130    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ groupView:Season 0
09-29 21:38:38.130    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroupCount: 6
09-29 21:38:38.130    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroup at position 0
09-29 21:38:38.130    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ groupView:Season 0
09-29 21:38:38.130    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroupCount: 6
09-29 21:38:38.130    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroup at position 0
09-29 21:38:38.130    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ groupView:Season 0
09-29 21:38:38.130    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroupCount: 6
09-29 21:38:38.130    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroup at position 0
09-29 21:38:38.130    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ groupView:Season 0
09-29 21:38:38.140    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroupCount: 6
09-29 21:38:38.140    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroup at position 0
09-29 21:38:38.140    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ groupView:Season 0
09-29 21:38:38.140    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroupCount: 6
09-29 21:38:38.140    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroup at position 0
09-29 21:38:38.140    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ groupView:Season 0
09-29 21:38:38.150    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroupCount: 6
09-29 21:38:38.150    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ getGroup at position 0
09-29 21:38:38.150    4199-4199/com.vwuilbea.mymoviecatalog D/SeasonAdapter﹕ groupView:Season 0

我不知道问题是否来自我的布局,如果是这样的话:

 <RelativeLayout >
     <!-- ... -->
     <ParallaxScrollView >
        <!-- ... -->
        <!-- Season layout -->
        <RelativeLayout
            android:id="@+id/layout_seasons"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/minimum_margin"
            android:layout_below="@id/layout_location">

            <TextView
                android:id="@+id/test_season"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/label_seasons"/>

            <ExpandableListView
                android:id="@+id/season_list"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/test_season"
                android:choiceMode="singleChoice" />

       </RelativeLayout>
      <!-- ... -->
    </ParallaxScrollView>
</RelativeLayout>

list_season_group.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/season_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="@dimen/subtitle_medium_size_details"
        android:textStyle="bold"
        android:text="@string/hello_world"
        tools:ignore="HardcodedText"/>

</LinearLayout>

最后,我的list_episode_item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/episode_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="@dimen/subtitle_medium_size_details"
        android:textStyle="bold"
        android:text="@string/hello_world"
        tools:ignore="HardcodedText"
        />

</LinearLayout>

我发现了我的问题来源:

我的ExpandableListView是一个ParallaxScrollView,所以如果它将list_height列表设置为&#34; wrap_content&#34;或&#34; match_parent&#34;,它不起作用(我不确切知道为什么?!)。我已经尝试将layout_height设置为200dp,它运行正常!

但是有人可以告诉我为什么?我怎么能正确地做到这一点?

(我是否必须打开一个新主题才能提出这个问题?)

1 个答案:

答案 0 :(得分:0)

如果组位置始终为0,则表示您在同一季节中存储所有剧集。