2017-12-03 42 views
1

내 EditText에 현재 커서 Y 위치을 검색하여 (이 Y에 따라 위치를 설정하는) 조각 목록을 표시하려고합니다. 내가 좋아하는 같은 행동을하고 싶지는 페이스 북 앱으로 목록을 언급 :edittext 커서의 절대 화면 위치를 얻는 방법은 무엇입니까?

enter image description here

그래서 내가 행한 :

int pos = editText.getSelectionStart(); 
    Layout layout = editText.getLayout(); 
    int line = layout.getLineForOffset(pos); 
    int baseline = layout.getLineBaseline(line); 
    int ascent = layout.getLineAscent(line); 

    int location[] = new int[2]; 
    editText.getLocationOnScreen(location);  

    Point point = new Point(); 
    point.x = (int) layout.getPrimaryHorizontal(pos); 
    point.y = baseline + ascent + location[1]; 

올바르게하지만 스크롤 내 글고 치기에있다, 위치를 작동 Y (point.y)가 올바르지 않게됩니다 ... 스크롤의 유무에 관계없이 커서의 절대 Y를 정확하게 표시하는 방법을 이해할 수 없습니다.

대단히 감사합니다!

답변

1

사용자 정의 List AdapterAutoCompleteTextView을 사용하여이를 수행 할 수 있습니다. 액티비티/조각에서

:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <ImageView 
    android:id="@+id/image" 
    android:layout_height="wrap_content" 
    android:src="@drawable/icon" 
    android:scaleType="center"/> 
    <TextView 
    android:id="@+id/user_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Name" /> 
</LinearLayout> 

만들기 :

public class User { 
    public String name; 
    public Bitmap image; 

    public User(String name, Bitmap image) { 
    this.name = name; 
    this.image= image; 
    } 
} 

이 행 레이아웃 만들기 :

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.tv_users); 
CustomAdapter<User> adapter = new CustomAdapter<User>(this, R.layout.user_row, usersList); 
textView.setAdapter(adapter); 

하는 객체 사용자 만들기 여기

은 예입니다 새로운 수업 Custo 객체 사용자

public class CustomAdapter extends ArrayAdapter<User> { 
    public CustomAdapter(Context context, int layout, ArrayList<User> users) { 
    super(context, layout, users); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    // Get the user for this position 
    User user = getItem(position); 

    TextView userName = (TextView) convertView.findViewById(R.id.user_name); 
    ImageView image = (ImageView) convertView.findViewById(R.id.image); 

    userName.setText(user.name); 
    image.setImageBitmap(user.image); 

    return convertView; 
} 
} 
0

위한 ArrayAdapter와 확장 mAdapter 비슷한 문제를했다 - 당신이합니다 (글고의 스크롤의 Y 픽셀 오프셋 (offset) 반환) editText.getScrollY()를 사용하여이 문제를 해결할 수 있습니다. 따라서 귀하의 경우

:

point.y = baseline + ascent + location[1] - editText.getScrollY();