点击RecyclerView

时间:2019-06-14 12:59:41

标签: android kotlin

我目前正在将猜谜游戏作为学习过程的一部分。在我的应用中,您基本上必须猜出三个正确的定义。 TextView提供了一个单词RecyclerView-三个选项。单词来自文本文件。

点击后,我已经学会了如何更改RecyclerViewTextView中的单词也必须更改,但是我无法从Adapter类中访问它。

enter image description here

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var listNames = mutableListOf<String>()

        listMain.layoutManager = GridLayoutManager (this,3,1,false) as RecyclerView.LayoutManager?
        listMain.adapter = ItemAdapter(listNames)

        readDictionaryFile(listNames)
        wordGuess.text = listNames[0]
    }



    private fun readDictionaryFile(listNames: MutableList<String>) {
        val reader = Scanner(resources.openRawResource(R.raw.test))
        while (reader.hasNextLine()) {
            val line = reader.nextLine()
            val split = line.split(";")
            listNames.add(split[0])
        }
    }



class ItemAdapter(private val listNames: MutableList<String>): RecyclerView.Adapter<CustomViewHolder>() {

    //number of items
    override fun getItemCount(): Int {
        return listNames.size
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): CustomViewHolder {
        val layoutInflater = LayoutInflater.from(p0.context)
        val cellForRow = layoutInflater.inflate(R.layout.list_layout, p0, false )
        return CustomViewHolder(cellForRow)
    }

    override fun onBindViewHolder(p0: CustomViewHolder, p1: Int) {
        val listTitle = listNames.get(p1)
        p0.view.listNamesView.text = listTitle
        p0.view.setOnClickListener {
            //do something
            notifyDataSetChanged()
        }

    }

2 个答案:

答案 0 :(得分:1)

您可以这样做:

在适配器中声明接口:

public interface MyInterface{
public void foo();
}

让Activity实施它:

public class MyActivity extends Activity implements MyInterface{
public void foo(){
    //do stuff
   }
}

然后将您的活动传递给RecyclerAdapter:

public MyAdapter extends BaseAdater{
private MyInterface listener;

public MyAdapter(MyInterface listener){
    this.listener = listener;
    }
}

在适配器中的某处,当您需要调用该Activity方法时:

listener.foo();

此答案基于this问题。

答案 1 :(得分:0)

将回调传递给适配器。

class ItemAdapter(private val listNames: MutableList<String>, private val onNameClicked: (String) -> Unit): RecyclerView.Adapter<CustomViewHolder>() {

    //number of items
    override fun getItemCount(): Int {
        return listNames.size
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): CustomViewHolder {
        val layoutInflater = LayoutInflater.from(p0.context)
        val cellForRow = layoutInflater.inflate(R.layout.list_layout, p0, false )
        return CustomViewHolder(cellForRow)
    }

    override fun onBindViewHolder(p0: CustomViewHolder, p1: Int) {
        val listTitle = listNames.get(p1)
        p0.view.listNamesView.text = listTitle
        p0.view.setOnClickListener {
            //do something
            onNameClicked(listTitle)
            notifyDataSetChanged()
        }

    }
}

在您的MainActivity中,

listMain.adapter = ItemAdapter(listNames) { name ->
    // Update your TextView or something
}

另一方面,我看到适配器中的列表是可变的。我建议使其不可变,因为修改适配器数据集并且不调用notifyDataSetChanged()容易产生错误,不是一种好习惯。