【问题标题】:Android ListView/Recyclerview dynamic contentAndroid ListView/Recyclerview 动态内容
【发布时间】:2015-06-26 08:14:24
【问题描述】:

你好,我是刚开始接触android的iOS开发者,我了解android的基本概念,如RecyclerView、ListView。

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

如何使用 RecyclerView/ListView 实现相同的功能?

我可以像这样在 iOS 中实现同样的效果:http://t.co/z1IRHTTjED

【问题讨论】:

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


    【解决方案1】:

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

    您可以创建一个自定义适配器,该适配器将创建一个视图,根据您拥有的数据,您将在哪里显示 cmets。

    编辑。 例如你有这样的布局

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

    如果您需要为 cmets 进行更复杂的布局,例如带有图像和日期字段,而不是 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;
    }
    

    【讨论】:

    • 如何在视图中放置动态文本视图数?
    • 适配器有一个方法 getCount() 只需更改该值即可。
    猜你喜欢
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多