android - 为什么我的 recyclerview 没有显示在我的选项卡布局片段上?
问题描述
所以我试图从 GitHub 的 API 中显示以下内容和关注者 recyclerview。因此,关注者和关注者的 2 个标签布局有两个片段。
这是我的 FollowFragment 的代码
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 回调中。
推荐阅读
- python - 面临错误:“TypeError:'int'对象不可调用”
- amazon-web-services - 如何将亚马逊机器学习服务与 Ionic-5 Angular 应用程序集成?
- keras - 使用 LSTM 根据不同的年度目标数据预测每周结果
- ios - Xamarin iOS WebView 为空白
- javascript - 如何将javascript中的变量获取到python文件?
- express - 如何在本地护照会话中传递多个参数?
- razor-pages - Razor pages - 包括模型之外的附加参数
- android-fragments - 如何在片段中添加、向上导航?
- javascript - Highcharts.map 折旧警告
- django - 当我关闭 CsrfViewMiddleware 时,Dajngo 会设置 CSRF cookie。并且不按预期设置 CSRF cookie