【发布时间】:2020-01-06 21:54:58
【问题描述】:
我正在开发一个 Android 应用程序,以在具有两列的可滚动视图中显示一些电影。
一切都很好,只是行之间的空间很大。
我尝试了很多在 StackOverflow 中发现的东西,例如:android:adjustViewBounds="true"、将填充设置为零等。
谁能赐教?
Here is how the app is looking like
As you can see there is a huge space between rows
我正在通过 inflate 动态添加 imageViews,我认为这就是问题所在。
这是我的代码:
package com.example.androidvidly
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.squareup.picasso.Picasso
import org.json.JSONArray
import org.json.JSONObject
const val MOVIE_OBJECT_ID = "tokenlab.com.MOVIE_OBJECT_ID"
const val MOVIE_OBJECT_VOTES = "tokenlab.com.MOVIE_OBJECT_VOTES"
const val MOVIE_OBJECT_TITLE = "tokenlab.com.MOVIE_OBJECT_TITLE"
const val MOVIE_OBJECT_POSTER_URL = "tokenlab.com.MOVIE_OBJECT_POSTER_URL"
const val MOVIE_OBJECT_RELEASE_DATE = "tokenlab.com.MOVIE_OBJECT_RELEASE_DATE"
const val MOVIE_OBJECT_GENRES = "tokenlab.com.MOVIE_OBJECT_GENRES"
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mQueue = Volley.newRequestQueue(this)
val url = "https://desafio-mobile.nyc3.digitaloceanspaces.com/movies" //API provided
//val url = "http://10.0.2.2:3000"// Node.js Backend
val gallery = findViewById<LinearLayout>(R.id.gallery)
val inflater = LayoutInflater.from(this)
var mAppName = findViewById<TextView>(R.id.appName)
mAppName.text="Vidly"
val gson = Gson()
var request = StringRequest(Request.Method.GET, url, Response.Listener { response ->
var moviesJSONArray = JSONArray(response)
for (i in 0 until moviesJSONArray.length()) {
var e : JSONObject = moviesJSONArray.getJSONObject(i)
//Initializing the object
val arrayStringType = object : TypeToken<ArrayList<String>>() {}.type
var genreListAux:ArrayList<String> = gson.fromJson(e.getString("genres"), arrayStringType)
var movieObject: Movie = Movie(e.getString("id").toInt(), e.getString("vote_average").toDouble(), e.getString("title"), e.getString("poster_url"), genreListAux, e.getString("release_date"))
if (i==0) {
var mImageView = findViewById<ImageView>(R.id.imageDisplay)
mImageView.setTag(movieObject)
loadImageFromUrl(movieObject.poster_url, mImageView)
mImageView.setPadding(0, 0, 0, 0);
}
else if (i==1) {
var mImageView = findViewById<ImageView>(R.id.imageDisplay2)
mImageView.setTag(movieObject)
loadImageFromUrl(movieObject.poster_url, mImageView)
mImageView.setPadding(0, 0, 0, 0);
}
else{
if(i.rem(2)==0) {
var view = inflater.inflate(R.layout.activity_main, gallery, false)
var mImageView = view.findViewById<ImageView>(R.id.imageDisplay)
mImageView.setTag(movieObject)
var height = gallery.height
view.minimumHeight=height
gallery.addView(view)
loadImageFromUrl(movieObject.poster_url, mImageView)
mImageView.setPadding(0, 0, 0, 0);
}
else {
var view = gallery.getChildAt(gallery.childCount-1)
var mImageView = view.findViewById<ImageView>(R.id.imageDisplay2)
mImageView.setTag(movieObject)
loadImageFromUrl(movieObject.poster_url, mImageView)
mImageView.setPadding(0, 0, 0, 0);
}
}
}
}, Response.ErrorListener { error ->
error.printStackTrace()
})
mQueue.add(request)
}
fun showMore(view: View) {
val intent = Intent(this, MovieSelectedActivity::class.java).apply {
var movieObj : Movie = view.getTag() as Movie
putExtra(MOVIE_OBJECT_ID, movieObj.id)
putExtra(MOVIE_OBJECT_GENRES, movieObj.genres)
putExtra(MOVIE_OBJECT_POSTER_URL, movieObj.poster_url)
putExtra(MOVIE_OBJECT_RELEASE_DATE, movieObj.release_date)
putExtra(MOVIE_OBJECT_TITLE, movieObj.title)
putExtra(MOVIE_OBJECT_VOTES, movieObj.vote_average)
}
startActivity(intent)
}
fun loadImageFromUrl(url: String, mImageView: ImageView) {
Picasso.with(this).load(url).placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(
mImageView, null
)
}
}
这是视图的 xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:orientation="vertical"
android:weightSum="10"
android:adjustViewBounds="true">
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true">
<TextView
android:id="@+id/appName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="#000000"
android:textSize="60dp"
android:adjustViewBounds="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true">
<ImageView
android:id="@+id/imageDisplay"
android:layout_width="170dp"
android:layout_height="280dp"
android:clickable="true"
android:onClick="showMore"
android:adjustViewBounds="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_alignParentRight="true"
android:adjustViewBounds="true">
<ImageView
android:id="@+id/imageDisplay2"
android:layout_width="170dp"
android:layout_height="280dp"
android:clickable="true"
android:onClick="showMore"
android:adjustViewBounds="true"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
【问题讨论】:
-
您的代码似乎很复杂,并不多。您应该尝试使用 recyclerview 而不是这个;看看这个:stackoverflow.com/questions/40587168/…
-
非常感谢,我一定会调查的。
标签: java android android-studio kotlin view