我工作的一個Android應用程序,我想實現一個列表視圖,每排4個內容EditTexts。 主要目標是編寫EditText,然後將它們保存在列表中。 我這裏https://code.google.com/p/android/issues/detail?id=21105 遇到了同樣的問題,當我點擊了三個第一要素和三節結束它完美,但是當我點擊第5個元素,例如,(如果我有我的名單上的22個項目),我失去了重點,我不得不滾動另一次來找到我點擊的地方。 我創建一個子類從ListView的延伸,在這裏我的GUI:的ListView與edittexts
<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);
}
}
問題是什麼?
如果你知道有多少的TextView您將使用列表視圖不是ONY解決方案。您也可以在.xml中放置佈局,並在運行時以編程方式添加textview =) –
我們不知道我們將使用多少個Edittext。我做了什麼ü說,但它不工作 – user2838231