android 中的自定义 listAdapter 中的可绘制图像在滚动列表视图后发生变化。为什么?

Replaced drawable images in the custom listAdapter in android changes after scrolling the listview. Why?

我使用 CustomBaseAdapter 来表示 listView 中的列表。 MainActivity 显示在下面第一个图像中的 link 图像中,其中包含自定义 listView 适配器。现在,当我单击位置编号 [4] 中的其中一项时,可绘制图像会随着填充颜色而变化,如第二张图像所示。当我再次滚动 listView 时,填充的可绘制颜色消失并与之前的状态保持一致。那么我怎样才能做到这一点?

Image Link to understand Clearly..

SingleRow.java

package androidhiveexample.jsonparsing.circularimageview;

import android.content.Context;

public class SingleRow {

    private String title , number ;
    private int image;
    Context context;

    public SingleRow(Context context){
        this.context = context;
    }
    public SingleRow(int image , String title , String number){
        this.title = title;
        this.image = image;
        this.number = number;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}

CustomListAdapter.java

package androidhiveexample.jsonparsing.circularimageview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;

public class CustomListAdapter extends BaseAdapter
{
    ArrayList<SingleRow> rowArrayList;
    Context context;

    public CustomListAdapter(Context context , ArrayList<SingleRow> rowArrayList)
    {
        this.context = context;
        this.rowArrayList = rowArrayList;
    }
    @Override
    public int getCount() {
        return rowArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return rowArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return rowArrayList.indexOf(getItem(i));
    }

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

        ViewHolder holder = null;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

        if(convertView == null){
            convertView = inflater.inflate(R.layout.single_row_view , null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.textView);
            holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            holder.number = (TextView) convertView.findViewById(R.id.textViewNumber);
            convertView.setTag(holder);

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

        SingleRow singleRow = (SingleRow) getItem(position);

        holder.title.setText(singleRow.getTitle());
        holder.imageView.setImageResource(singleRow.getImage());
        holder.number.setText(singleRow.getNumber());


        return convertView;
    }

    private class ViewHolder
    {
        TextView title , number;
        ImageView imageView;
    }
}

MainActivity.java

package androidhiveexample.jsonparsing.circularimageview;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends ActionBarActivity
{
    ListView lv;
    String number[] = {"1" , "2" , "3" ,"4" , "5" };
    String title[] = {"Title_1_titlename" , "Title_1_titlename" , "Title_1_titlename" ,"Title_1_titlename" , "Title_1_titlename" };
    int icons[] = {R.drawable.one , R.drawable.two , R.drawable.three , R.drawable.four , R.drawable.five};
    ArrayList<SingleRow> singleRowArray = new ArrayList<>();
    SingleRow singleRow;
    static ImageView iv;
    CustomListAdapter customListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.listView);
        loopThroughRows();
        lv.addHeaderView(getLayoutInflater().inflate(R.layout.header, null));
        customListAdapter = new CustomListAdapter(this , singleRowArray);
        lv.setAdapter(customListAdapter);

        ImageView iv1 = (ImageView) findViewById(R.id.imageViewHeader);
        iv1.setImageResource(R.drawable.new_img);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                position = position - 1;
                switch (position) {
                    case 0:
                        iv = (ImageView) view.findViewById(R.id.imageView);
                        // iv.setImageResource(R.drawable.one_background);
                        Log.i("IMAGEVIEW_ID", iv.toString());

                        Fragment_1 fr = new Fragment_1();
                        FragmentManager fm = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fm.beginTransaction();
                        fragmentTransaction.replace(R.id.fragment_container, fr);
                        fragmentTransaction.addToBackStack(null);
                        fragmentTransaction.commit();

                        break;
                    case 1:

                        iv = (ImageView) view.findViewById(R.id.imageView);
                        iv.setImageResource(R.drawable.two_background);

                        break;
                    case 2:

                        iv = (ImageView) view.findViewById(R.id.imageView);
                        iv.setImageResource(R.drawable.three_background);

                        break;
                    case 3:

                        iv = (ImageView) view.findViewById(R.id.imageView);
                        iv.setImageResource(R.drawable.four_background);

                        break;
                    case 4:

                        iv = (ImageView) view.findViewById(R.id.imageView);
                        iv.setImageResource(R.drawable.five_background);


                        break;
                }
            }
        });
    }
    public void loopThroughRows(){
        for(int i = 0 ; i < title.length ; i++){
            singleRow = new SingleRow(icons[i] , title[i] , number[i]);
            singleRowArray.add(singleRow);
        }
    }

    @Override
    public void onBackPressed() {
        FragmentManager fm = getFragmentManager();
        if (fm.getBackStackEntryCount() > 0) {
            fm.popBackStack();
        } else {
            super.onBackPressed();
        }
    }
}

drawable/one.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:thickness="5dp"
    android:useLevel="false"
    >

    <size
        android:height="50dp"
        android:width="50dp"/>

    <solid android:color="#64DD17" />

</shape>

在您的模型中创建或添加该字段 class 以获取列表中项目的状态,然后在您的 getView 中对其进行操作。喜欢下面..

public class StatusBean{

private boolean isActive;


public String isActive() {
    return isActive;
}
public void setNameActive(boolean isActive) {
    this.isActive=isActive;
}

}

然后在单击按钮时将这些字段设置为活动状态。并致电

adapter.notifyDataSetChanged();

并在 getview 内部使用 if-else 相应地更改图像。 如果您遇到任何问题,请告诉我

天哪,挂了8天终于解决了。感谢#AMD 帮助我解决这个问题..

SingleRow.java

    private String title , number ;
    private int defaultImage ;
    private boolean isClicked;
    Context context;

    public SingleRow(Context context){
        this.context = context;
    }

    public SingleRow(int defaultImage , String title , String number)
    {
        this.defaultImage = defaultImage;
        this.title = title;
        this.number = number;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public int getDefaultImage() {
        return defaultImage;
    }

    public void setDefaultImage(int defaultImage) {
        this.defaultImage = defaultImage;
    }

    public boolean isClicked() {
        return isClicked;
    }

    public void setClicked(boolean isClicked) {
        this.isClicked = isClicked;
    }

CustomAdapter.java

public View getView(int position, View convertView, ViewGroup parent) {
        Log.i(TAG , "getView MethodofCustomListAdapter");
        ViewHolder holder = null;
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

        if(convertView == null){
            convertView = inflater.inflate(R.layout.single_row_view , null);
            holder = new ViewHolder();
            holder.icon = (ImageView) convertView.findViewById(R.id.imageViewIcon);
            holder.title = (TextView) convertView.findViewById(R.id.textViewTitle);
            holder.number = (TextView) convertView.findViewById(R.id.textViewNumber);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(arrayList.get(position).getTitle());
        holder.number.setText(arrayList.get(position).getNumber());
        holder.icon.setImageResource(arrayList.get(position).getDefaultImage());


        if(arrayList.get(0).isClicked()) {
            if(position == 0) {
                holder.icon.setImageResource(R.drawable.one_background);
            }
            Log.i("POSITION_CLICKED" , String.valueOf(position));
        }
        if(arrayList.get(1).isClicked()) {
            if(position == 1) {
                holder.icon.setImageResource(R.drawable.two_background);
            }
            Log.i("POSITION_CLICKED" , String.valueOf(position));
        }
        if(arrayList.get(2).isClicked()) {
            if(position == 2) {
                holder.icon.setImageResource(R.drawable.three_background);
            }
            Log.i("POSITION_CLICKED" , String.valueOf(position));
        }
        if(arrayList.get(3).isClicked()) {
            if(position == 3) {
                holder.icon.setImageResource(R.drawable.four_background);
            }
            Log.i("POSITION_CLICKED" , String.valueOf(position));
        }
        if(arrayList.get(4).isClicked()) {
            if(position == 4) {
                holder.icon.setImageResource(R.drawable.five_background);
            }
            Log.i("POSITION_CLICKED" , String.valueOf(position));
        }
        if(arrayList.get(5).isClicked()) {
            if(position == 5) {
                holder.icon.setImageResource(R.drawable.one_background);
            }
            Log.i("POSITION_CLICKED" , String.valueOf(position));
        }
        if(arrayList.get(6).isClicked()) {
            if(position == 6) {
                holder.icon.setImageResource(R.drawable.two_background);
            }
            Log.i("POSITION_CLICKED" , String.valueOf(position));
        }
        if(arrayList.get(7).isClicked()) {
            if(position == 7) {
                holder.icon.setImageResource(R.drawable.three_background);
            }
            Log.i("POSITION_CLICKED" , String.valueOf(position));
        }
        if(arrayList.get(8).isClicked()) {
            if(position == 8) {
                holder.icon.setImageResource(R.drawable.four_background);
            }
            Log.i("POSITION_CLICKED" , String.valueOf(position));
        }


        return convertView;
    }

MainActivity.java

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Log.i(TAG , "onItemClickListener");
            switch (position){
                case 0:
                    arrayList.get(position).setClicked(true);
                    break;
                case 1:
                    arrayList.get(position).setClicked(true);
                    break;
                case 2:
                    arrayList.get(position).setClicked(true);
                    break;
                case 3:
                    arrayList.get(position).setClicked(true);
                    break;
                case 4:
                    arrayList.get(position).setClicked(true);
                    break;
                case 5:
                    arrayList.get(position).setClicked(true);
                    break;
                case 6:
                    arrayList.get(position).setClicked(true);
                    break;
                case 7:
                    arrayList.get(position).setClicked(true);
                    break;
                case 8:
                    arrayList.get(position).setClicked(true);
                    break;
            }
        }
    });
    customListAdapter.notifyDataSetChanged();

我已经更改了你的模型class。

public class SingleRow {

private String title , number ;
private int defaultImage;
private int enabledImage;
private boolean isClicked; 
Context context;

public SingleRow(Context context){
    this.context = context;
}
public SingleRow(int defaultImage , String title , String number){
    this.title = title;
    this.image = image;
     this.number = number;
 }

public void setIsClick(boolean bool){
this.isClicked=bool
}
public void isClick(){
return isClicked;
}
public int getdefaultImage() {
    return image;
}

public void setdefaultImage (int image) {
    this.defaultImage  = defaultImage ;
}

 public int getenabledImage() {
    return enabledImage;
}

public void setEnabledImage (int image) {
    this.enabledImage  = image;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getNumber() {
    return number;
}

public void setNumber(String number) {
    this.number = number;
}

然后在 onClickListener.Call NotifyDatasetChanged() 中将 setIsClick 设置为 true 和 false。

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

ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

if(convertView == null){
    convertView = inflater.inflate(R.layout.single_row_view , null);
    holder = new ViewHolder();
    holder.title = (TextView) convertView.findViewById(R.id.textView);
    holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
    holder.number = (TextView) convertView.findViewById(R.id.textViewNumber);
    convertView.setTag(holder);

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

holder.title.setText(rowArrayList.get(position).getTitle());
holder.imageView.setImageResource(rowArrayList.get(position).getImage());
holder.number.setText(rowArrayList.get(position).getNumber());


if(rowArrayList.get(position).isClicked()) {
    holder.imageView.setImageResource(rowArrayList.get(position).getEnabledImage());
}

else{
    holder.imageView.setImageResource(rowArrayList.get(position).getdefaultImage());
}

return convertView;

}