【问题标题】:RecyclerView issues: EditText loses focusRecyclerView 问题:EditText 失去焦点
【发布时间】:2021-10-26 18:24:15
【问题描述】:

我在RecyclerView 中添加了一些EditText,因为我需要获取一些值。实现是这样的:

<android.support.v7.widget.RecyclerView
    android:layout_below="@id/firstrow"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    android:descendantFocusability="beforeDescendants"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/rectable"
    android:layout_marginLeft="@dimen/table_margin"
    android:layout_marginRight="@dimen/table_margin"
    android:layout_marginBottom="300dp" />

这是带有一些editText的自定义xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="60dp ">


<TextView
    android:text="TextView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/description"
    android:layout_weight="1"
    android:textColor="@color/black" />

<View
    style="@style/VerticalSeparator" />


<EditText
    android:hint="lol"
    android:id="@+id/weight"
    android:focusable="true"
    style="@style/DefaultEditCellStyle" />

<View
    style="@style/VerticalSeparator" />

<EditText
    android:hint="lol"
    android:id="@+id/arm"
    android:focusable="true"
    style="@style/DefaultEditCellStyle" />

<View
    style="@style/VerticalSeparator" />


<EditText
    android:hint="lol"
    android:focusable="true"
    android:id="@+id/moment"
    style="@style/DefaultEditCellStyle" />

实际上,当我单击EditText 时,它会打开键盘,失去焦点并立即关闭键盘,因此无法在其中写入。我也尝试阅读其他问题,这样说:

recyclerView.setFocusable(true);
        recyclerView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        recyclerView.setAdapter(adapter);

但是,与listview 不同,它不起作用。

我该如何解决这个问题? 谢谢

【问题讨论】:

  • 嘿,你找到解决办法了吗?
  • @Anu 是的,请参阅下面的解决方案

标签: android android-recyclerview


【解决方案1】:

我终于用这个解决了:

 android:focusableInTouchMode="true"
 android:descendantFocusability="beforeDescendants"

在 RecyclerView 的布局中,我将它添加到 Manifest 中:

android:windowSoftInputMode="stateHidden|adjustPan"

【讨论】:

    【解决方案2】:

    也许,它会帮助某人:在您的适配器类中使用 notifyItemRangeChanged() 而不是 notifyDataSetChanged()

    【讨论】:

      【解决方案3】:

      如果你在 xml 代码中使用正确的实现,那么这个问题可以很容易地解决。 这里我找到了一个很好的例子Android Recyclerview with edittext

      他们解决了这个问题。 以下是部分源代码

      recyclerview 行项的xml代码

      <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
          android:id="@+id/card_view"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_marginLeft="0dp"
          android:layout_marginRight="0dp"
          android:layout_marginTop="0dp"
          card_view:cardCornerRadius="4dp">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="80dp"
              android:orientation="horizontal">
      
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="12dp"
                  android:textColor="#000"
                  android:text="TextView:"/>
      
              <EditText
                  android:layout_width="match_parent"
                  android:layout_height="50dp"
                  android:id="@+id/editid"
                  android:layout_marginTop="10dp"
                  android:paddingLeft="10dp"
                  android:textColor="#000"
                  android:text="hello"
                  />
      
          </LinearLayout>
      
      
      </android.support.v7.widget.CardView>
      

      适配器

      import android.content.Context;
      import android.support.v7.widget.RecyclerView;
      import android.text.Editable;
      import android.text.TextWatcher;
      import android.util.Log;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.EditText;
      import android.widget.ImageView;
      import android.widget.TextView;
      import java.util.ArrayList;
      
      /**
       * Created by Parsania Hardik on 17-Apr-18.
       */
      
      public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
      
          private LayoutInflater inflater;
          public static ArrayList<EditModel> editModelArrayList;
      
      
          public CustomAdapter(Context ctx, ArrayList<EditModel> editModelArrayList){
      
              inflater = LayoutInflater.from(ctx);
              this.editModelArrayList = editModelArrayList;
          }
      
          @Override
          public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      
              View view = inflater.inflate(R.layout.rv_item, parent, false);
              MyViewHolder holder = new MyViewHolder(view);
      
              return holder;
          }
      
          @Override
          public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, final int position) {
      
      
              holder.editText.setText(editModelArrayList.get(position).getEditTextValue());
              Log.d("print","yes");
      
          }
      
          @Override
          public int getItemCount() {
              return editModelArrayList.size();
          }
      
          class MyViewHolder extends RecyclerView.ViewHolder{
      
              protected EditText editText;
      
              public MyViewHolder(View itemView) {
                  super(itemView);
      
                  editText = (EditText) itemView.findViewById(R.id.editid);
      
                  editText.addTextChangedListener(new TextWatcher() {
                      @Override
                      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
      
                      }
      
                      @Override
                      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
      
                            editModelArrayList.get(getAdapterPosition()).setEditTextValue(editText.getText().toString());
                      }
      
                      @Override
                      public void afterTextChanged(Editable editable) {
      
                      }
                  });
      
              }
      
          }
      }
      

      根据上述代码,edittext 值在适配器类中进行控制,以消除焦点和滚动问题。

      【讨论】:

        【解决方案4】:
        android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
        

        从你的 manifest 上的 Activity 本身应该足以解决键盘问题

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多