簡體   English   中英

列表視圖中的 Android 編輯文本失去了對調用 notifydatachanged 的​​關注

[英]Android edittext in listview loses focus on calling notifydatachanged

我在列表視圖中有一些編輯文本。 我在 edittext 上有一個通用的 focuslistener,它通過調用 notifydatachanged 在失去焦點時更新數據模型的值以及 edittext 的背景。 問題是,如果其中一個編輯文本保持焦點,當我觸摸下一個編輯文本時,它會暫時獲得焦點然后失去焦點。 我懷疑是由於notifydatachanged 方法調用導致所有視圖被重繪,之后焦點丟失。 有沒有人對這個問題有建議或解決方法? 謝謝。

這確實發生了,因為所有的視圖都被重繪了,所以代表過去聚焦的任何行的編輯文本現在是一個完全不同的對象。 在您的適配器中設置一個變量: int currentFocusedRow;

在適配器的 getView 中:為每個編輯文本添加一個 onFocusChanged 偵聽器,當該編輯文本獲得焦點時,設置 currentFocusedRow = 焦點編輯文本恰好位於的任何行。同時將 currentFocusedRow 中的任何編輯文本設置為焦點。

android:windowSoftInputMode="adjustPan"

用於您在 AndroidManifest.xml 中的活動

我也有 EditText 失去焦點的問題,但調用setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS)解決了它。

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final ListView lv = getListView();
    lv.setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS);
}

對於未來的用戶來說,這里的一切都過於復雜了。 正確答案是添加

android:descendantFocusability="afterDescendants"

到您的列表視圖。

使用附加開關 use_last_focus 實現 Sam Judd 建議的示例

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;
        }

    }



}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM