我正在尝试在onClick方法中使用Intent,Intent确实可以工作...但它不想更改为其他Activity,我正在到处查找,但似乎没有任何错误。注意:我不知道为什么FragmentPagerAdapter被弃用
密码:
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var viewModel: MainViewModel
private lateinit var adapter: UserAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
adapter = UserAdapter()
adapter.notifyDataSetChanged()
adapter.setOnItemClickCallback(object : UserAdapter.OnItemClickCallback{
override fun onItemClicked(data: User) {
Intent(this@MainActivity, DetailUserActivity::class.java).also{
intent.putExtra(DetailUserActivity.EXTRA_USERNAME, data.login)
startActivity(intent)
}
}
})
viewModel= ViewModelProvider(this, ViewModelProvider.NewInstanceFactory())[MainViewModel::class.java]
binding.apply {
rvUser.layoutManager = LinearLayoutManager ( this@MainActivity)
rvUser.setHasFixedSize(true)
rvUser.adapter = adapter
btnSearch.setOnClickListener{
searchUser()
}
etQuery.setOnKeyListener { _, KeyCode, event ->
if (event.action == KeyEvent.ACTION_DOWN && KeyCode == KeyEvent.KEYCODE_ENTER){
searchUser()
return@setOnKeyListener true
}
return@setOnKeyListener false
}
}
viewModel.getSearchUsers().observe(this) {
if (it != null) {
adapter.setList(it)
showLoading(false)
}
}
}
private fun searchUser(){
binding.apply {
val query = etQuery.text.toString()
if (query.isEmpty()) return
showLoading(true)
viewModel.setSearchUsers(query)
}
}
private fun showLoading(state: Boolean){
if (state){
binding.progressBar.visibility = View.VISIBLE
}else{
binding.progressBar.visibility = View.GONE
}
}
}
UserAdapter.kt
class UserAdapter : RecyclerView.Adapter<UserAdapter.UserViewHolder>() {
private val list = ArrayList<User>()
private var onItemClickCallback: OnItemClickCallback? = null
fun setOnItemClickCallback (onItemClickCallback: OnItemClickCallback){
this.onItemClickCallback = onItemClickCallback
}
fun setList(users: ArrayList<User>){
list.clear()
list.addAll(users)
notifyDataSetChanged()
}
inner class UserViewHolder(private val binding: ItemUserBinding) : RecyclerView.ViewHolder(binding.root){
fun bind(user: User){
binding.root.setOnClickListener{
onItemClickCallback?.onItemClicked(list[adapterPosition])
}
binding.apply {
Glide.with(itemView)
.load(user.avatar_url)
.transition(DrawableTransitionOptions.withCrossFade())
.centerCrop()
.into(tvUser)
tvUsername.text = user.login
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
val view = ItemUserBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return UserViewHolder((view))
}
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
holder.bind(list[position])
}
override fun getItemCount(): Int = list.size
interface OnItemClickCallback{
fun onItemClicked(data: User)
}
}
DetailUserAdapter.kt
class DetailUserViewModel: ViewModel() {
val user= MutableLiveData<DetailUserResponse>()
fun setUserDetail(username: String){
RetrofitClient.apiInstance
.getUserDetail(username)
.enqueue(object : Callback<DetailUserResponse>{
override fun onResponse(
call: Call<DetailUserResponse>,
response: Response<DetailUserResponse>
) {
if (response.isSuccessful){
user.postValue(response.body())
}
}
override fun onFailure(call: Call<DetailUserResponse>, t: Throwable) {
t.message?.let { Log.d("Failure", it) }
}
})
}
fun getUserDetail(): LiveData<DetailUserResponse>{
return user
}
}
SectionPagerAdapter.kt
class SectionPagerAdapter(private val mCtx: Context, fm: FragmentManager) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
@StringRes
private val TAB_TITLES = IntArray(R.string.tab_1)
override fun getCount(): Int = 2
override fun getItem(position: Int): Fragment {
var fragment: Fragment? = null
when(position){
0 -> fragment = FollowerFragment()
1 -> fragment = FollowingFragment()
}
return fragment as Fragment
}
override fun getPageTitle(position: Int): CharSequence? {
return mCtx.resources.getString(TAB_TITLES[position])
}
}
1条答案
按热度按时间bq8i3lrv1#
这里的代码有一个小问题:
这里您使用的是
.also
,它将您创建的Intent传递为it
,但是您使用intent
来启动Activity,这是您在Activity中获得的Intent,您可以使用getIntent()
获得相同的Intent,这导致了这里的问题。请将其更改为类似的内容,然后尝试:
或者可以类似于:
谢谢你!