2

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

我的观点持有者:

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());
}
}

我的课程:

团体课:

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 +
            '}';
}
}

我的孩子班:

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);
}
}

主要活动:

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'}]}

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

提前致谢!

4

1 回答 1

1

这样做的问题是,从这里的文档中,您不必像您所做的那样将自定义模型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");
    }
    }

这可能是它的构建方式。也许是一种解决方法,这可能是该方法接受两个参数,即 ExpandableGroup 类和自定义模型类

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");
    }
于 2021-04-06T12:43:16.800 回答