Android edittext in listview loses focus on calling notifydatachanged

后端 未结 5 1710
清酒与你
清酒与你 2020-12-15 18:05

I have a few edittext within a listview. i have a generic focuslistener on the edittext that updates the value of the data model and also the background of the edittext when

相关标签:
5条回答
  • 2020-12-15 18:15

    Set

    android:windowSoftInputMode="adjustPan"
    

    for your activity in the AndroidManifest.xml

    0 讨论(0)
  • 2020-12-15 18:17

    Example of implementing Sam Judd's suggestion with an additional switch use_last_focus

    public class my_friends_adapter extends ArrayAdapter<group_user> {
        boolean use_last_focus=false;
        int currentlyFocusedRow=-1;
        String currentlyFocusedField="";
    
        public my_friends_adapter(Context context, int resource, ArrayList<group_user> users) {
                super(context, resource, users);
                  }
            EditText tvName,tvEmail;
    
        @Override
       public View getView(int position, View convertView, ViewGroup parent) {
    
            if (convertView == null) convertView = LayoutInflater.from(getContext()).inflate(R.layout.my_friends_item, parent, false);
            tvName = (EditText) convertView.findViewById(R.id.tvName);
            tvEmail = (EditText) convertView.findViewById(R.id.tvEmail);
            if (use_last_focus && currentlyFocusedRow ==position){
                if (currentlyFocusedField=="tvName")tvName.requestFocus();
                else tvEmail.requestFocus();
                use_last_focus=false;
            }
            tvName.setTag(position);//When focus is lost save the entered value for later use
            tvName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {save_data2( v,  hasFocus);
                }
            });
    
            tvEmail.setTag(position);//When focus is lost save the entered value for later use
            tvEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {save_data2( v,  hasFocus);}
            });
    
            return convertView;
            }
    
    
        public  void save_data2(View v, boolean hasFocus){
            int position = (Integer) v.getTag();
            EditText tvName1 = (EditText) v.findViewById(R.id.tvName);
            EditText tvEmail1 = (EditText) v.findViewById(R.id.tvEmail);
            boolean data_changed=false;
            if (hasFocus) currentlyFocusedRow=position;
            if (!(tvName1==null)) {
                if (hasFocus) currentlyFocusedField="tvName";
            }
            if (!(tvEmail1==null)) {
                if (hasFocus) currentlyFocusedField="tvEmail";
            }
            // set when data_changed
            // ..........
            if (!hasFocus && data_changed) {
                //your code to save the changed data
                // ...............
                //
                my_friends.adapter.notifyDataSetChanged();
                use_last_focus=true;
            }
    
        }
    
    
    
    }
    
    0 讨论(0)
  • 2020-12-15 18:30

    I also had a problem of EditText loosing the focus, but calling setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS) solved it.

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final ListView lv = getListView();
        lv.setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS);
    }
    
    0 讨论(0)
  • 2020-12-15 18:30

    For future users, everything here is overcomplicated. The correct answer is to add

    android:descendantFocusability="afterDescendants"
    

    to your listview.

    0 讨论(0)
  • 2020-12-15 18:39

    It is indeed happening because all the views are redrawn, so the edit text representing whatever row used to be focused is now a completely different object. Set a variable in your adapter: int currentlyFocusedRow;

    in getView for your adapter: Add an onFocusChanged listener to each edit text and when that edit text gains focus, set currentlyFocusedRow = whatever row the focused edit text happens to be in. Also set any edit text that is in the currentlyFocusedRow to be focused.

    0 讨论(0)
提交回复
热议问题