片段ListView奇怪的行为

时间:2016-03-21 12:55:31

标签: android listview android-listfragment

我的片段中有ListView,我尝试通过LongClick选择一些项目。 在我的屏幕上我可以看到8个项目(在另一个智能手机上我可以看到6个项目。)当我在ListView中有很多项目(例如23)并且我在第一个项目上长按时看到更改的图像,但是如果我向下滚动我可以看到10和19项也改变了图像(就像它们已经被检查过一样)。在LongClick第一个项目之后的另一个智能手机,我看到更改8和16等等)。正如您所看到的那样,我可以在显示与我的智能手机相同数量的项目之后更改ListView项目图像。 "额外"的真实状态项目未更改,只有图像视图。这是ListView的奇怪行为,它复制了每个组中项目视图的更改图像(组表示ListView项目的数量是智能手机可以同时显示的项目数量的倍数)

我的代码有什么问题或者如何避免ListView的这种意外行为? 感谢名单

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import ru.someCode.R;

public class ListItems extends ListFragment {

    private ListView lv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_list, null);
        lv = getListView();

        return  rootView;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        super.onListItemClick(l, v, position, id);
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> adapter, View view,
                                           int position, long id) {

                Log.d("MY", "Checked");

                ImageView imageView = ((ImageView) view.findViewById(R.id.operImg));
                ImageView imageViewCheck = ((ImageView) view.findViewById(R.id.operImgCheck));

                if (lv.isItemChecked(position)) {

                    lv.setItemChecked(position, false);
                    imageView.setVisibility(View.VISIBLE);
                    imageViewCheck.setVisibility(View.GONE);


                } else {

                    lv.setItemChecked(position, true);
                    imageView.setVisibility(View.INVISIBLE);
                    imageViewCheck.setVisibility(View.VISIBLE);

                }
                return true;
            }
        });
    }
}

---- fragment_list.xml ----

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="6dp">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listContainer">

            <ListView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:dividerHeight="1dip"
                android:divider="@color/listDev"
                android:footerDividersEnabled="true"/>

            <TextView
                android:id="@id/android:empty"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="16sp"
                android:gravity="center" />

        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/progressContainer"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:visibility="gone">

            <ProgressBar style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </RelativeLayout>
</LinearLayout>

----适配器代码部分---

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.history_list_group, null);

            holder = findViewsById(convertView);
            convertView.setTag(holder);

            if (lv == null) {
                lv = (ListView) parent;
            }


        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        SetDataView(holder, position);

        return convertView;
    }

---- history_list_group.xml -

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/circle"
            android:id="@+id/operImgLayout"
            android:layout_gravity="center_vertical"
            android:layout_margin="6dp">

            <ru.phone4pay.phone4pay.extlib.MLRoundedImageView
                android:id="@+id/operImg"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_action_1"/>

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:id="@+id/operImgCheck"
                android:src="@drawable/ic_action_tick"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:visibility="gone"/>

        </RelativeLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

ListView回收视图,所以我错了代码。我的决定

---在ListFragment ---

private ListView lv;    
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(AdapterView<?> adapter, View view,
                                               int position, long id) {

                    lv.setItemChecked(position, !lv.isItemChecked(position));
                    mAdapter.setCheckedItems(position, lv.isItemChecked(position));

                    return true;
                }
            });

---在适配器 -

    private boolean [] checkedItems;

      public void setCheckedItems(int position, boolean state){
            checkedItems[position] = state;
            notifyDataSetChanged();
        }

        public void unCheckAllItems(){
            for (int i=0; i<checkedItems.length; i++){
                checkedItems[i] = false;
            }
        }

private void SetDataView(ViewHolder holder, int position){

        if (checkedItems[position]){
            holder.operImgCheck.setVisibility(View.VISIBLE);
            holder.operImg.setVisibility(View.INVISIBLE);
        } else {
            holder.operImgCheck.setVisibility(View.INVISIBLE);
            holder.operImg.setVisibility(View.VISIBLE);
        }
}

问题的主要原因:我应该更改并重建适配器,但不要从ListView更改项目!不要使用ListView访问ListView项目来更改它们。