为什么我的 recyclerview 没有显示在我的选项卡布局片段上?
Why is my recyclerview not showing on my tab layout fragment?
所以我正在尝试显示 GitHub 的 API 中的以下内容和关注者回收视图。所以有两个 fragments for 2 tablayouts for followers and following.
这是我的 FollowingFragment 的代码
class FollowingFragment : Fragment() {
private lateinit var binding: FragmentFollowingBinding
private var list = ArrayList<Users>()
companion object {
private val ARG_USERNAME = "extra_username"
fun newInstance(username:String):Fragment{
var fragment = FollowingFragment()
val bundle = Bundle()
bundle.putString(ARG_USERNAME,username)
fragment.arguments = bundle
return fragment
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val username = arguments?.getString(ARG_USERNAME)
setListUser(username)
binding.rvFollowing.setHasFixedSize(true)
showRecyclerList()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentFollowingBinding.inflate(inflater,container,false)
val view: View = binding.root
return view
}
private fun showRecyclerList() {
binding.rvFollowing.layoutManager = LinearLayoutManager(activity)
val listUserAdapter = FollowingAdapter(list)
binding.rvFollowing.adapter = listUserAdapter
}
private fun setListUser(username: String?) {
val listItems = ArrayList<Users>()
val token = "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"
val url = "https://api.github.com/users/$username/following"
val client = AsyncHttpClient()
client.addHeader("Authorization","token $token")
client.addHeader("User-Agent","request")
client.get(url,object: AsyncHttpResponseHandler(){
override fun onSuccess(statusCode: Int, headers: Array<Header>, responseBody: ByteArray) {
//parsingjson
try {
val result = String(responseBody)
val items = JSONArray(result)
for (i in 0 until items.length()){
val UserClass = Users()
val user = items.getJSONObject(i)
val username = user.getString("login")
val avatar = user.getString("avatar_url")
UserClass.login = username
UserClass.avatar = avatar
listItems.add(UserClass)
Log.d("TAG","$username $avatar")
}
list = listItems
} catch (e: Exception){
Log.d("Exception",e.message.toString())
}
}
override fun onFailure(statusCode: Int, headers: Array<Header>, responseBody: ByteArray, error: Throwable) {
Log.d("onFailure",error.message.toString())
}
})
}
}
这是我的以下 recyclerview 适配器的代码:
class FollowingAdapter(private val listFollowing: ArrayList<Users>): RecyclerView.Adapter<FollowingAdapter.UserViewHolder>() {
class UserViewHolder(private val binding:ItemRowDeveloperProfilesBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(user:Users){
with(binding){
Glide.with(itemView.context)
.load(user.avatar)
.apply(RequestOptions().override(400,400))
.into(imgProfilePictureSearch)
usernameSearch.text = user.login
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
val binding = ItemRowDeveloperProfilesBinding.inflate(LayoutInflater.from(parent.context),parent,false)
return UserViewHolder(binding)
}
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
holder.bind(listFollowing[position])
}
override fun getItemCount(): Int {
return listFollowing.size
}
}
可以在我的 github 存储库 https://github.com/briancatraguna/GitHubClone 上查看完整代码。我真的不确定我在这里做错了什么?谁能帮帮我?
您在从 api 调用获取列表之前设置 listItems(对于您的搜索用户页面也是如此)。
将您的 showRecyclerView() 移动到 AsyncHttpClient get Call 的 onSuccess 回调中..
所以我正在尝试显示 GitHub 的 API 中的以下内容和关注者回收视图。所以有两个 fragments for 2 tablayouts for followers and following.
这是我的 FollowingFragment 的代码
class FollowingFragment : Fragment() {
private lateinit var binding: FragmentFollowingBinding
private var list = ArrayList<Users>()
companion object {
private val ARG_USERNAME = "extra_username"
fun newInstance(username:String):Fragment{
var fragment = FollowingFragment()
val bundle = Bundle()
bundle.putString(ARG_USERNAME,username)
fragment.arguments = bundle
return fragment
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val username = arguments?.getString(ARG_USERNAME)
setListUser(username)
binding.rvFollowing.setHasFixedSize(true)
showRecyclerList()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentFollowingBinding.inflate(inflater,container,false)
val view: View = binding.root
return view
}
private fun showRecyclerList() {
binding.rvFollowing.layoutManager = LinearLayoutManager(activity)
val listUserAdapter = FollowingAdapter(list)
binding.rvFollowing.adapter = listUserAdapter
}
private fun setListUser(username: String?) {
val listItems = ArrayList<Users>()
val token = "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"
val url = "https://api.github.com/users/$username/following"
val client = AsyncHttpClient()
client.addHeader("Authorization","token $token")
client.addHeader("User-Agent","request")
client.get(url,object: AsyncHttpResponseHandler(){
override fun onSuccess(statusCode: Int, headers: Array<Header>, responseBody: ByteArray) {
//parsingjson
try {
val result = String(responseBody)
val items = JSONArray(result)
for (i in 0 until items.length()){
val UserClass = Users()
val user = items.getJSONObject(i)
val username = user.getString("login")
val avatar = user.getString("avatar_url")
UserClass.login = username
UserClass.avatar = avatar
listItems.add(UserClass)
Log.d("TAG","$username $avatar")
}
list = listItems
} catch (e: Exception){
Log.d("Exception",e.message.toString())
}
}
override fun onFailure(statusCode: Int, headers: Array<Header>, responseBody: ByteArray, error: Throwable) {
Log.d("onFailure",error.message.toString())
}
})
}
}
这是我的以下 recyclerview 适配器的代码:
class FollowingAdapter(private val listFollowing: ArrayList<Users>): RecyclerView.Adapter<FollowingAdapter.UserViewHolder>() {
class UserViewHolder(private val binding:ItemRowDeveloperProfilesBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(user:Users){
with(binding){
Glide.with(itemView.context)
.load(user.avatar)
.apply(RequestOptions().override(400,400))
.into(imgProfilePictureSearch)
usernameSearch.text = user.login
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
val binding = ItemRowDeveloperProfilesBinding.inflate(LayoutInflater.from(parent.context),parent,false)
return UserViewHolder(binding)
}
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
holder.bind(listFollowing[position])
}
override fun getItemCount(): Int {
return listFollowing.size
}
}
可以在我的 github 存储库 https://github.com/briancatraguna/GitHubClone 上查看完整代码。我真的不确定我在这里做错了什么?谁能帮帮我?
您在从 api 调用获取列表之前设置 listItems(对于您的搜索用户页面也是如此)。
将您的 showRecyclerView() 移动到 AsyncHttpClient get Call 的 onSuccess 回调中..