繁体   English   中英

Android Studio中的LinearLayouts动态数量

[英]Dynamic number of LinearLayouts in Android Studio

在我的Android应用程序中,我想显示带有动态数量图像的Horizo​​ntalScrollView。 目前,我必须为每个图像构建一个LinearLayout。 有没有一种方法可以动态处理网址列表,并适当调整此列表长度的LinearLayouts数量?

附上我当前的代码

activity_mail.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.post.scollagainandagain.MainActivity">



<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="178dp"
        android:text="Fotos hinzufügen" />

    <HorizontalScrollView
        android:id="@+id/hz"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:layout_marginTop="208dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/ll1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical" >
                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent" >
                    <ImageView
                        android:id="@+id/iv1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                    <TextView
                        android:layout_width="0.5dp"
                        android:layout_height="fill_parent"
                        android:layout_alignParentRight="true"
                        android:background="@color/colorSeperator" />
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical" >
                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent" >
                    <ImageView
                        android:id="@+id/iv2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                    <TextView
                        android:layout_width="0.5dp"
                        android:layout_height="fill_parent"
                        android:layout_alignParentRight="true"
                        android:background="@color/colorSeperator" />
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical" >
                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent" >
                    <ImageView
                        android:id="@+id/iv3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                    <TextView
                        android:layout_width="0.5dp"
                        android:layout_height="fill_parent"
                        android:layout_alignParentRight="true"
                        android:background="@color/colorSeperator" />
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical" >
                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent" >
                    <ImageView
                        android:id="@+id/iv4"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                    <TextView
                        android:layout_width="0.5dp"
                        android:layout_height="fill_parent"
                        android:layout_alignParentRight="true"
                        android:background="@color/colorSeperator" />
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical" >
                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent" >
                    <ImageView
                        android:id="@+id/iv5"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                    <TextView
                        android:layout_width="0.5dp"
                        android:layout_height="fill_parent"
                        android:layout_alignParentRight="true"
                        android:background="@color/colorSeperator" />
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical" >
                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent" >
                    <ImageView
                        android:id="@+id/iv6"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                    <TextView
                        android:layout_width="0.5dp"
                        android:layout_height="fill_parent"
                        android:layout_alignParentRight="true"
                        android:background="@color/colorSeperator" />
                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>
    </HorizontalScrollView>
</RelativeLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

ImageView iv1, iv2, iv3, iv4, iv5, iv6;
LinearLayout ll1, ll2, ll3, ll4, ll5, ll6;
HorizontalScrollView hz;

int scrollViewHeigth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iv1 = (ImageView) findViewById(R.id.iv1);
    iv2 = (ImageView) findViewById(R.id.iv2);
    iv3 = (ImageView) findViewById(R.id.iv3);
    iv4 = (ImageView) findViewById(R.id.iv4);
    iv5 = (ImageView) findViewById(R.id.iv5);
    iv6 = (ImageView) findViewById(R.id.iv6);

    ll1 = (LinearLayout) findViewById(R.id.ll1);
    ll2 = (LinearLayout) findViewById(R.id.ll2);
    ll3 = (LinearLayout) findViewById(R.id.ll3);
    ll4 = (LinearLayout) findViewById(R.id.ll4);
    ll5 = (LinearLayout) findViewById(R.id.ll5);
    ll6 = (LinearLayout) findViewById(R.id.ll6);

    hz = (HorizontalScrollView) findViewById(R.id.hz);

    ViewTreeObserver vto = hz.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            scrollViewHeigth = hz.getHeight();
        }
    });

    GetImage getimage1 = new GetImage("1/1.png", iv1, ll1);
    getimage1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    GetImage getimage2 = new GetImage("1/2.png", iv2, ll2);
    getimage2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    GetImage getimage3 = new GetImage("1/3.png", iv3, ll3);
    getimage3.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    GetImage getimage4 = new GetImage("1/4.png", iv4, ll4);
    getimage4.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    GetImage getimage5 = new GetImage("1/5.png", iv5, ll5);
    getimage5.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    GetImage getimage6 = new GetImage("1/6.png", iv6, ll6);
    getimage6.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

}
private class GetImage extends AsyncTask<Void, Void, Boolean> {

    String url;
    ImageView iv;
    LinearLayout ll;
    Bitmap bitmap;

    public GetImage (String url, ImageView iv, LinearLayout ll) {
        this.iv = iv;
        this.url = "https://url.url.com/app/images/" + url;
        this.ll = ll;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        try {
            bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
            MainActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    double srcollHeight = scrollViewHeigth;
                    double bitmapHeight = bitmap.getHeight();
                    double bitmapWidth = bitmap.getWidth();
                    double width = srcollHeight/bitmapHeight*bitmapWidth;
                    iv.setImageBitmap(bitmap);
                    ll.setLayoutParams(new LayoutParams((int) width, LayoutParams.WRAP_CONTENT));
                }
            });

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

    protected void onPostExecute(Boolean result) {

    }
}

}

你能帮助我吗?

解决此错误的正确方法是使用RecyclerView和Adapter。

Recyclerview是专门为此任务而构建的,在该任务中,您有动态数量的项目要在屏幕上显示。

如果您想开始使用RecyclerView,请查看以下链接: http : //www.theappguruz.com/blog/learn-recyclerview-with-an-example-in-android

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM