我正在开发一个android应用程序,我想实现一个listview,每行内容4个EditText。主要目标是在 EditText 中写入,然后将它们保存在列表中。我在这里遇到了同样的问题https://code.google.com/p/android/issues/detail?id=21105 当我点击三个第一个元素和三个结尾时它完美地工作,但是当我点击例如,第 5 个元素(如果我的列表中有 22 个项目),我失去了焦点,我必须再次滚动才能找到我单击的点。我创建了一个从 ListView 扩展的子类,这里是我的 GUI:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<AutoResizingListView
android:id="@+id/listViewProduits"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_color" />
</LinearLayout>
</ScrollView>
子类:
import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
/**
*
*/
public class AutoResizingListView extends ListView {
public AutoResizingListView(Context context) {
super(context);
}
public AutoResizingListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoResizingListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void clearChildFocus(View child) {
super.clearChildFocus(child);
if (getSelectedItemPosition() != -1 && getItemsCanFocus()) {
requestFocus();
}
}
@Override public void setAdapter(final ListAdapter adapter) {
super.setAdapter(adapter);
adapter.registerDataSetObserver(new DataSetObserver() {
@Override public void onChanged() {
setHeight();
}
});
setHeight();
}
private void setHeight() {
ListAdapter adapter = getAdapter();
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, this);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = this.getLayoutParams();
int dividerHeights = getDividerHeight() * (adapter.getCount() == 0 ? 0 : adapter.getCount() - 1);
params.height = totalHeight + dividerHeights;
setLayoutParams(params);
}
}
问题是什么?