我有一个连接到GitHub API的应用程序。它加载并显示某些用户的存储库列表。当我单击存储库名称时,我使用WebView切换到另一个片段。当我返回列表时,列表中有项目,但它们只是一个字符串项目,不可单击。如何保存翻新响应并由此刷新列表?
片段,在其中显示列表:
class MainFragment : Fragment() {
lateinit var mRecyclerView: RecyclerView
lateinit var name:String
lateinit var service: GitHubService
var responseSave:List<GitHubPOJO> = ArrayList()
var posts: MutableList<GitHubPOJO> = ArrayList()
private lateinit var btn:Button
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_main, container, false)
btn = view.findViewById(R.id.button)
btn.setOnClickListener (onClick)
return view
}
val onClick = View.OnClickListener {
posts = ArrayList()
mRecyclerView = view!!.findViewById(R.id.posts_recycle_view)
val layoutManager = LinearLayoutManager(this.activity!!)
mRecyclerView.layoutManager = layoutManager
val adapter = PostsAdapter(posts)
mRecyclerView.adapter = adapter
hideKeyboard()
name = view!!.findViewById<EditText>(R.id.txt_user_name).text.toString()
if (name.isEmpty()){
Toast.makeText(activity!!,"You do not entered User's name",Toast.LENGTH_LONG).show()
};else{
if (isInternet()) {
serverConnect()
recyclerClick()
};else {
Toast.makeText(activity!!,"Please, check your internet connection!",Toast.LENGTH_LONG).show()
}
}
}
private fun serverConnect(){
service = Retrofit.Builder()
.baseUrl("https://api.github.com/") // CHANGE API
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(GitHubService::class.java)
service.retrieveRepositories(name)
.enqueue(object : Callback<List<GitHubPOJO>> {
override fun onResponse(call: Call<List<GitHubPOJO>>, response: Response<List<GitHubPOJO>>) {
responseSave = response.body()!!
posts.addAll(responseSave)
mRecyclerView.adapter?.notifyDataSetChanged()
response.body()?.forEach { println("TAG_: $it") }
}
override fun onFailure(call: Call<List<GitHubPOJO>>, t: Throwable) {
}
})
}
private fun recyclerClick(){
mRecyclerView.addOnItemTouchListener(ClickListener(this.activity!!, mRecyclerView, object : ClickListener.OnItemClickListener {
override fun onLongItemClick(view: View?, position: Int) {
}
override fun onItemClick(view: View, position: Int) {
val url = posts[position].htmlUrl
println("URL = $url")
view.findNavController().navigate(MainFragmentDirections.actionMainFragmentToWebFragment(url))
}
}
)
)
}
@Suppress("DEPRECATION")
fun isInternet():Boolean{
val cm = context!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = cm.activeNetworkInfo
return activeNetwork!=null
}
fun hideKeyboard(){
val inputMethodManager = this.activity!!.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
if (activity!!.currentFocus != null) {
inputMethodManager.hideSoftInputFromWindow(this.activity!!.currentFocus!!.windowToken, 0)
}
}
我有我的 onResume 代码。它可以显示一个列表,但列表项不可单击。
override fun onResume() {
super.onResume()
mRecyclerView = view!!.findViewById(R.id.posts_recycle_view)
val layoutManager = LinearLayoutManager(this.activity!!)
mRecyclerView.layoutManager = layoutManager
val adapter = PostsAdapter(posts)
mRecyclerView.adapter = adapter
adapter.updateAdapterList(responseSave.toMutableList())
println("RESUME")
responseSave.forEach { println("TAG_: $it") }
}
答案 0 :(得分:0)
我找到了解决方法。
我的新 onResume 代码:
override fun onResume() {
super.onResume()
repos?.clone()?.enqueue(obj)
recyclerClick()
}