Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make EditText selectable when view attached to window #11

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import android.content.Context;
import android.graphics.PointF;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.facebook.react.bridge.ReactContext;
import com.facebook.react.common.SystemClock;
Expand Down Expand Up @@ -158,6 +159,7 @@ public boolean onTouchEvent(MotionEvent event) {
private final RecyclerViewBackedScrollView mScrollView;
private int mItemCount = 0;


public ReactListAdapter(RecyclerViewBackedScrollView scrollView) {
mScrollView = scrollView;
//setHasStableIds(true);
Expand Down Expand Up @@ -205,6 +207,42 @@ public void onViewRecycled(ConcreteViewHolder holder) {
((RecyclableWrapperViewGroup) holder.itemView).removeAllViews();
}

@Override
public void onViewAttachedToWindow(@NonNull ConcreteViewHolder holder) {
ArrayList<View> allEditTextsInViewGroup = getAllEditTextChildren(holder.itemView);
for (View child : allEditTextsInViewGroup) {
EditText editText = (EditText) child;
// by default, an EditText that is focusable should also be selectable, so

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that we're making a strong assumption here. This could work in our case, but I can have an EditText that is focusable but not selectable.
Should we check textIsSelectable before resetting it to true?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it is an assumption. Unfortunately there is no query API for this. However, I can't think of a practical case where it makes sense to use an EditText that is focused but can't be selected. It may make sense for TextView though. We can certainly live without that in this fwiw.

// we should be safe to make sure text is selectable by resetting it.
if (editText.isFocusable()) {
editText.setTextIsSelectable(true);
}
}

super.onViewAttachedToWindow(holder);
}

private ArrayList<View> getAllEditTextChildren(View v) {
if (!(v instanceof ViewGroup)) {
ArrayList<View> viewArrayList = new ArrayList<View>();
if (v instanceof EditText) {
viewArrayList.add(v);
}
return viewArrayList;
}

ArrayList<View> result = new ArrayList<View>();

ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
ArrayList<View> childChildren = new ArrayList<View>();
childChildren.addAll(getAllEditTextChildren(child));
result.addAll(childChildren);
}
return result;
}

@Override
public int getItemCount() {
return mItemCount;
Expand Down
7 changes: 5 additions & 2 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
Button,
TouchableHighlight,
ToastAndroid,
SafeAreaView
SafeAreaView,
TextInput,
} from 'react-native';

import RecyclerViewList, { DataSource } from 'react-native-recyclerview-list';
Expand Down Expand Up @@ -208,7 +209,9 @@ class Item extends Component {
return (
<TouchableHighlight
onPress={onIncrementCounter}>
<View style={{ flexDirection: 'row', alignItems: 'center', marginHorizontal: 5 }}>
{/* <View style={{ flexDirection: 'row', alignItems: 'center', marginHorizontal: 5 }}> */}
<View>
<TextInput value="hello"/>
<Image
source={{ uri: 'http://loremflickr.com/320/240?t=' + (id % 9) }}
style={{
Expand Down