Edittext in Listview android

[亡魂溺海] 提交于 2019-11-28 09:45:01
Cyrille

I was having the same problem. My numberic keyboard would momentarily appear before being replaced by the qwerty keyboard and the EditText losing focus.

The problem is that the keyboard appearing makes your EditText lose focus. To prevent this put the following in your AndroidManifest.xml for the appropriate Activity (or Activities):

android:windowSoftInputMode="adjustPan"

See Android documentation:

When the input method appears on the screen, it reduces the amount of space available for your app's UI. The system makes a decision as to how it should adjust the visible portion of your UI, but it might not get it right. To ensure the best behavior for your app, you should specify how you'd like the system to display your UI in the remaining space.

To declare your preferred treatment in an activity, use the android:windowSoftInputMode attribute in your manifest's <activity> element with one of the "adjust" values.

For example, to ensure that the system resizes your layout to the available space—which ensures that all of your layout content is accessible (even though it probably requires scrolling)—use "adjustResize"

Without seeing your code, how can we suggest the possible solution for your problem. So keep practice of posting possible code whenever you ask any question.

However, here i have found one tutorial for implementing Android Focusable EditText inside ListView. Go through the example and try to implement in your way or find out the solution for your problem.

I solved this "Putting EditText in ListView as an item" problem recently. I am not very good at English. So if there's something I don't explain clearly please tell me.

We know ListView can be scrolled verticaly and we want to put EditText in ListView as an item.

First: Add

android:windowSoftInputMode="adjustResize"

in your AndroidManifest.xml at the activity node.

Second: We create an pojo as model data source to control EditText state

Line.java

public class Line{
    int num;
    String text;
    boolean focus;

    get set method and so on...
}

Third: We write an adapter to adapt EditText to ListView.

Item item_line.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="80dp">

    <EditText
        android:id="@+id/etLine"
        android:focusable="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"/>
</FrameLayout>

Adapter:

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_line, parent, false);
        holder.etLine = (EditText) convertView.findViewById(R.id.etLine);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final Line line = lines.get(position);

    // step 1: remove android.text.TextWatcher added in step 5 to make sure android.text.TextWatcher 
    //         don't trigger in step 2;
    // why?
    // 
    // note: When an object of a type is attached to an Editable, 
    //       TextWatcher's methods will be called when the EidtText's text is changed.
    //       
    //       EditText use a ArrayList<TextWatcher> type object to store the listener, so we must
    //       make sure there's only one TextWatcher object in this list;
    // 
    // Avoid triggering TextWatcher's method in step 2 we remove it at first time.
    // 
    if (holder.etLine.getTag() instanceof TextWatcher) {
        holder.etLine.removeTextChangedListener((TextWatcher) (holder.etLine.getTag()));
    }

    // step 2: set text and focus after remove android.text.TextWatcher(step 1);
    holder.etLine.setHint(position + ".");

    // set text
    if (TextUtils.isEmpty(line.getText())) {
        holder.etLine.setTextKeepState("");
    } else {
        holder.etLine.setTextKeepState(line.getText());
    }

    // set focus status
    // why?
    //
    // note: ListView has a very elegant recycle algorithm. So views in ListView is not reliable.
    //       Especially in this case, EditText is an item of ListView. Software input window may cause
    //       ListView relayout leading adapter's getView() invoke many times.
    //       Above all if we change EditText's focus state directly in EditText level(not in Adapter). 
    //       The focus state may be messed up when the particularly view reused in other position. 
    //       
    //       So using data source control View's state is the core to deal with this problem.   
    if (line.isFocus()) {
        holder.etLine.requestFocus();
    } else {
        holder.etLine.clearFocus();
    }

    // step 3: set an OnTouchListener to EditText to update focus status indicator in data source
    // why?
    // 
    // in step 2, we know we must control view state through data source. We use OnTouchListener
    // to watch the state change and update the data source when user move up fingers(ACTION_UP).
    // We don't want to consume the touch event, simply return false in method onTouch().
    holder.etLine.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                check(position);
            }
            return false;
        }
    });

    // step 4: set TextWatcher to EditText to listen text changes in EditText to updating the text in data source
    // why?
    // 
    // again, use data source to control view state.
    // When user edit the text in one EditText item and scroll the ListView. The particularly EditText item will be
    // reuse in adapter's getView(), this may lead text messed up in ListView.
    // How to deal with this problem?
    // Easy! We update the text in data source at the same time when user is editing. TextWatcher is the best way to
    // do this.
    final TextWatcher watcher = new SimpeTextWather() {

        @Override
        public void afterTextChanged(Editable s) {
            if (TextUtils.isEmpty(s)) {
                line.setText(null);
            } else {
                line.setText(String.valueOf(s));
            }
        }
    };
    holder.etLine.addTextChangedListener(watcher);

    // step 5: Set watcher as a tag of EditText.
    // so we can remove the same object which was setted to EditText in step 4;
    // Make sure only one callback is attached to EditText
    holder.etLine.setTag(watcher);

    return convertView;
}

/**
 * change focus status in data source
 */
private void check(int position) {
    for (Line l : lines) {
        l.setFocus(false);
    }
    lines.get(position).setFocus(true);
}

static class ViewHolder {
    EditText etLine;
}

All done!

You can read more details in my github.

Demo: https://github.com/Aspsine/EditTextInListView

ListView recreate the View, Try to use a LinearLayout inside in ScrollView, then in your code use a runOnUiThread to fill your view in an other thread like this

public void fillDataTask(Context context, final LinearLayout listView) {

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {

                fillView(context, listView);
            }
        });
    }



    private void fillView(Context context, LinearLayout listView) {
            MyAdapter adapter = new MyAdapter(context);
            final int adapterCount = adapter.getCount();
            for (int i = 0; i < adapterCount; i++) {
                View item = adapter.getView(i, null, null);
                listView.addView(item);
            }

    }
        <ListView 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" 
        android:id="@+id/list1" 
        android:layout_weight="1"
        android:cacheColorHint="#00000000" 
        android:focusable="false"                   
        >
        </ListView> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!