我有一个视图,其中包含多个水平滚动的回收站视图。每个回收者视图单元格都包含一个EditText控件。当我将回收站视图滚动到右侧并点击EditText框时,回收站将滚动回到左侧的第一个位置。这些文件所包含的主要视图还包含一个EditText。
我正在为回收器视图使用自定义适配器。
我尝试了很多搜索,发现了类似的问题,但没有类似的问题:
Android EditText's container (RecyclerView) auto scroll when EditText is Focused
任何帮助将不胜感激。
这是我所拥有的一个例子: 我的主要布局的一部分包含回收者视图:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:paddingTop="10dp"
android:paddingBottom="5dp"
android:paddingStart="10dp"
android:paddingLeft="10dp"
android:paddingEnd="10dp"
android:paddingRight="10dp"
android:text="@string/txtVendorName"
android:textColor="@color/rust"
android:id="@+id/lblVendor"
android:layout_below="@+id/include"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="start" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/lblVendor"
android:paddingBottom="5dp"
android:paddingStart="10dp"
android:paddingLeft="10dp"
android:paddingEnd="10dp"
android:paddingRight="10dp"
android:id="@+id/llQuantity"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textSize="12sp"
android:textColor="@color/black"
android:text="@string/lblQuantity"
android:id="@+id/lblQuantity"
android:gravity="center_vertical|start"
/>
<EditText
android:layout_width="60dp"
android:layout_height="25dp"
android:id="@+id/editQuantitysc"
android:hint="@string/txtRepListItemHint"
android:textSize="12sp"
android:maxEms="4"
android:background="@drawable/edit_text_box"
android:gravity="center"
android:inputType="number"
android:textColor="@color/black"
android:ems="4"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:maxLength="4"
android:imeOptions="actionDone"
android:singleLine="true"
android:focusable="true"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/llayRequiredSection"
android:layout_below="@+id/llQuantity"
android:orientation="vertical"
android:visibility="gone"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/llayRequiredHeader"
android:orientation="horizontal"
android:background="@color/white"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnShowHideRequired"
android:text="@string/lblRequiredSign"
android:textSize="32sp"
android:background="@color/white"
android:gravity="center"
android:textColor="@color/rust"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/txtRequiredHeaderLabel"
android:text="@string/lblRequiredItems"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/black"
android:gravity="center"
/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/graySeparator"
android:id="@+id/lineBreakRequired"
android:paddingTop="5dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/txtRequiredItemsMessage"
android:text="@string/lblRequiredMessage"
android:textSize="14sp"
android:textColor="#DB2C28"
android:gravity="start"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/llayRequiredAreaToShowHide"
android:orientation="vertical"
android:animateLayoutChanges="true"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyItemRequired"
android:paddingBottom="2dp"
android:layout_marginTop="5dp"
>
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
我的适配器:
class ItemAccessoriesAdapter extends RecyclerView.Adapter<ItemAccessoriesAdapter.ViewHolder> {
private Context mContext;
private List<Item> lstAccessories;
ItemAccessoriesAdapter(List<Item> list, Context c) {
mContext = c;
this.lstAccessories = list;
}
@Override
public ItemAccessoriesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_sub_item, parent, false);
ViewHolder vh = new ViewHolder(v, new ToOrderTextListener());
return vh;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if (lstAccessories.get(position) != null) {
final Item itemAccessory = lstAccessories.get(position);
holder.etQuantity.setText(displayQty);
holder.toOrderTListener.updatePosition(position);
holder.etQuantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
holder.etQuantity.getText().clear();
} else {
holder.etQuantity.setText("" + itemAccessory.get_quantity()); //reset the item quantity to be displayed
}
}
});
//other field setting...
}
}
@Override
public int getItemCount() {
if (lstAccessories != null) {
return lstAccessories.size();
}
else{
return 0;
}
}
void didClickSubItem(Item itemClicked){
// ... take to view
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
EditText etQuantity;
ToOrderTextListener toOrderTListener;
ViewHolder(View itemView, ToOrderTextListener tlToOrder) {
super(itemView);
this.etQuantity = itemView.findViewById(R.id.editSubQuantity);
this.toOrderTListener = tlToOrder;
this.etQuantity.addTextChangedListener(tlToOrder);
//other fields....
}
@Override
public void onClick(View view) {
if(lstAccessories.get(getAdapterPosition()) != null){
Item itemClicked = lstAccessories.get(getAdapterPosition());
didClickSubItem(itemClicked);
}
}
}
private class ToOrderTextListener implements TextWatcher {
private int position;
public void updatePosition(int position) {
this.position = position;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void afterTextChanged(Editable editable) {
if (lstAccessories.get(position) != null){
Item item = lstAccessories.get(position);
//update item quantity...
}
}
}
}
我的单元格布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="@+id/relLaySubItem"
android:focusable="true"
android:focusableInTouchMode="false"
android:background="@drawable/cell_border"
android:layout_margin="5dp"
android:paddingTop="10dp"
>
<ImageView
android:id="@+id/imgSubItem"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:contentDescription="@string/imgContentDescriptionProduct"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imgSubItem"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="@color/black"
android:text="@string/txtVendorName"
android:id="@+id/lbl_vendor"
android:gravity="center"
android:padding="4dp"
android:singleLine="true"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lbl_vendor"
android:textSize="14sp"
android:textColor="@color/black"
android:text="@string/txtDescrp"
android:id="@+id/lbl_description"
android:gravity="center"
android:singleLine="true"
android:padding="4dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lbl_description"
android:textSize="14sp"
android:textColor="@color/rust"
android:text="@string/lblItemDetail"
android:id="@+id/lbl_itemID"
android:gravity="center"
android:padding="4dp"
android:singleLine="true"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lbl_itemID"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="@color/black"
android:text="@string/lblPrice"
android:id="@+id/lbl_price"
android:gravity="center"
android:padding="4dp"
android:singleLine="true"
android:layout_marginTop="2dp"
/>
<LinearLayout
android:id="@+id/llStockStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lbl_price"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
>
<Button
android:id="@+id/btnCheckAvailabilityCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="10dp"
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="@string/btnCheckAvailability"
android:textColor="@color/rust"
android:textSize="12sp" />
<ImageView
android:id="@+id/imgAvailIcon"
android:layout_marginTop="2dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="2dp"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="start"
android:src="@drawable/infoicon"
android:contentDescription="@string/imgContentDescriptionProduct"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/llaySubQuantitySection"
android:layout_below="@+id/llStockStatus"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
>
<EditText
android:id="@+id/editSubQuantity"
android:layout_width="60dp"
android:layout_height="25dp"
android:layout_centerInParent="true"
android:layout_marginTop="5dp"
android:background="@drawable/edit_text_box"
android:ems="4"
android:focusableInTouchMode="true"
android:gravity="center"
android:inputType="number"
android:maxEms="4"
android:maxLength="4"
android:text="0"
android:textColor="@color/black"
android:textSize="12sp"
android:imeOptions="actionDone"
android:singleLine="true"
android:layout_marginBottom="10dp"
/>
</RelativeLayout>
</RelativeLayout>