安卓系统中的保存/恢复改装2响应



我有一个连接到GitHub API的应用程序。它加载并显示某个用户的存储库列表。当我点击repositorium的名称时,我会用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") }
}

我找到了一个解决方案。

我的新简历代码:

override fun onResume() {
super.onResume()
repos?.clone()?.enqueue(obj)
recyclerClick()
}

相关内容

  • 没有找到相关文章

最新更新