gpt4 book ai didi

android - 自定义 ListView 适配器选择的项目增量 TextView

转载 作者:行者123 更新时间:2023-11-30 01:52:10 26 4
gpt4 key购买 nike

我用了https://github.com/wdullaer/SwipeActionAdapter滑动 ListView 上的每个项目

一旦我滑动其中一项,textview 文本将增加到一个。问题是如果我滚动列表,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();
}
}

项目.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>

我在考虑项目位置无效或 View 无效。任何想法如何解决这个问题。谢谢

更新递增现在工作正常,但项目名称未填充。见附件 item name not populating

最佳答案

问题是 ListView 作弊。这是一个回收 View ,所以发生的情况是您实际上只有 10 个与当前看到的 View 相同的 View 。当您滚动到足以使 View 消失时,它会再次显示为刚刚进入 View 的 View 。为此,它摆脱了旧 View ,要求适配器将这个废弃的 View 变成看起来像新 View 的东西(这对于内存和快速 View 创建来说非常棒)。

这就是您的项目消失的原因,因为在您滚动离开后, ListView 会使用适配器回收 View 。如果您真的想看到这个,请尝试通过滑动将 View 的可见性设置为不可见,然后您会发现整个地方的 View 都不见了。因为它们是相同的 View 。

简而言之,滑动必须更改用于构建 View 的数据。对 View 本身的任何更改都将被删除,或者弄乱其他 View (诸如可见性和 .transform() 之类的东西通常不会被适配器重置),它们实际上又是同一个 View 。

public class SwipeActivity extends AppCompatActivity {

SwipeActionAdapter mAdapter;

private class YourCustomRowEntry {
String displayString;
int swipes;

public YourCustomRowEntry( String displayString, int swipes) {
this.swipes = swipes;
this.displayString = displayString;
}
}

private class Holder {
public TextView textName, textNumber;
public ImageView imageView;
public Holder(TextView textName, TextView textNumber, ImageView imageView) {
this.textName = textName;
this.textNumber = textNumber;
this.imageView = imageView;
}
}

ArrayList<YourCustomRowEntry> mDataYouEditThatBacksTheAdapter = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);

for (int i = 1; i <= 200; i++) {
mDataYouEditThatBacksTheAdapter.add(new YourCustomRowEntry("Row " + i,0));
}

BaseAdapter customAdapter = new BaseAdapter() {
@Override
public int getCount() {
return mDataYouEditThatBacksTheAdapter.size();
}

@Override
public Object getItem(int position) {
return mDataYouEditThatBacksTheAdapter.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView;
Holder viewHolder;
if (convertView != null) {
itemView = convertView; //if you already made this view, and it's being recycled use that.
viewHolder = (Holder)convertView.getTag(); //And fetch the already findByViews things.
}
else {
//if this is the first time, inflate the view.
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.items, parent, false);
TextView textName = (TextView)itemView.findViewById(R.id.txtName);
TextView textNumber = (TextView)itemView.findViewById(R.id.txtNumber);
ImageView imageView = (ImageView)itemView.findViewById(R.id.imageView);
viewHolder = new Holder(textName,textNumber,imageView);
itemView.setTag(viewHolder); //store the data in the view's tag.
}
YourCustomRowEntry ycre = mDataYouEditThatBacksTheAdapter.get(position);
viewHolder.textName.setText(ycre.displayString);
viewHolder.textNumber.setText("" + ycre.swipes); // Gotta tell it that this is a string and not a resource.
//You would also set the imageView from the saved set of data here too.
return itemView;
}
};

ListView listView = (ListView)findViewById(R.id.myActivitysListView);

// Wrap your content in a SwipeActionAdapter
mAdapter = new SwipeActionAdapter(customAdapter);

// Pass a reference of your ListView to the SwipeActionAdapter
mAdapter.setListView(listView);

// Set the SwipeActionAdapter as the Adapter for your ListView
listView.setAdapter(mAdapter);

// Listen to swipes
mAdapter.setSwipeActionListener(new SwipeActionAdapter.SwipeActionListener() {
@Override
public boolean hasActions(int position) {
// All items can be swiped
return true;
}

@Override
public boolean shouldDismiss(int position, int direction) {
// Only dismiss an item when swiping normal left
return false;
//return direction == SwipeDirections.DIRECTION_NORMAL_LEFT;
}

@Override
public void onSwipe(int[] positionList, int[] directionList) {
for (int i = 0; i < positionList.length; i++) {
int direction = directionList[i];
int position = positionList[i];
switch (direction) {
case SwipeDirections.DIRECTION_FAR_LEFT:
mDataYouEditThatBacksTheAdapter.get(position).swipes++; //add 1 to swipes;
mAdapter.notifyDataSetChanged();
break;
case SwipeDirections.DIRECTION_FAR_RIGHT:
mDataYouEditThatBacksTheAdapter.get(position).swipes--; //subtract 1 to swipes;
mAdapter.notifyDataSetChanged();
break;
}
}
}
});


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_swipe, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

它的工作视频: https://youtu.be/6wPF2OOKu2U

保存用于支持 ListView 的数组。您需要拥有它,以便您可以更改它并让适配器构建新 View 。 notifyDataSetChanged() 并根据它保存的原始数据结构更新并重建 View 。这意味着您需要修改该数据,而不是 View 。这正确地编写了一个类并使用它来构建 View 。

关于android - 自定义 ListView 适配器选择的项目增量 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32861503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com