Android ListView / Recyclerview动态内容

时间:2015-06-26 08:14:24

标签: android android-layout android-listview android-recyclerview recycler-adapter

您好我是iOS开发人员刚开始使用android,我知道android的基本概念,如RecyclerView,ListView。

我想制作一个Feed页面,其中每行可以包含一张照片和多条评论,根据数据,评论不会动态变化。

如何使用RecyclerView / ListView实现相同目标?

我可以在iOS中实现相同的目标:http://t.co/z1IRHTTjED

1 个答案:

答案 0 :(得分:0)

如果您已经了解ListView和RecyclerView的概念,您应该知道我们使用Adapters为List提供视图。

您可以创建一个自定义适配器,它将创建一个视图,其中,取决于您拥有的数据,您将显示或不显示注释。

修改 例如,你有这样的布局

<LinearLayout>

<ImageView />

<TextView android:id="@+id/comments" />

</LinearLayout>
在你的适配器中你应该打电话

TextView comments = (TextView) findViewById(R.id.comments);
if(haveComments) {
    comments.setVisibility(View.VISIBLE);
    //display comments
} else {
    comments.setVisibility(View.GONE);
}

如果您需要更复杂的评论布局,例如图片和日期字段,而不是TextView声明具有垂直方向的LinearLayout。您可以使用代码中的项目填充它。

 LinearLayout commentsLayout = (LinearLayout) findViewById(R.id.commentsLayout);
    if(haveComments) {
        comments.setVisibility(View.VISIBLE);
        //display comments
        commentsLayout.addView(inflateCommentView());
    } else {
        comments.setVisibility(View.GONE);
    }

private View inflateCommentView() {
     View myComment =  LayoutInflater.from(context).inflate(R.layout.my_complex_comment, null);
     //set listeners, fill with data
     return myComment;
}