4

我有一个listview适配器,当我修改我的TextView时,我打电话notifyDataSetChanged()方法addTextChangeListener()。但是我的TextView失去了焦点。 我如何保持焦点,覆盖notifyDataSetChanged()在自定义列表视图上调用notifyDatasetChanged()后,将注意力集中在TextView上?

我这样做,但没有奏效

@Override 
public void notifyDataSetChanged(){ 
    TextView txtCurrentFocus = (TextView) getCurrentFocus(); 
    super.notifyDataSetChanged(); 
    txtCurrentFocus.requestFocus(); 
} 

回答

0

你可以扩展ListView类并重写requestLayout()方法。该方法被调用,当ListView完成更新并窃取焦点。所以,在这种方法结束时,你可以将焦点返回到你的TextView

public class ExampleListView extends ListView { 

    private ListViewListener mListener; 

    public ExampleListView(Context context) { 
     super(context); 
    } 

    public ExampleListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ExampleListView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void requestLayout() { 
     super.requestLayout(); 
     if (mListener != null) { 
      mListener.onChangeFinished(); 
     } 
    } 

    public void setListener(ListViewListener listener) { 
     mListener = listener; 
    } 

    public interface ListViewListener { 
     void onChangeFinished(); 
    } 
} 

,并设置监听到这个ListView

ExampleListView listView = (ExampleListView) view.findViewById(R.id.practice_exercises_list); 
listView.setListener(new ExampleListView.ListViewListener() { 
      @Override 
      public void onChangeFinished() { 
       txtCurrentFocus.requestFocus(); 
      } 
     }); 
+0

这就是很好的和所有,但这样做的时候,我失去在EDITTEXT的焦点位置。任何意见如何我可以解决这个问题? – glace

+0

@glace你可以通过使用[TextWatcher建议这里](http://stackoverflow.com/a/15030172/1219012)记住文本位置,并使用[setSelection]恢复该位置(http://stackoverflow.com/questions/8035107/how-to-set-cursor-position-in-edittext) –

+0

如何知道在动态生成时应使用哪个EditText。请不要告诉我,我必须为每个EditText使用一个TextWatcher。对于多个EditTexts,必须可以使用一个TextWatcer吗?否则,我将需要〜30加TextWatchers -.- – glace

相关问题