RecycleView中的EditText fire focus事件

时间:2018-05-15 15:06:33

标签: xamarin.android

我在RecycleView中有一个EditText字段。

当我添加OnFocusChange事件侦听器时,当文本框获得焦点时,事件不会触发。

1 个答案:

答案 0 :(得分:0)

  

当我添加OnFocusChange事件侦听器时,当文本框获得焦点时,事件不会触发。

您可以在适配器中的OnFocusChangeListener方法中设置OnBindViewHolder()。 例如:

public class MainActivity : AppCompatActivity
{
    RecyclerView mRecyclerView;
    RecyclerView.LayoutManager mLayoutManager;
    MyAdapter mAdapter;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.SetLayoutManager(mLayoutManager);
        mAdapter = new MyAdapter(this);
        mRecyclerView.SetAdapter(mAdapter);
    }

    public class MyViewHolder : RecyclerView.ViewHolder
    {
        public EditText Box1 { get; private set; }
        public EditText Box2 { get; private set; }

        public MyViewHolder(View itemView)
            : base(itemView)
        {
            Box1 = itemView.FindViewById<EditText>(Resource.Id.Box1);
            Box2 = itemView.FindViewById<EditText>(Resource.Id.Box2);
        }
    }

    public class MyAdapter : RecyclerView.Adapter
    {
        Context context;
        public MyAdapter(Context context)
        {
            this.context = context;
        }

        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.RecyclerItem, parent, false);
            MyViewHolder vh = new MyViewHolder(itemView);
            return vh;
        }

        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            MyViewHolder vh = holder as MyViewHolder;
            var box1 = vh.ItemView.FindViewById<EditText>(Resource.Id.Box1);
            box1.OnFocusChangeListener = new MyOnFocusChangeListener(context);               
        }

        public override int ItemCount
        {
            get { return 4; }
        }
    }

    public class MyOnFocusChangeListener : Java.Lang.Object, IOnFocusChangeListener
    {
        Context context;
        public MyOnFocusChangeListener(Context context)
        {
            this.context = context;
        }
        public void OnFocusChange(View v, bool hasFocus)
        {
            Toast.MakeText(context, "FocusChanged", ToastLength.Short).Show();
        }
    }
}

Recyclerview项目的xaml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <EditText
      android:id="@+id/Box1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="ABC"
      android:textAlignment="center" />
  <EditText
      android:id="@+id/Box2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="DEF"
      android:textAlignment="center" />
</LinearLayout>