1
我可以發誓此工作之前,但現在當我測試它我有一個問題。我有一個自定義列表視圖,它有一系列不同高度的文字視圖。在Android 4.x中,它完美運行。在我的舊手機2.3列表中出現,但它只有一行高,所以大部分的textview無法看到。它的行爲應該像它應該的那樣,它滾動並且我可以通過長時間按下刪除項目。Android ListView無法正常工作薑餅
我試圖只是改變了TextView
高度爲固定值,這適用於4.x的,但使在2.3
沒有區別這裏是代碼的相關位: notam_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listOfNotams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
mylistviewstyle.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listviewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:textSize="12sp" />
從活動片段:
public class NotamList extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notam_list);
final ListView listOfNotams = (ListView) findViewById(R.id.listOfNotams);
....
}
final StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.mylistviewstyle, NOTAMS);
listOfNotams.setAdapter(adapter);
listOfNotams.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public boolean onItemLongClick(AdapterView<?> parent, final View view, int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
Pattern pattern;
Matcher matcher;
if (item.substring(4, 5).matches("-")) {
pattern = Pattern.compile("[A-Z][A-Z][A-Z][A-Z]-[A-Z][0-9]+/[12]\\d");
matcher = pattern.matcher(item);
matcher.find();
String notamSelected = matcher.group(0);
HiddenNotam hiddenNotam = new HiddenNotam(notamSelected, 1);
if (db.checkHiddenNotamExists(notamSelected)) {
db.deleteHiddenNotam(hiddenNotam);
Toast toast = Toast.makeText(getApplicationContext(), notamSelected + " Un-Hidden", Toast.LENGTH_SHORT);
toast.show();
} else {
db.addHiddenNotam(hiddenNotam);
if (hideNotams) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
// POSSIBLE SOLUTION TO THIS
// http://nineoldandroids.com/
NOTAMS.remove(item);
adapter.notifyDataSetChanged();
} else {
view.animate().setDuration(2000).alpha(0).withEndAction(new Runnable() {
@Override
public void run() {
NOTAMS.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
}
}
Toast toast = Toast.makeText(getApplicationContext(), notamSelected + " Hidden", Toast.LENGTH_SHORT);
toast.show();
}
} else {
Toast toast = Toast.makeText(getApplicationContext(), "This can't be hidden", Toast.LENGTH_SHORT);
toast.show();
}
return true;
}
});
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
任何想法?
編輯:我也剛剛發現,在我的幫助屏幕,這只是一系列TextViews文本沒有包裝。當然,每個TextView只是變成一個大的行,沒有水平滾動。我已經用完全相同的方式編寫了其他應用程序(據我所知),我以前沒有遇到過這個問題?
你在哪裏設置文本值? – keshav
它們被加載到onCreate方法中稱爲NOTAMS的ArrayList中。 –
Gavin
在你的StableArrayAdapter中,你應該重寫getView() – keshav