Android 如何访问RecyclerView位置

Android 如何访问RecyclerView位置,android,android-recyclerview,position,Android,Android Recyclerview,Position,这里完全是初学者。似乎我在这里遗漏了一些非常基本的东西,所以任何关于它的资源或一个好的解释都将不胜感激 我用PagerSnapHelper添加了一个Recycler视图。我左右滑动,一切正常。现在,在我的activity_main.xml的文本视图中,我希望应用程序显示适配器的当前位置 最好的方法是什么 我尝试了两种方法,例如val pozycja adaptera=recyclerView.layoutManager.findContainingItemView()或一些奇怪的getAdapt

这里完全是初学者。似乎我在这里遗漏了一些非常基本的东西,所以任何关于它的资源或一个好的解释都将不胜感激

我用PagerSnapHelper添加了一个Recycler视图。我左右滑动,一切正常。现在,在我的activity_main.xml的文本视图中,我希望应用程序显示适配器的当前位置

最好的方法是什么

我尝试了两种方法,例如
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 MainActivity:AppCompatActivity(){
@RequiresApi(Build.VERSION\u code.O)
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//添加RecyclerView
val recyclerView=findviewbyd(R.id.recyclerView\u id)
循环视图{
layoutManager=LinearLayoutManager(
this@MainActivity,
LinearLayoutManager.HORIZONTAL,
(错误)
adapter=MyAdapter()
//这使mood_布局捕捉到网格(全屏)
PagerSnapHelper().attachToRecyclerView(此)
}
//访问回收器视图位置
val widokPozycji=findViewById(R.id.textView\u位置)
val pozycjaAdaptera=recyclerView.layoutManager.findContainingItemView()
//添加AlertDialog以添加注释
btn_addNote.setOnClickListener{
//将充气器设置为使用comment_edit_text.xml布局充气对话框
val dialogLayout=LayoutInflater.from(this).充气(R.layout.comment\u edit\u text,null)
val builder=AlertDialog.builder(此)
.setView(对话框布局)
.show()
//添加共享首选项以在confirmCommentButton按钮上保存注释
builder.confirmCommentButton.setOnClickListener{
//创建共享首选项的实例
val pref=getSharedReferences(“CommentSharedReferences”,Context.MODE\u PRIVATE)
val editor=pref.edit()
//访问用户在AlertDialog.Builder-val Builder中编写的注释
val insertedName=builder.editTextComment.text.toString()
//保存共享首选项
editor.apply{
putString(“字符串_键”,insertedName)
应用()
}
//Toast以确认保存的数据
Toast.makeText(此“注释已保存”,Toast.LENGTH\u SHORT.show())
//单击“确认”按钮关闭对话框
解雇
}
//单击关闭对话框的取消按钮
builder.cancelCommentButton.setOnClickListener{
解雇
}
builder.setOnDismissListener{
}
}
}
//单击“历史记录”按钮将用户带到历史活动
趣味历史(查看:查看){
//打开HistoryActivity的新活动
var historyActivity:Intent=Intent(applicationContext,historyActivity::class.java)
星触觉(历史活动)
}
}
适配器:

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
}
}
类MyViewHolder(val视图:视图):RecyclerView.ViewHolder(视图)
类MyAdapter:RecyclerView.Adapter(){
重写CreateViewHolder(父级:ViewGroup,viewType:Int):MyViewHolder{
val layoutInflater=layoutInflater.from(parent.context)
val moodInflater=layoutInflater.flate(R.layout.mood_布局,父级,false)
返回MyViewHolder(moodInflater)
}
覆盖onBindViewHolder(holder:MyViewHolder,位置:Int){
val context=holder.view.context
val emoji=holder.view.emoji\u img
val background=holder.view.moodLayout\u id
val moodSelected=位置
何时(moodSelected){
0->{emoji.setImageResource(R.drawable.smiley\u super\u happy)
backgroundColor(context.resources.getColor(R.color.banana_yellow))
}
1->{emoji.setImageResource(R.drawable.smiley_happy)
backgroundColor(context.resources.getColor(R.color.light_-sage))
}
2->{emoji.setImageResource(R.drawable.smiley_normal)
backgroundColor(context.resources.getColor(R.color.cornflower\u blue\u 65))
}
3->{emoji.setImageResource(R.drawable.smiley_)
backgroundColor(context.resources.getColor(R.color.warm\u grey))
}
4->{emoji.setImageResource(R.drawable.smiley_sad)
backgroundColor(context.resources.getColor(R.color.flated_red))
}
}
}
重写getItemCount():Int{
返回5
}
}

好的,所以我使用共享首选项来存储位置,并能够从其他活动中访问它。我把它们放在文件夹里。代码如下所示:

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()