简体   繁体   English

Android Listview ViewHolder 不同的项目一起反应

[英]Android Listview ViewHolder different items react together

I'm working on a ListView which use an Adapter and ViewHolder component in aim to ensure stability of items but I'm facing an unexpected behavior.我正在开发一个 ListView,它使用 Adapter 和 ViewHolder 组件以确保项目的稳定性,但我面临着意外行为。

At first I need to explain that my items aren't static : each item is a row with a checkbox and when the box is checked a form appears below to ask user to give more informations.首先,我需要解释我的物品不是静态:每个项目都是一个带复选框的行,检查框后表格下面询问用户提供更多信息。

But at the moment when user check the box and enter some text in the form, another item react the same way :但是当用户选中该框并在表单中输入一些文本时,另一个项目的反应方式相同:

  • checkbox checked复选框已选中
  • the same text in the form表单中的相同文本

The unwanted reacting item is not drawn at the same time ( both items aren't visible together, a scroll is needed to see one or the other)不需要的反应项目不会同时绘制(两个项目不一起可见,需要滚动才能看到一个或另一个)

It's not accurate but my activity setting windowSoftInputMode is adjustNothing这不准确,但我的活动设置 windowSoftInputMode 是 adjustNothing

Thanks for any answer!感谢您的任何回答!

Great day愉快的一天

Here is my code这是我的代码

private List<Object> mObjects;
private LongSparseArray<Account> uAccounts;

ImageView illustration;
TypefaceTextView explanation;
ProgressBar load;
ListView lv;

private Context context;
private AccountExpListAdapter accountExpListAdapter;
public int occurences;
private boolean allGood = true;

private void initUI() {
    if (getActivity() != null && getActivity().getApplicationContext() != null) {
        accountExpListAdapter = new AccountExpListAdapter(getActivity().getApplicationContext(),
                mObjects);
        if (lv != null) {
            lv.setAdapter(accountExpListAdapter);
        }
    }
    if (load != null)
        load.setVisibility(View.GONE);
}

static class ViewHolder {
    ImageView logo;
    TextView tv;
    RelativeLayout header;
    RelativeLayout form;
    CheckBox checkBox;
    EditText email_ET;
    EditText pwd_ET;
}

public class AccountExpListAdapter extends ArrayAdapter<Object> {


    private List<Object> objects;
    private Context context;


    public AccountExpListAdapter(Context context, List<Object> objects) {
        super(context, android.R.layout.simple_list_item_1, objects);
        this.context = context;
        this.objects = objects;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        ViewHolder holder;
        if (vi == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            vi = inflater.inflate(R.layout.selectable, parent, false);
            holder = new ViewHolder();
            holder.logo = (ImageView) vi.findViewById(R.id.logo);
            holder.tv = (TextView) vi.findViewById(R.id.name);
            holder.header = (RelativeLayout) vi.findViewById(R.id.header);
            holder.form = (RelativeLayout) vi.findViewById(R.id.form);
            holder.checkBox = (CheckBox) vi.findViewById(R.id.box);
            holder.email_ET = (EditText) vi.findViewById(R.id.email_edit_text);
            holder.pwd_ET = (EditText) vi.findViewById(R.id.pwd_edit_text);
            vi.setTag(holder);

        } else {
            holder = (ViewHolder) vi.getTag();
        }
        final Object object = objects.get(position);

        Glide.with(context).load(Uri.parse(object.getLogo())).into(holder.logo);
        holder.tv.setText(object.getName());
        final ViewHolder finalHolder = holder;
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isChecked = ((CheckBox) view).isChecked();
                manageKeyboard(isChecked);
                int visibility = isChecked ? View.VISIBLE : View.GONE;
                finalHolder.form.setVisibility(visibility);
                if (!isChecked) {
                    uAccounts.remove(object.getId());
                } else {
                    uAccounts.put(object.getId(), new Account());
                    allGood = false;
                   lv.smoothScrollToPositionFromTop(position + 1, 150, 500);
                    finalHolder.email_ET.requestFocus();
                }
            }
        });
        holder.header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finalHolder.checkBox.performClick();
            }
        });
        if (holder.form != null) {
            holder.email_ET.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    if (!checkEmailFormat(editable.toString())) {
                        finalHolder.email_ET.setError(getString(R.string.email_invalid));
                        allGood = false;
                        return;
                    } else {
                        allGood = true;
                    }
                    Long objectId = object.getId();
                    Account a;
                    if (uAccounts.indexOfKey(objectId) < 0) {
                        a = new Account();
                    } else {
                        a = uAccounts.get(objectId);
                    }
                    a.setUsername(editable.toString());
                    uAccounts.put(objectId, a);
                }
            });
            holder.pwd_ET.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    if (editable.toString().length() == 0) {
                        finalHolder.pwd_ET.setError(getString(R.string.password_invalid));
                        allGood = false;
                        return;
                    } else {
                        allGood = true;
                    }
                    Long objectId = object.getId();
                    Account a;
                    if (uAccounts.indexOfKey(objectId) < 0) {
                        a = new Account();
                    } else {
                        a = uAccounts.get(objectId);
                    }
                    a.setEncrypted_password(editable.toString());
                    uAccounts.put(objectId, a);
                }
            });
        }
        return vi;
    }
}
}

the code is quite complex for the checkbox due to the show/hide form but does it may produce this unwanted listview items behavior?由于显示/隐藏表单,复选框的代码非常复杂,但它是否会产生这种不需要的列表视图项目行为?

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM