Reputation: 1892
I've put some EditText
in RecyclerView
because I need to get some values. The implementation is this:
<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" />
and this is the custom xml with some editText
:
<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" />
practically when I click on an EditText
it opens keyboard, lose focus and close keyboard immediately, so it's impossible to write in these. I also tried reading other question, to put this:
recyclerView.setFocusable(true);
recyclerView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
recyclerView.setAdapter(adapter);
but, at different of listview
, it doesn't work.
How could I solve this issue? Thank you
Upvotes: 24
Views: 20074
Reputation: 71
Maybe, it will help someone: use notifyItemRangeChanged()
instead notifyDataSetChanged()
in your adapter class.
Upvotes: -1
Reputation: 4463
android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
From your manifest on the activity itself should be sufficient to solve the keyboard
Upvotes: -1
Reputation: 4623
If you use proper implementation in xml code, then this problem can be solved easily. Here I have found a nice example Android Recyclerview with edittext
They solved this problem. Here are some of the source code
xml code for recyclerview row item
<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>
Adapter
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) {
}
});
}
}
}
As per above code, edittext values are controlled in the adapter class to remove focus and scrolling problems.
Upvotes: -1
Reputation: 1892
Finally I've solved it using this:
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
in RecyclerView's layout and I add it in the Manifest:
android:windowSoftInputMode="stateHidden|adjustPan"
Upvotes: 20