【发布时间】:2015-09-30 08:35:19
【问题描述】:
我使用https://github.com/wdullaer/SwipeActionAdapter 滑动列表视图上的每个项目
一旦我轻扫其中一项,textview 文本将递增到一。如果我滚动列表的问题,文本视图将返回到每个默认值,即 0 并且一些隐藏项也在递增。
onswipe 事件代码:
switch (direction) {
case SwipeDirections.DIRECTION_FAR_LEFT:
selectedText = (TextView) getViewByPosition(position, getListView()).findViewById(R.id.txtNumber);
selectedText.setText(String.valueOf(Integer.parseInt(selectedText.getText().toString()) + 1));
break;
和适配器代码:
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(data);
} catch (JSONException e) {
e.printStackTrace();
}
String[] strArr = new String[jsonArray.length()];
ArrayList<String> arrayList = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
try {
strArr[i] = jsonArray.getJSONObject(i).getString("name");
arrayList.add(jsonArray.getString(i));
stringAdapter = new ArrayAdapter<String>(
this,
R.layout.items,
R.id.txtName,
new ArrayList<String>(Arrays.asList(strArr))
);
setListAdapter(stringAdapter);
stringAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
items.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100sp"
android:background="@drawable/listview_style"
android:padding="8dp"
android:descendantFocusability="blocksDescendants">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@mipmap/ic_launcher"
android:layout_centerVertical="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/txtName"
android:textSize="20sp"
android:gravity="center"
android:ellipsize="none"
android:singleLine="false"
android:scrollHorizontally="false"
android:layout_centerVertical="true"
android:layout_marginLeft="20sp"
android:layout_marginRight="20sp"
android:layout_toRightOf="@+id/imageView"
android:layout_toLeftOf="@+id/txtNumber"
android:layout_toStartOf="@+id/txtNumber"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/txtNumber"
android:textSize="25sp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="40dp"
android:layout_marginEnd="40dp"
/>
</RelativeLayout>
我认为项目位置无效或视图无效。 知道如何解决这个问题。谢谢
【问题讨论】:
标签: android listview android-listview