适配器文件
class RecyclerViewAdapter(val context: Context) :
RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>() {
var itemList: List<Photo>? = null
private lateinit var viewModel: MainActivityViewModel
fun setData(it: List<Photo>) {
itemList = it
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerViewAdapter.MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_gallery, parent, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: RecyclerViewAdapter.MyViewHolder, position: Int) {
val photo = itemList?.get(position)
Glide.with(holder.imggallery).load(photo?.url_s).into(holder.imggallery)
val arg = Bundle()
arg.putString("url", photo?.url_s.toString())
holder.imggallery.setOnClickListener {
Navigation.findNavController(it)
.navigate(R.id.action_recyclerMainFragment_to_bigPictureFragment)
}
}
override fun getItemCount(): Int {
return itemList?.size ?: 1
}
fun setViewModel(viewModel: MainActivityViewModel) {
this.viewModel = viewModel
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val imggallery = view.findViewById<ImageView>(R.id.imgItem)
}
}
}
字符串
RecyclerMain片段
@AndroidEntryPoint
class RecyclerMainFragment : Fragment(R.layout.fragment_recycler_main) {
lateinit var recyclerView: RecyclerView
lateinit var recyclerViewAdapter: RecyclerViewAdapter
lateinit var navController : NavController
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_recycler_main, container, false)
recyclerView = view.findViewById(R.id.recyclerHome)
initViewModel()
initRecyclerView()
return view
}
private fun initRecyclerView() {
recyclerView.layoutManager =
StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
recyclerViewAdapter = RecyclerViewAdapter(context as Context)
recyclerView.adapter = recyclerViewAdapter
}
private fun initViewModel() {
val viewModel = ViewModelProvider(this).get(MainActivityViewModel::class.java)
viewModel.getLiveDataObserver().observe(viewLifecycleOwner, {
if (it != null) {
recyclerViewAdapter.setData(it)
recyclerViewAdapter.setViewModel(viewModel)
recyclerViewAdapter.notifyDataSetChanged()
} else {
Toast.makeText(context, "ERROR0", Toast.LENGTH_LONG).show()
}
})
viewModel.loadlistofData()
}
}
型
主要活动
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
lateinit var drawerLayout: DrawerLayout
lateinit var coordinatorLayout: CoordinatorLayout
lateinit var toolbar: androidx.appcompat.widget.Toolbar
lateinit var navigationView: NavigationView
lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
drawerLayout = findViewById(R.id.drawerLayout)
coordinatorLayout = findViewById(R.id.coordinatorLayout)
toolbar = findViewById(R.id.toolbar)
navigationView = findViewById(R.id.navView)
setSupportActionBar(toolbar)
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
var bundle = Bundle()
replaceFragment(RecyclerMainFragment(),"Gallery",bundle )
val actionBarDrawerToggle = ActionBarDrawerToggle(
this@MainActivity,
drawerLayout,
R.string.open_drawer,
R.string.close_drawer
)
drawerLayout.addDrawerListener(actionBarDrawerToggle)
actionBarDrawerToggle.syncState()
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == android.R.id.home) {
drawerLayout.openDrawer((GravityCompat.START))
}
return super.onOptionsItemSelected(item)
}
fun replaceFragment(fragment: Fragment, tag: String , arg: Bundle) {
fragment.arguments=arg
supportFragmentManager.beginTransaction()
.replace(R.id.frameLayout, fragment).addToBackStack(null).commit()
supportActionBar?.title=tag
}
fun navController(): NavController {
return navController
}
}
型
我想从适配器类中的imggallery click导航到BigsizepictureFragment()。我确实尝试了Navigation.findnavcontroller().navigate(idof action),但它给出了navcontroller设置的错误youtube channels正在声明使用绑定,我现在不知道。我任何人都可以帮助它将对我有帮助。
1条答案
按热度按时间ifmq2ha21#
以下是您需要采取的步骤:
1.在viewModel中定义onItemClick函数
1.在适配器中定义的onClickListener中调用此onItemClick方法。
1.在onItemClick方法中,更新一个livedata对象的值,该对象指示您需要导航到的页面。
1.观察片段中的livedata对象,并在观察者中添加适当的导航。
希望这能帮助任何仍在努力解决这个问题的人。