我想使用列表显示单个文本视图中的附件列表。但我只能显示一个对象



我正在制作一个应用程序,我想显示我想显示所有附件。但是只能查看一个附件。下面是函数和XML:

//this is the function that displays attachments:    
public void showAttachments(List<Attachment> attachments) {
    mAttachmentsLayout.setVisibility(View.VISIBLE);
    for (Attachment attachment : attachments) {
        View view = getLayoutInflater().inflate(R.layout.attachment_item_layout,   mAttachmentsLayout, true); 
        view.setTag(attachment);
        //inflating the layout xml file
        TextView attachmentTitle = (TextView)view.findViewById(R.id.attachment_title); 
        String title = attachment.getFileName();
        //getFileName() returns the name of the attachment
        if (TextUtils.isEmpty(title)) {
            title = attachment.getFileName();
        }
        //assigning title to textview:
        attachmentTitle.setText(title);
        //want to display all attachments
        //not just one
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBasePermissionPresenter.onClickAttachment((Attachment)v.getTag());
            }
        });
    }
}             

附件容器大小增加,但只显示一个附件名称

这是相应的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/attachmentHeading"
            android:padding='@dimen/posting_details_attachment_item_padding'
            android:orientation="vertical">
    <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:id="@+id/attachment_title"/> 
</LinearLayout>
我知道解决方法很简单,但是请帮忙。谢谢。

您只能使用单个附件,因为在这段代码中您每次都在膨胀相同的视图:

View view = getLayoutInflater().inflate(R.layout.attachment_item_layout,   mAttachmentsLayout, true);

你可以通过两种方式得到你想要的结果:

  • 为每个图像创建不同的layout.xml(这将是很多工作)
  • 或者您可以在运行时添加图像

我会选择第二个选项。不要在xml文件中设置图像,而是在运行时添加图像。代码如下:

ImageView imView=(ImageView)view.findViewById(Your_Image_View_ID_here);
imView.setImageResource(Your_Resource_File_ID_Here);

并随着循环的增加改变Resource_Id。祝你玩得开心!

最新更新