0

我一直在尝试使用适用于 android 的 groupie 库在 RecyclerView 上实现一个简单的帖子和评论设计,用户可以在其中发表帖子并在该帖子下发表评论。我在下面附上了一个简单的设计。当用户点击帖子时,它会显示附加到每个帖子的所有评论。我遇到了 groupie 库,并决定在这个简单应用程序的沙箱中对其进行测试,因此当它工作时,我可以将其推送到主应用程序,但问题是如何自动加载或让 HeaderItemClass 内容具有或显示 ContentItemClass 显示或加载单击或展开时在其下方。

时间线帖子

时间线评论

这是我到目前为止所做的

HeaderItemClass.java

    import android.text.TextUtils;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import androidx.annotation.NonNull;
    import androidx.databinding.ViewDataBinding;
    
    import com.bumptech.glide.Glide;
    import com.xwray.groupie.ExpandableGroup;
    import com.xwray.groupie.ExpandableItem;
    import com.xwray.groupie.GroupieViewHolder;
    import com.xwray.groupie.Item;
    import com.xwray.groupie.databinding.BindableItem;
    
    import java.util.ArrayList;
    
    
    public class HeaderItemClass extends Item<HeaderItemClass.HeaderItemViewHolder>  implements ExpandableItem {
        private String sender_name, sender_post, sender_icon_link;
        private ExpandableGroup expandableGroup;
        private HeaderModel headerModel;
        private final String EMPTY_VAL = "Empty val";
        private View v;
    
    
    
    
    
        public HeaderItemClass(HeaderModel headerModel) {
            this.headerModel = headerModel;
            expandableGroup = new ExpandableGroup(new HeaderItemClass());
    
        }
    
        public HeaderItemClass(){}
    
    
        @Override
        public void bind(@NonNull HeaderItemViewHolder viewHolder, int position) {
           // headerModel = headerModels.get(position);
            if(!TextUtils.isEmpty(headerModel.getSender_name())){
                viewHolder.tv_sender_name.setText(headerModel.getSender_name());
            }
            else{
                viewHolder.tv_sender_name.setText(EMPTY_VAL);
            }
    
            if(!TextUtils.isEmpty(headerModel.getSender_contents())){
            viewHolder.sender_text.setText(headerModel.getSender_contents());}
            else{
                viewHolder.sender_text.setText(EMPTY_VAL);
            }
            viewHolder.linLay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    expandableGroup.onToggleExpanded();
                    
                }
            });
    
        }
    
        @Override
        public int getLayout() {
            return R.layout.layout_header_group;
        }
    
        @NonNull
        @Override
        public HeaderItemViewHolder createViewHolder(@NonNull View itemView) {
            return new HeaderItemViewHolder(itemView);
        }
    
        @Override
        public void setExpandableGroup(@NonNull ExpandableGroup onToggleListener) {
            this.expandableGroup = onToggleListener;
        }
    
        class HeaderItemViewHolder extends GroupieViewHolder{
            TextView tv_sender_name, sender_text;
            LinearLayout linLay;
    
    
         public HeaderItemViewHolder(@NonNull View rootView) {
             super(rootView);
             tv_sender_name = rootView.findViewById(R.id.tv_head_name);
             sender_text = rootView.findViewById(R.id.tv_head_post);
             linLay = rootView.findViewById(R.id.lin_expand_header);
    
         }
    
    
     }
    
    
 }

ContentItemClass.java

   import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.xwray.groupie.GroupieViewHolder;
import com.xwray.groupie.Item;

public class ContentItemClass  extends Item<ContentItemClass.ContentItemViewHolder> {


    ContentModel contentModel;
    private final String EMPTY_VAL = "Empty val";



    public ContentItemClass(ContentModel contentModel) {
        this.contentModel = contentModel;
    }

    public ContentItemClass(){}

    @Override
    public void bind(@NonNull ContentItemViewHolder viewHolder, int position) {
        if(!TextUtils.isEmpty(contentModel.getCommenter_comment())){
        viewHolder.commenter_comments.setText(contentModel.getCommenter_comment());}
        else{
            viewHolder.commenter_comments.setText(EMPTY_VAL);
        }

        if (!TextUtils.isEmpty(contentModel.getCommenter_name())){
        viewHolder.commenter_name.setText(contentModel.getCommenter_name());}
        else{
            viewHolder.commenter_name.setText(EMPTY_VAL);}
        }


    @NonNull
    @Override
    public ContentItemViewHolder createViewHolder(@NonNull View itemView) {
        return new ContentItemViewHolder(itemView);
    }

    @Override
    public int getLayout() {
        return R.layout.layout_content_group;
    }

    class ContentItemViewHolder extends GroupieViewHolder{
            TextView commenter_name, commenter_comments;

        public ContentItemViewHolder(@NonNull View rootView) {
            super(rootView);
            commenter_name = rootView.findViewById(R.id.tv_commenter_name);
            commenter_comments = rootView.findViewById(R.id.tv_commenter_comment);

        }
    }
}

layout_header_group.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/lin_expand_header"
    android:layout_margin="4dp"
    android:padding="4dp"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="Samuel Iwuchukwu"
        android:layout_margin="2dp"
        android:padding="2dp"
        android:id="@+id/tv_head_name"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        tools:text="Happy new month guys"
        android:padding="2dp"
        android:id="@+id/tv_head_post"/>

    <!--<ImageView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:contentDescription="@string/app_name"
        android:layout_margin="2dp"
        android:padding="2dp"
        android:src="@drawable/abc_vector_test"
        android:id="@+id/img_post_img"/>-->

</LinearLayout>

layout_content_group.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_margin="4dp"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="Samuel Joel"
        android:layout_margin="2dp"
        android:padding="2dp"
        android:id="@+id/tv_commenter_name"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="Happy new month guys"
        android:layout_margin="2dp"
        android:padding="2dp"
        android:id="@+id/tv_commenter_comment"/>

</LinearLayout>

HeaderModel.java

public class HeaderModel  {
    private String sender_name, sender_contents, post_icon;

    public HeaderModel(String sender_name, String sender_contents, String post_icon) {
        this.sender_name = sender_name;
        this.sender_contents = sender_contents;
        this.post_icon = post_icon;
    }

    public HeaderModel(String sender_name, String sender_contents) {
        this.sender_name = sender_name;
        this.sender_contents = sender_contents;
    }

    public String getSender_name() {
        return sender_name;
    }

    public String getSender_contents() {
        return sender_contents;
    }

    public String getPost_icon() {
        return post_icon;
    }
}

内容模型.java

    public class ContentModel {
    public String commenter_name;
    public String commenter_comment;

    public String getCommenter_name() {
        return commenter_name;
    }

    public void setCommenter_name(String commenter_name) {
        this.commenter_name = commenter_name;
    }

    public String getCommenter_comment() {
        return commenter_comment;
    }

    public void setCommenter_comment(String commenter_comment) {
        this.commenter_comment = commenter_comment;
    }

    public ContentModel(String commenter_name, String commenter_comment) {
        this.commenter_name = commenter_name;
        this.commenter_comment = commenter_comment;
    }
}
4

0 回答 0