ListView - 更改项目的孩子

时间:2015-03-28 06:27:25

标签: android listview android-listview android-view android-adapter

当我点击项目时,其图标会发生变化。但此外一些其他图标也在变化。每9或10个图标都会发生变化。看起来我在每个"屏幕"上更改了图标。我不明白为什么4天。请帮忙。

http://i.imgur.com/iDbawLO.png

http://i.imgur.com/5cce4iy.png

此处我的ListView位于 activity_main.xml

<ListView
    android:id=                     "@+id/lvSimple"
    android:layout_width=           "fill_parent"
    android:layout_height=          "wrap_content"
    android:paddingTop=             "8dp"
    android:paddingBottom=          "8dp"
    android:paddingLeft=            "0dp"
    android:paddingRight=           "0dp"
    android:divider=                "@null"
    android:dividerHeight=          "0dp"
    android:choiceMode="multipleChoice"
    android:descendantFocusability= "blocksDescendants">
</ListView>

list_view_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="56dp"
    android:orientation="horizontal"
    android:padding="0dp"
    android:background="@drawable/change_color_on_press"
    android:checkable="true"
    android:id="@+id/item">
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/icon">
        <ImageView
            android:id="@+id/iconFront"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:src="@drawable/ic_folder_white_grey600_36dp">
        </ImageView>
        <ImageView
            android:id="@+id/iconBack"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:alpha="0"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:src="@drawable/ic_check_circle_grey600_white_36dp">
        </ImageView>
    </FrameLayout>
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent"
        android:gravity="start"
        android:paddingTop="16dp"
        android:paddingLeft="0dp"
        android:paddingRight="32dp"
        android:textSize="16sp"
        android:text="TextView">
    </TextView>
</LinearLayout>

MainActivity.java

...
lvSimple = (ListView) findViewById(R.id.lvSimple);
sAdapter = createSimpleAdapter(readFolder(current_folder));
lvSimple.setAdapter(sAdapter);
...
    lvSimple.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent,
        View view, int position, long id) {
            ImageView child1_img = (ImageView) view.findViewById(R.id.iconFront);
            ImageView child2_img = (ImageView) view.findViewById(R.id.iconBack);

            final AnimatorSet setLeftIn = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(),
                    R.animator.flip_left_in);

            final AnimatorSet setLeftOut = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(),
                    R.animator.flip_left_out);

            if(lvSimple.isItemChecked(position)){
                setLeftOut.setTarget(child1_img);
                setLeftIn.setTarget(child2_img);
                setLeftOut.start();
                setLeftIn.start();
                lvSimple.setItemChecked(position, true);
            }
            else{
                setLeftOut.setTarget(child2_img);
                setLeftIn.setTarget(child1_img);
                setLeftOut.start();
                setLeftIn.start();
                lvSimple.setItemChecked(position, false);
            }
        }
    });

...
public String[] readFolder(String path) {
    try {
        if ((new File(path)).isDirectory()) {
            File file = new File(path);
            File[] files = file.listFiles();
            ArrayList<String> file_names = new ArrayList<String>();
            ArrayList<String> dir_names = new ArrayList<String>();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) dir_names.add(files[i].getName());
                else file_names.add(files[i].getName());
            }
            Collections.sort(dir_names);
            Collections.sort(file_names);
            String[] texts = new String[dir_names.size() + file_names.size()];
            int count = 0;
            for (int i = 0; i < dir_names.size(); i++) {
                texts[count] = dir_names.get(i);
                count++;
            }
            for (int i = 0; i < file_names.size(); i++) {
                texts[count] = file_names.get(i);
                count++;
            }
            if (texts.length == 0) {
                list_view_error = true;
                sToast("Directory is empty");
            }
            return texts;
        }
        else {
            sToast("It's file");
            list_view_error = true;
            return (new String[0]);
        }
    } catch (Exception e) {
        sToast("Directory is empty");
        list_view_error = true;
        return (new String[0]);
    }
}
public SimpleAdapter createSimpleAdapter(String[] texts) {
    data = new ArrayList<Map<String, Object>>(texts.length);
    for (int i = 0; i < texts.length; i++) {
        m = new HashMap<String, Object>();
        m.put(ATTRIBUTE_NAME_TEXT, texts[i]);
        if ((new File(current_folder + texts[i])).isDirectory())
            m.put(ATTRIBUTE_NAME_IMAGE, img_folder);
        else m.put(ATTRIBUTE_NAME_IMAGE, img_file);
        data.add(m);
    }
    String[] from = {ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_IMAGE};
    int[] to = {R.id.text, R.id.iconFront};
    return new SimpleAdapter(this, data, R.layout.list_view_item, from, to);
}

1 个答案:

答案 0 :(得分:0)

ListView仅通过绘制屏幕上可见的列表行来提高应用的效果。这意味着一旦一行离开屏幕,那么它就会被刷新,除非存储在它上面的任何内容被保存到某个地方以便稍后检索信息,否则它就会消失。滚动时图像会随之改变。

要抵消这种情况,您可以保存对要显示的正确图像的引用,以及稍后检索的图像,我将对ArrayList进行处理。

OnItemClickListener中,而不是拥有以下内容:

 if(lvSimple.isItemChecked(position)){
                setLeftOut.setTarget(child1_img);
                setLeftIn.setTarget(child2_img);
                setLeftOut.start();
                setLeftIn.start();
                lvSimple.setItemChecked(position, true);
            }
            else{
                setLeftOut.setTarget(child2_img);
                setLeftIn.setTarget(child1_img);
                setLeftOut.start();
                setLeftIn.start();
                lvSimple.setItemChecked(position, false);

ArrayList中创建一个新的onCreate

checkedItems = new ArrayList<>(texts.length); //Assuming texts contains the data you use to populate your ListView

并在for循环中填写您想要的默认值(我假设为"unchecked")。

OnItemClickListener代码替换为以下内容:

if(checkedItems.get(position).equals("unchecked"){
    //...whatever you want to happen
    checkedItems.set(position, "checked");
    lvSimple.setItemChecked(position, true);
} else {
    //the opposite
}

然后,创建自定义ListAdapterOverride getView()方法。

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

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) parent.getContext().
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.row, parent, false);
    } else {
        view = convertView;
    }

    ImageView checkedImage = (ImageView)view.findViewById(//yourImageViewPath);            

    if (checkedItems.get(position).equals("checked"){
        checkedImage.setImageDrawable(//drawable path);
    } else {
        //the opposite
    }

    return view;

希望这是您正在寻找的并将解决您的问题。