0
私のアンドロイドアプリでは、動的な数の画像を含むHorizontalScrollViewを表示したいと考えています。現在、各画像に対してLinearLayoutを構築する必要があります。 URLのリストを使って動的にそれを行い、このリストの長さのLinearLayoutsの量を適切にする方法はありますか?Androidスタジオの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) {
}
}
}
は、あなたが私を助けることができますか?