以编程方式将ImageView正确添加并显示到LinearLayout

时间:2020-04-17 21:38:57

标签: android-studio imageview android-linearlayout

我有一个ParseUsers列表,每个用户都有一个包含图片的ParseFile。 我有一个带有单个空的垂直LinearLayout的Activity,我想以编程方式为每个ParseUsers添加一个ImageView并将其正确显示在ListView上(因此,如果需要,它必须是可滚动的,例如Instagram)

我不知道为什么,但是我得到4个ImageViews的结果是这样的(1个大ImageView,下面是一个小ImageView,而没有其他2个的痕迹): https://www.noelshack.com/2020-16-5-1587159377-capture.png

这是我的布局的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SpecificFeed">

        <LinearLayout
            android:id="@+id/feedLinearLayout"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"></LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我关于这个问题的Java代码:

for (ParseObject object : objects) {

                    ParseFile feedParseFile = object.getParseFile("image");
                    feedParseFile.getDataInBackground(new GetDataCallback() {
                        @Override
                        public void done(byte[] data, ParseException e) {

                            if (e == null) {

                                Bitmap feedBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

                                ImageView feedImageView = new ImageView(getApplicationContext());
                                feedImageView.setImageBitmap(feedBitmap);
                                feedImageView.setLayoutParams(new ViewGroup.LayoutParams(

                                        ViewGroup.LayoutParams.MATCH_PARENT,
                                        ViewGroup.LayoutParams.WRAP_CONTENT

                                ));

                                feedLinearLayout.addView(feedImageView);

                            } else {

                                e.printStackTrace();

                            }

                        }
                    });

                }

感谢您阅读

0 个答案:

没有答案