如何访问 RecyclerView 位置
How to access RecyclerView position
这里是初学者。好像我在这里遗漏了一些非常基本的东西,所以任何关于它是什么的资源或一个好的解释将不胜感激。
我用 PagerSnapHelper 添加了一个 Recycler View。我左右滑动,一切正常。现在,在我 activity_main.xml 的 TextView 中,我希望该应用程序显示适配器的当前位置。
执行此操作的最佳方法是什么?
我尝试了几件事,例如。 val pozycja adaptera = recyclerView.layoutManager.findContainingItemView()
或 getAdapterPosition()
的一些奇怪实例
但我认为它们要么不适合这个场合,要么我只是用错了。
看看下面的代码:
class MainActivity : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Adding RecyclerView
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview_id)
recyclerView.run {
layoutManager = LinearLayoutManager(
this@MainActivity,
LinearLayoutManager.HORIZONTAL,
false)
adapter = MyAdapter()
// This makes mood_layout snap to grid (full screen)
PagerSnapHelper().attachToRecyclerView(this)
}
//Accessing Recycler View position
val widokPozycji = findViewById<TextView>(R.id.textView_position)
val pozycjaAdaptera = recyclerView.layoutManager.findContainingItemView()
//Adding AlertDialog to add comments
btn_addNote.setOnClickListener {
//Setting Inflater to inflate Dialog with comment_edit_text.xml layout
val dialogLayout = LayoutInflater.from(this).inflate(R.layout.comment_edit_text, null)
val builder = AlertDialog.Builder(this)
.setView(dialogLayout)
.show()
//Adding Shared Preferences to save a comment on confirmCommentButton button
builder.confirmCommentButton.setOnClickListener {
//creating instance of Shared Preferences
val pref = getSharedPreferences("commentSharedPreferences", Context.MODE_PRIVATE)
val editor = pref.edit()
//ACCESSING COMMENT WRITTEN BY USER in AlertDialog.Builder - val builder
val insertedName = builder.editTextComment.text.toString()
//Saving shared Preferences
editor.apply {
putString("STRING_KEY", insertedName)
apply()
}
// Toast to confirm saved data
Toast.makeText(this, "Comment Saved", Toast.LENGTH_SHORT).show()
//close Dialog on CONFIRM button click
builder.dismiss()
}
//CANCEL button that closes Dialog on click
builder.cancelCommentButton.setOnClickListener{
builder.dismiss()
}
builder.setOnDismissListener {
}
}
}
//HISTORY button taking user to HistoryActivity on click
fun history(view: View) {
// New Activity to open HistoryActivity
var historyActivity: Intent = Intent(applicationContext, HistoryActivity::class.java)
startActivity(historyActivity)
}
}
适配器:
class MyViewHolder(val view : View):RecyclerView.ViewHolder(view)
class MyAdapter : RecyclerView.Adapter<MyViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val moodInflater = layoutInflater.inflate(R.layout.mood_layout, parent, false)
return MyViewHolder(moodInflater)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val context = holder.view.context
val emoji = holder.view.emoji_img
val background = holder.view.moodLayout_id
val moodSelected = position
when(moodSelected){
0 -> {emoji.setImageResource(R.drawable.smiley_super_happy)
background.setBackgroundColor(context.resources.getColor(R.color.banana_yellow))
}
1 -> {emoji.setImageResource(R.drawable.smiley_happy)
background.setBackgroundColor(context.resources.getColor(R.color.light_sage))
}
2 -> {emoji.setImageResource(R.drawable.smiley_normal)
background.setBackgroundColor(context.resources.getColor(R.color.cornflower_blue_65))
}
3 -> {emoji.setImageResource(R.drawable.smiley_disappointed)
background.setBackgroundColor(context.resources.getColor(R.color.warm_grey))
}
4 -> {emoji.setImageResource(R.drawable.smiley_sad)
background.setBackgroundColor(context.resources.getColor(R.color.faded_red))
}
}
}
override fun getItemCount(): Int {
return 5
}
}
好的,所以我使用共享首选项来存储位置并能够从其他活动访问它。我把它们放在 onBindViewHolder 中。代码如下所示:
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val context = holder.view.context
val sharedPref = holder.view.context.getSharedPreferences("position", MODE_PRIVATE)
val editor = sharedPref.edit()
editor.putInt("POSITION_KEY", position)
editor.apply()
这里是初学者。好像我在这里遗漏了一些非常基本的东西,所以任何关于它是什么的资源或一个好的解释将不胜感激。
我用 PagerSnapHelper 添加了一个 Recycler View。我左右滑动,一切正常。现在,在我 activity_main.xml 的 TextView 中,我希望该应用程序显示适配器的当前位置。
执行此操作的最佳方法是什么?
我尝试了几件事,例如。 val pozycja adaptera = recyclerView.layoutManager.findContainingItemView()
或 getAdapterPosition()
的一些奇怪实例
但我认为它们要么不适合这个场合,要么我只是用错了。
看看下面的代码:
class MainActivity : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Adding RecyclerView
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview_id)
recyclerView.run {
layoutManager = LinearLayoutManager(
this@MainActivity,
LinearLayoutManager.HORIZONTAL,
false)
adapter = MyAdapter()
// This makes mood_layout snap to grid (full screen)
PagerSnapHelper().attachToRecyclerView(this)
}
//Accessing Recycler View position
val widokPozycji = findViewById<TextView>(R.id.textView_position)
val pozycjaAdaptera = recyclerView.layoutManager.findContainingItemView()
//Adding AlertDialog to add comments
btn_addNote.setOnClickListener {
//Setting Inflater to inflate Dialog with comment_edit_text.xml layout
val dialogLayout = LayoutInflater.from(this).inflate(R.layout.comment_edit_text, null)
val builder = AlertDialog.Builder(this)
.setView(dialogLayout)
.show()
//Adding Shared Preferences to save a comment on confirmCommentButton button
builder.confirmCommentButton.setOnClickListener {
//creating instance of Shared Preferences
val pref = getSharedPreferences("commentSharedPreferences", Context.MODE_PRIVATE)
val editor = pref.edit()
//ACCESSING COMMENT WRITTEN BY USER in AlertDialog.Builder - val builder
val insertedName = builder.editTextComment.text.toString()
//Saving shared Preferences
editor.apply {
putString("STRING_KEY", insertedName)
apply()
}
// Toast to confirm saved data
Toast.makeText(this, "Comment Saved", Toast.LENGTH_SHORT).show()
//close Dialog on CONFIRM button click
builder.dismiss()
}
//CANCEL button that closes Dialog on click
builder.cancelCommentButton.setOnClickListener{
builder.dismiss()
}
builder.setOnDismissListener {
}
}
}
//HISTORY button taking user to HistoryActivity on click
fun history(view: View) {
// New Activity to open HistoryActivity
var historyActivity: Intent = Intent(applicationContext, HistoryActivity::class.java)
startActivity(historyActivity)
}
}
适配器:
class MyViewHolder(val view : View):RecyclerView.ViewHolder(view)
class MyAdapter : RecyclerView.Adapter<MyViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val moodInflater = layoutInflater.inflate(R.layout.mood_layout, parent, false)
return MyViewHolder(moodInflater)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val context = holder.view.context
val emoji = holder.view.emoji_img
val background = holder.view.moodLayout_id
val moodSelected = position
when(moodSelected){
0 -> {emoji.setImageResource(R.drawable.smiley_super_happy)
background.setBackgroundColor(context.resources.getColor(R.color.banana_yellow))
}
1 -> {emoji.setImageResource(R.drawable.smiley_happy)
background.setBackgroundColor(context.resources.getColor(R.color.light_sage))
}
2 -> {emoji.setImageResource(R.drawable.smiley_normal)
background.setBackgroundColor(context.resources.getColor(R.color.cornflower_blue_65))
}
3 -> {emoji.setImageResource(R.drawable.smiley_disappointed)
background.setBackgroundColor(context.resources.getColor(R.color.warm_grey))
}
4 -> {emoji.setImageResource(R.drawable.smiley_sad)
background.setBackgroundColor(context.resources.getColor(R.color.faded_red))
}
}
}
override fun getItemCount(): Int {
return 5
}
}
好的,所以我使用共享首选项来存储位置并能够从其他活动访问它。我把它们放在 onBindViewHolder 中。代码如下所示:
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val context = holder.view.context
val sharedPref = holder.view.context.getSharedPreferences("position", MODE_PRIVATE)
val editor = sharedPref.edit()
editor.putInt("POSITION_KEY", position)
editor.apply()