android - Thoughtbot Recyclerview 可扩展组不扩展

标签 android android-recyclerview expandablerecyclerview thoughtbot

我使用的是 Thoughtbot 可扩展回收 View (https://github.com/thoughtbot/expandable-recycler-view),但我无法通过点击来扩展组。我也知道子项甚至没有绑定(bind)(不确定它们是否应该绑定(bind)或仅在扩展组时绑定(bind)。)

我的观点持有者:

public class PlaylistViewHolder extends GroupViewHolder {
private TextView mPlaylistName, mPlaylistCount;
private ImageView arrow; 
ExpandableListView.OnGroupClickListener listener;

public PlaylistViewHolder(View itemView) {
    super(itemView);
    mPlaylistName = itemView.findViewById(R.id.playlistNameView);
    mPlaylistCount = itemView.findViewById(R.id.videosCount);
    arrow = itemView.findViewById(R.id.expandBtn);
}


public void setCurrentPlaylist(YoutubePlaylist playlist){
    Log.e("###",playlist.toString());
    mPlaylistName.setText(playlist.getListTitle());
    mPlaylistCount.setText("5");
}
}

/////

public class VideoViewHolder extends ChildViewHolder {

TextView mVideoName,mVideoLinkView;
ImageView mVideoThumb;

public VideoViewHolder(View itemView) {
    super(itemView);
    mVideoName = itemView.findViewById(R.id.videoNameView);
    mVideoThumb = itemView.findViewById(R.id.videoThumb);
    mVideoLinkView = itemView.findViewById(R.id.videoLinkView);
}

public void onBind(YoutubeVideo video){
    Log.e("###","Video binded!!!!!!!");
    mVideoName.setText(video.getTitle());
    mVideoLinkView.setText(video.getThumbnailLink());
}
}

我的类(class):

组类:

public class YoutubePlaylist extends ExpandableGroup<YoutubeVideo> {

@SerializedName("ListTitle")
private String title;

@SerializedName("ListItems")
private ArrayList<YoutubeVideo> videos;

public YoutubePlaylist(String title, List<YoutubeVideo> items) {
    super(title, items);
    this.title = title;
}

public String getListTitle() {
    return title;
}

public void setListTitle(String listTitle) {
    this.title = listTitle;
}

public ArrayList<YoutubeVideo> getVideos() {
    return videos;
}

public void setVideos(ArrayList<YoutubeVideo> videos) {
    this.videos = videos;
}

@Override
public String toString() {
    return "YoutubePlaylist{" +
            "title='" + title + '\'' +
            ", videos=" + videos +
            '}';
}
}

我的 child 类(class):

class YoutubeVideo implements Parcelable {

@SerializedName("Title")
private String title;

@SerializedName("link")
private String linkToVideo;

@SerializedName("thumb")
private String thumbnailLink;

public YoutubeVideo(String title, String linkToVideo, String thumbnailLink) {
    this.title = title;
    this.linkToVideo = linkToVideo;
    this.thumbnailLink = thumbnailLink;
}

protected YoutubeVideo(Parcel in) {
    title = in.readString();
    linkToVideo = in.readString();
    thumbnailLink = in.readString();
}

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

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

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getLinkToVideo() {
    String newLink;
    if (linkToVideo.contains(" ")) {
        newLink = linkToVideo.replaceAll(" ", "");
        return newLink;
    }
    return linkToVideo;
}

public void setLinkToVideo(String linkToVideo) {
    this.linkToVideo = linkToVideo;
}

public String getThumbnailLink() {
    String fixedThumb=thumbnailLink;
    if (thumbnailLink.contains(" ")) {
        fixedThumb = thumbnailLink.replaceAll(" ", "");
        return fixedThumb;
    }
    return thumbnailLink;
}

public void setThumbnailLink(String thumbnailLink) {
    this.thumbnailLink = thumbnailLink;
}

@Override
public String toString() {
    return "YoutubeVideo{" +
            "title='" + getTitle() + '\'' +
            ", linkToVideo='" + getLinkToVideo() + '\'' +
            ", thumbnailLink='" + getThumbnailLink() + '\'' +
            '}';
}

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

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

适配器:

public class PlaylistAdapter extends 
ExpandableRecyclerViewAdapter<PlaylistViewHolder, VideoViewHolder> {


public PlaylistAdapter(List<? extends ExpandableGroup> groups) {
    super(groups);
}

@Override
public PlaylistViewHolder onCreateGroupViewHolder(ViewGroup parent, int 
viewType) {
    View view = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.playlist_item, 
parent, false);
    return new PlaylistViewHolder(view);
}

@Override
public VideoViewHolder onCreateChildViewHolder(ViewGroup parent, int 
viewType) {
    View view = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.video_item, 
parent, 
false);
    return new VideoViewHolder(view);
}

@Override
public void onBindChildViewHolder(VideoViewHolder holder, int flatPosition, 
ExpandableGroup group, int childIndex) {
    YoutubeVideo video = (YoutubeVideo) group.getItems().get(childIndex);
    holder.onBind(video);
}

@Override
public void onBindGroupViewHolder(PlaylistViewHolder holder, int 
flatPosition, ExpandableGroup group) {
    holder.setCurrentPlaylist((YoutubePlaylist) group);
}
}

主要 Activity :

public class MainActivity extends AppCompatActivity {

RecyclerView videoListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    GetJsonFromUrl getJsonService = RetrofitInstance.getRetrofitInstance().create(GetJsonFromUrl.class);
    Call<JsonObject> call = getJsonService.getJSON();
    call.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
            if(response.isSuccessful()){
                JsonArray array = response.body().getAsJsonArray("Playlists");
                Gson gson = new Gson();
                Type type = new TypeToken<ArrayList<YoutubePlaylist>>(){}.getType();
                ArrayList<YoutubePlaylist> youtubePlay = gson.fromJson(array, type);
                for(YoutubePlaylist playlist : youtubePlay){
                    ArrayList<YoutubeVideo> videos = playlist.getVideos();
                    Log.e("###",videos.toString());
                }
                setArrayToAdapter(youtubePlay);
            }
        }

        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {

        }
    });
}

private void setArrayToAdapter(ArrayList<YoutubePlaylist> youtubePlay) {
    videoListView = findViewById(R.id.videosView);
    LinearLayoutManager manager = new LinearLayoutManager(this);

    PlaylistAdapter adapter = new PlaylistAdapter(youtubePlay);
    videoListView.setLayoutManager(manager);
    videoListView.setAdapter(adapter);

}
}

播放列表对象输出示例:

YoutubePlaylist{title='Zen Work Music', videos=[YoutubeVideo{title='HEALING ZEN Music', linkToVideo='https://www.youtube.com/watch?v=SbCpzWMWb68', thumbnailLink='https://i.ytimg.com/vi_webp/SbCpzWMWb68/mqdefault.webp'}, YoutubeVideo{title='Relaxing Music - Meditation', linkToVideo='https://www.youtube.com/watch?v=qrx1vyvtRLY', thumbnailLink='https://i.ytimg.com/vi_webp/qrx1vyvtRLY/mqdefault.webp'}, YoutubeVideo{title='Relaxing Music - Background', linkToVideo='https://www.youtube.com/watch?v=loIZy6GqhUw', thumbnailLink='https://i.ytimg.com/vi_webp/loIZy6GqhUw/mqdefault.webp'}, YoutubeVideo{title='Delta Waves Sleep Music', linkToVideo='https://www.youtube.com/watch?v=EshmcHB3yMg', thumbnailLink='https://i.ytimg.com/vi_webp/EshmcHB3yMg/mqdefault.webp'}]}

我正在绑定(bind)组并显示在列表中,但是 onClick 没有任何反应,我在文档中没有看到任何关于 onClickListener 的内容。

提前致谢!

最佳答案

问题在于文档 here您不必像以前那样将自定义模型 YoutubePlaylist.java 作为参数传递,而是将其替换为 ExpandableGroup 以便您的代码

      public class PlaylistViewHolder extends GroupViewHolder {
    private TextView mPlaylistName, mPlaylistCount;
    private ImageView arrow; 
    ExpandableListView.OnGroupClickListener listener;
    
    public PlaylistViewHolder(View itemView) {
        super(itemView);
        mPlaylistName = itemView.findViewById(R.id.playlistNameView);
        mPlaylistCount = itemView.findViewById(R.id.videosCount);
        arrow = itemView.findViewById(R.id.expandBtn);
    }
    
    
    public void setCurrentPlaylist(ExpandableGroup playlist){ 
//the parameter has to be ExpandableGroup class and you can only get title
        Log.e("###",playlist.getTitle());//you must call the getTitle() method
        mPlaylistName.setText(playlist.getListTitle());
        mPlaylistCount.setText("5");
    }
    }

This could be the way it was built. maybe a workaround this may be for this method to then accept two parameters which is the ExpandableGroup class and the custom model, class

public void setCurrentPlaylist(ExpandableGroup playlist, YoutubePlaylist playlist2){ 
//the parameter has to be ExpandableGroup class and you can only get title
        Log.e("###",playlist.getTitle());//you must call the getTitle() method
        mPlaylistName.setText(playlist2.getListTitle());
        mPlaylistCount.setText("5");
    }

关于android - Thoughtbot Recyclerview 可扩展组不扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48245753/

相关文章:

android - 可扩展的 Recyclerview 自定义子工具问题

java - 为什么我不能在 Kotlin 中访问父类(super class)的枚举?

android - 启动时 fragment 中的 fab 位置不正确

android - RecyclerView如何循环

android - 列表过滤与 Material Design 兼容?

android - Grid Recyclerview,偶数行有 3 列,奇数行有 4 列

android - 可扩展的 RecyclerView Android Studio 示例

android - 如何为 Sherlock fragment 创建对话 fragment ?

android - 如何更改 Alertdialog 的背景图像

android - android中的分段recyclerview标题和子项目