我刚开始学习Kotlin,也许对于高级开发人员来说这是很容易的。我在navigationview上有标题,这个标题有图像,图像有ID。我想当我点击这个图像时,打开新的活动,但是对于这个,我需要这个图像ID并添加onClickListener。我如何从navigationview标题中获取这个图像ID?抱歉,错误,英语不是我的第一语言。image
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
lateinit var binding: ActivityMainBinding
var adapter: MyAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.navView.setNavigationItemSelectedListener(this)
binding.navView.getHeaderView()
binding.navView.header
var list = ArrayList<ListItem>()
list.addAll(fillArrays(getImageId(R.array.imAvatar),
resources.getStringArray(R.array.tvName),resources.getStringArray(R.array.tvPosition),
resources.getStringArray(R.array.tvNumber),getImageId(R.array.imFlag),
resources.getStringArray(R.array.tvInfo),getImageId(R.array.imInfo),
resources.getStringArray(R.array.tvInfo2),getImageId(R.array.imInfo2),
resources.getStringArray(R.array.tvInfo3),getImageId(R.array.imInfo3),
resources.getStringArray(R.array.tvInfo4),getImageId(R.array.imInfo4),
resources.getStringArray(R.array.tvInfo5),getImageId(R.array.imInfo5)))
findViewById<RecyclerView>(R.id.rcView).hasFixedSize()
findViewById<RecyclerView>(R.id.rcView).layoutManager = LinearLayoutManager(this)
adapter = MyAdapter(list, this)
findViewById<RecyclerView>(R.id.rcView).adapter = adapter
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.id_result -> {
Toast.makeText(this, "3-3 (6-5)", Toast.LENGTH_SHORT).show()
startActivity(Intent(this, ActivityResult::class.java))
}
R.id.id_liverpool -> {
Toast.makeText(this, "Gerrard",Toast.LENGTH_SHORT).show()
adapter?.updateAdapter(fillArrays(getImageId(R.array.imAvatar),
resources.getStringArray(R.array.tvName),resources.getStringArray(R.array.tvPosition),
resources.getStringArray(R.array.tvNumber),getImageId(R.array.imFlag),
resources.getStringArray(R.array.tvInfo),getImageId(R.array.imInfo),
resources.getStringArray(R.array.tvInfo2),getImageId(R.array.imInfo2),
resources.getStringArray(R.array.tvInfo3),getImageId(R.array.imInfo3),
resources.getStringArray(R.array.tvInfo4),getImageId(R.array.imInfo4),
resources.getStringArray(R.array.tvInfo5),getImageId(R.array.imInfo5)))
}
R.id.id_milan -> {
Toast.makeText(this, "Maldini",Toast.LENGTH_SHORT).show()
adapter?.updateAdapter(fillArrays(getImageId(R.array.imAvatarM),
resources.getStringArray(R.array.tvNameM),resources.getStringArray(R.array.tvPositionM),
resources.getStringArray(R.array.tvNumberM),getImageId(R.array.imFlagM),
resources.getStringArray(R.array.tvInfoM),getImageId(R.array.imInfoM),
resources.getStringArray(R.array.tvInfo2M),getImageId(R.array.imInfo2M),
resources.getStringArray(R.array.tvInfo3M),getImageId(R.array.imInfo3M),
resources.getStringArray(R.array.tvInfo4M),getImageId(R.array.imInfo4M),
resources.getStringArray(R.array.tvInfo5M),getImageId(R.array.imInfo5M)))
}
}
binding.drawerLayout.closeDrawer(GravityCompat.START)
return true
}
fun fillArrays(imAvatar:IntArray,tvName:Array<String>,tvPosition:Array<String>,tvNumber:Array<String>,imFlag:IntArray,tvInfo:Array<String>,imInfo:IntArray,tvInfo2:Array<String>,imInfo2:IntArray,tvInfo3:Array<String>,imInfo3:IntArray,tvInfo4:Array<String>,imInfo4:IntArray,tvInfo5:Array<String>,imInfo5:IntArray):List<ListItem>
{
var listItemArray = ArrayList<ListItem>()
for(n in 0..tvName.size -1)
{
var listItem = ListItem(imAvatar[n],tvName[n],tvPosition[n],tvNumber[n],imFlag[n],tvInfo[n], imInfo[n],tvInfo2[n], imInfo2[n],tvInfo3[n], imInfo3[n],tvInfo4[n], imInfo4[n],tvInfo5[n], imInfo5[n])
listItemArray.add(listItem)
}
return listItemArray
}
fun getImageId(imageArrayId:Int):IntArray
{
var tArray:TypedArray = resources.obtainTypedArray(imageArrayId)
val count = tArray.length()
val ids = IntArray(count)
for (i in ids.indices)
{
ids[i] = tArray.getResourceId(i, 0)
}
tArray.recycle()
return ids
}
}
布局:
android:layout_width="match_parent"
android:layout_height="@dimen/header_main_height"
android:background="@color/liverpool_red">
<ImageView
android:id="@+id/imHeader"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/champions_league_logo" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="text"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="@+id/imHeader"
app:layout_constraintStart_toStartOf="@+id/imHeader"
app:layout_constraintTop_toBottomOf="@+id/imHeader" />
1条答案
按热度按时间xmakbtuz1#
你必须先获取headerView,然后找到你的图像id并设置点击监听器。