从onClick侦听器检索值

时间:2018-12-27 00:39:07

标签: android kotlin onclicklistener

我的项目中有margin: auto,并且正在使用此RecyclerView显示所有项目。我的问题是如何存储在RecyclerView项目中单击的名称。代码工作正常,任何帮助将不胜感激。

代码

RecyclerView

3 个答案:

答案 0 :(得分:0)

您可以在onClickListener中使用textView.setTag(xx)和getTag()

答案 1 :(得分:0)

当前形式

class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView)
{

    fun bindItem(Test:TestModel)
    {
       var TestName:TextView=itemView.findViewById<TextView>(R.id.TestName)
        TestName.text=Test.TestName

        itemView.setOnClickListener {

            val intent= Intent(itemView.context, Test::class.java)
             intent.putExtra("name",TestName.Text.toString())
            itemView.context.startActivity(intent)
                                     }
    }
}

要去的表格是

var testName:String=intent.getStringExtra("name")

或对于Java:

Intent intent = getIntent();
String name = intent.getExtraString("name")  

答案 2 :(得分:0)

Get clicked item and its position in RecyclerView,也许此链接可以解决您的问题。 据我说,您应该首选的代码:

 public abstract class TrackSelectionAdapter<VH extends TrackSelectionAdapter.ViewHolder> extends RecyclerView.Adapter<VH> {
    // Start with first item selected
    private int selectedItem = 0;

    @Override
    public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        // Handle key up and key down and attempt to move selection
        recyclerView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();

                // Return false if scrolled to the bounds and allow focus to move off the list
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                        return tryMoveSelection(lm, 1);
                    } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                        return tryMoveSelection(lm, -1);
                    }
                }

                return false;
            }
        });
    }

    private boolean tryMoveSelection(RecyclerView.LayoutManager lm, int direction) {
        int nextSelectItem = selectedItem + direction;

        // If still within valid bounds, move the selection, notify to redraw, and scroll
        if (nextSelectItem &gt;= 0 &amp;&amp; nextSelectItem &lt; getItemCount()) {
            notifyItemChanged(selectedItem);
            selectedItem = nextSelectItem;
            notifyItemChanged(selectedItem);
            lm.scrollToPosition(selectedItem);
            return true;
        }

        return false;
    }

    @Override
    public void onBindViewHolder(VH viewHolder, int position) {
        // Set selected state; use a state list drawable to style the view
        viewHolder.itemView.setSelected(selectedItem == position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);

            // Handle item click and set the selection
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Redraw the old selection and the new
                    notifyItemChanged(selectedItem);
                    selectedItem = mRecyclerView.getChildPosition(v);
                    notifyItemChanged(selectedItem);
                }
            });
        }
    }
}

注意:此代码可能不是您想要的100%,但我希望您能从中得到答案。谢谢。