0

这是我的适配器类

public class GenreAdapter extends ExpandableRecyclerViewAdapter<GenreViewHolder, ArtistViewHolder> {
    Context context ;
    LayoutInflater inflater ;

    public GenreAdapter(List<? extends ExpandableGroup> groups,Context context) {
        super(groups);
        this.context = context ;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public GenreViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
        //LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.list_item_genre, parent, false);
        return new GenreViewHolder(view);
    }

    @Override
    public ArtistViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.list_item_artist, parent, false);
        return new ArtistViewHolder(view);
    }

    @Override
    public void onBindChildViewHolder(ArtistViewHolder holder, int flatPosition, ExpandableGroup group,
                                      int childIndex) {
        final Artist artist =(Artist) group.getItems().get(childIndex);
        holder.setArtistName(artist.getName());
    }

    @Override
    public void onBindGroupViewHolder(GenreViewHolder holder, int flatPosition,
                                      ExpandableGroup group) {
        holder.setGenreTitle(group);
    }
}

这是我的模型课

package com.nmn.expandablerecycler.utils;

import android.os.Parcel;
import android.os.Parcelable;

import com.thoughtbot.expandablerecyclerview.models.ExpandableGroup;

import java.util.List;


public class Artist  implements Parcelable {

    private String name;
    private boolean isFavorite;


    public Artist(String name, boolean isFavorite) {
        this.name = name;
        this.isFavorite = isFavorite;
    }

    protected Artist(Parcel in) {
        name = in.readString();
    }

    public String getName() {
        return name;
    }
  /*  public String getTitle()
    {
        return title ;
    }*/
    public boolean isFavorite() {
        return isFavorite;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Artist)) return false;

        Artist artist = (Artist) o;

        if (isFavorite() != artist.isFavorite()) return false;
        return getName() != null ? getName().equals(artist.getName()) : artist.getName() == null;

    }

    @Override
    public int hashCode() {
        int result = getName() != null ? getName().hashCode() : 0;
        result = 31 * result + (isFavorite() ? 1 : 0);
        return result;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Artist> CREATOR = new Creator<Artist>() {
        @Override
        public Artist createFromParcel(Parcel in) {
            return new Artist(in);
        }

        @Override
        public Artist[] newArray(int size) {
            return new Artist[size];
        }
    };
}


public class Genre extends ExpandableGroup<Artist> {

    public Genre(String title, List<Artist> items) {
        super(title, items);
    }
}

>This are my viewholder classes 
public class ArtistViewHolder extends ChildViewHolder {

    private TextView artistName;

    public ArtistViewHolder(View itemView) {
        super(itemView);
        artistName = (TextView) itemView.findViewById(R.id.artist_name);
    }

    public void onBind(Artist artist) {
       artistName.setText(artist.getName());
    }

    public void setArtistName (String name )
    {

    }
}


public class GenreViewHolder  extends GroupViewHolder {

        private TextView genreTitle;

        public GenreViewHolder(View itemView) {
            super(itemView);
            genreTitle =(TextView) itemView.findViewById(R.id.genre_title);
        }

        public void setGenreTitle(ExpandableGroup group) {
           genreTitle.setText(group.getTitle());
        }
}

>this is the main activity 

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList<Artist>artists = new ArrayList<>();
        for (int i = 0 ; i<6 ;i++)
        {
            artists.add(new Artist("naman",true));
        }

        ArrayList<Genre> genres = new ArrayList<>();
        for (int i = 0 ; i<6 ;i++)
        {
            genres.add(new Genre("Sufi",artists));
        }


        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        //instantiate your adapter with the list of genres
        GenreAdapter adapter = new GenreAdapter(genres,this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }
}

艺术家(儿童)姓名上的文字未显示。尽管父(流派文本)也会扩展和折叠。让我知道如何在这个可扩展的回收器视图中显示子(艺术家)项目的文本

4

3 回答 3

0

我也在实施 com.thoughtbot.expandablerecyclerview。我在同一场景中所做的是,在扩展 ExpandableGroup 的自定义类中,我重写了 getItems() 和 getItemCount() 方法,并提供了我想要的值作为“子”扩展状态。

于 2021-03-31T14:14:51.343 回答
0

伙计,这听起来可能很愚蠢,但是您的setArtistName方法是空的。粘贴此代码artistName.setText(artist.getName());

于 2017-01-25T05:08:20.207 回答
0

库文档有错误

public void onBind(Artist artist) {
   artistName.setText(artist.getName());
}

public void setArtistName (String name )
{

}

在此代码段中,只需将方法的名称从 onBind 更改为 setArtistName 并删除您创建的空方法。

于 2017-02-09T07:35:05.683 回答