我尝试在获取API数据时显示进度条,但它从不显示。我假设我只是在错误的地方调用了进度条,但到目前为止我所做的一切都没有解决问题。以下是活动的代码。 我想要的只是在加载数据之前可以看到进度条。
package com.hmadland.paranoia
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ProgressBar
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import kotlinx.android.synthetic.main.activity_profile.*
import org.json.JSONObject
import com.android.volley.AuthFailureError
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.hmadland.paranoia.R.layout.activity_profile
class Profile : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
//get token from login
val token = intent.getStringExtra("token")
//get email to find PlayerID in sharedPref
val email = intent.getStringExtra("email")
//get PlayerID from sharedPref
val mypref = getSharedPreferences(email, Context.MODE_PRIVATE)
val PlayerID = mypref.getString(email, "")
val progressBar: ProgressBar = this.progressBar1
// task is run on a thread
Thread(Runnable {
// dummy thread mimicking some operation whose progress cannot be tracked
// display the indefinite progressbar
this@Profile.runOnUiThread(java.lang.Runnable {
progressBar1.visibility = View.VISIBLE
})
// performing operation
try {
var url = "https://elimination.azurewebsites.net/api/Players/GetPlayer?id="+ PlayerID
val que = Volley.newRequestQueue(this@Profile)
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null,
Response.Listener<JSONObject> { response ->
//get UserName
userName.text = response.get("UserName").toString()
//get profile
profile.text = response.get("Profile").toString()
//get photo
Glide.with(this).load(response.get("PhotoUrl")).into(ProfilePic)
//round photo
Glide.with(this).load(response.get("PhotoUrl")).apply(RequestOptions.circleCropTransform()).into(ProfilePic)
},
Response.ErrorListener { error -> Log.e("error is ", "" + error) }
)
//This is for Headers If Needed
@Throws(AuthFailureError::class)
fun getHeaders(): Map<String, String> {
val params = HashMap<String, String>()
params.put("Content-Type", "application/json; charset=UTF-8")
params.put("token", token)
return params
}
que.add(jsonObjectRequest)
} catch (e: InterruptedException) {
e.printStackTrace()
}
// when the task is completed, make progressBar gone
this@Profile.runOnUiThread(java.lang.Runnable {
progressBar1.visibility = View.GONE
})
}
).start()
///////////buttons to other views//////////////////////////////
//go to current target view
CurrentGame.setOnClickListener{
val intent = Intent(this, Target::class.java)
intent.putExtra("token", token)
intent.putExtra("email", email)
startActivity(intent)
}
Inventory.setOnClickListener{
val intent = Intent(this, com.hmadland.paranoia.Inventory::class.java)
startActivity(intent)
}
coming.setOnClickListener{
val intent = Intent(this, UpcomingGames::class.java)
startActivity(intent)
}
////////////////////////////////////////////////////////////////////
}
}
这是布局中的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.hmadland.paranoia.Profile">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="101dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:paddingTop="56dp"
>
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="match_parent"
android:layout_height="46dp"
android:padding="10dp"
/>
<ImageView
android:id="@+id/ProfilePic"
android:layout_width="match_parent"
android:layout_height="132dp"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:maxHeight="300px"
android:maxWidth="450px"
android:minHeight="300px"
android:minWidth="450px" />
<TextView
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:textColor="@color/colorAccent"
android:textSize="20sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="@+id/CurrentGame"
android:layout_width="90dp"
android:layout_height="80dp"
android:layout_margin="10dp"
android:background="@drawable/round"
android:fontFamily="monospace"
android:text="Target"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/Inventory"
android:layout_width="90dp"
android:layout_height="80dp"
android:layout_margin="10dp"
android:background="@drawable/round"
android:fontFamily="monospace"
android:text="Stats"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/coming"
android:layout_width="90dp"
android:layout_height="80dp"
android:layout_margin="10dp"
android:shape="oval"
android:background="@drawable/round"
android:fontFamily="monospace"
android:text="Games"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/edit"
android:layout_width="90dp"
android:layout_height="80dp"
android:layout_margin="10dp"
android:background="@drawable/round"
android:fontFamily="monospace"
android:text="Edit"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
我认为这与Volley.newRequestQueue(this@Profile)
返回RequestQueue
这一事实有关,这个{{3}}通过它的add
方法将请求排入队列。然而,add
方法立即返回,而不等待调用本身的执行。
因为在代码中,您在add
调用之后直接更改了进度条的可见性,您可能根本看不到该栏(因为整个代码会快速注意到) 。你可能想要的是隐藏Response.Listener
回调中的栏(也可能在Response.ErrorListener
中),因为这些是Volley在完成时调用的回调(错误与否)。
所以你排队请求并显示进度条,当Volley完成它会通过回调告诉你,这将是再次隐藏进度条的地方。