我正在制作我想要显示的应用程序我想要显示所有附件。但只能查看一个附件。下面是函数以及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>
我知道解决方案非常简单,但请帮帮忙。谢谢。
答案 0 :(得分:0)
您只能使用单个附件,因为每次在此代码片段中都会为同一个视图充气:
View view = getLayoutInflater().inflate(R.layout.attachment_item_layout, mAttachmentsLayout, true);
您可以通过两种方式获得所需的结果:
我会选择第二种选择。不要在 xml 文件中设置图像,而是在运行时添加图像。代码如下:
ImageView imView=(ImageView)view.findViewById(Your_Image_View_ID_here);
imView.setImageResource(Your_Resource_File_ID_Here);
并在循环递增时更改 Resource_Id 。 玩得开心:))