如何切换 ListView 项目颜色并保存?

Posted

技术标签:

【中文标题】如何切换 ListView 项目颜色并保存?【英文标题】:How to toggle ListView item colors and Save it? 【发布时间】:2017-03-26 15:45:03 【问题描述】:

我正在开发一个考勤应用程序。该应用程序有ListActivity 由学生列表组成。我想将颜色更改为红色和绿色,表示不存在和存在。问题是如果我的学生名单比屏幕长。当前视图向下或向上的项目会失去颜色。我的方法正确吗?如何保存每个单独列表项的颜色。当然,如果除了使用ListView 之外还有其他最佳方法,我愿意接受建议。

这是包含列表的 ListActivity

public class ListActivity extends Activity implements Serializable
private String userName;
private TextView nameTextView;
private ListView nameList;
private CustomAdapter adapter;
private boolean colorRed;
private Class myClass;

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

    myClass = new Class();
    colorRed = false;
//         Open the students list from file if exists
    openFromFile();

    nameList = (ListView) findViewById(R.id.nameListView);
    nameTextView = (TextView) findViewById(R.id.nameTextView);
    Bundle extras = getIntent().getExtras();
    if (extras != null) 
        userName = extras.getString("NAME");
        nameTextView.setText("Hi! " + userName);
    

    adapter = new CustomAdapter(this, myClass.getStudentNames());
    nameList.setAdapter(adapter);
    TextView tv = (TextView) findViewById(R.id.sNametv);
    nameList.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) 

            if (colorRed == true) 
                view.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.colorPresent));
                colorRed = false;
             else 
                view.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.colorAbsent));
                colorRed = true;
            

//              ALso  could use this
//              adapterView.getChildAt(position).setBackgroundColor(Color.RED);

        
    );

我使用boolean colorRed 来检查颜色,因为我无法将视图颜色与colors xml 的颜色进行比较。

nameList.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) 
            if (colorRed == true) 
                view.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.colorPresent));
                colorRed = false;
             else 
                     view.setBackgroundColor(ContextCompat.getColor(getBaseContext(),    R.color.colorAbsent));
                colorRed = true;
            
        
    );

ListView 的CustomAdapter 类

public class CustomAdapter extends ArrayAdapter<String> 

public CustomAdapter(Context context, ArrayList<String> names) 
    super(context, R.layout.custom_layout, names);



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

    LayoutInflater inflater = LayoutInflater.from(getContext());
    View customView = inflater.inflate(R.layout.custom_layout, parent, false);
    String singleName = getItem(position);
    TextView tv = (TextView) customView.findViewById(R.id.sNametv);
    tv.setText(singleName);

    return customView;


我已经仔细检查了这是一个独特的问题!放心……

提前致谢

【问题讨论】:

你使用的是POJO模型类? 请明确! 如果您将模型类用于列表项,那么您可以为每个项目创建另一个整数值并将其值设置为 1 或 0。然后在您的 getView 方法中检查该值并根据该值更改颜色。 好主意!但颜色变化不是永久性的。堆叠更多列表项后,颜色将丢失为默认的灰色。 您希望您的列表项永久使用另一种颜色? 【参考方案1】:

创建监听器接口:

public interface ListListener 
     void clickListItem(int position);

这是模型类:

public class Route 
    String studentName;
    boolean colorRed;

    public Route(String studentName, boolean colorRed) 
        this.studentName=studentName;
        this.colorRed=colorRed;
    

    public String getStudentName() 
        return studentName;
    

    public void setStudentName(String studentName) 
        this.studentName = studentName;
    

    public boolean isColorRed() 
        return colorRed;
    

    public void setColorRed(boolean colorRed) 
        this.colorRed = colorRed;
    

创建适配器类:

public class AAdapter  extends BaseAdapter implements View.OnClickListener 

    Context context;
    private List<Route> routes;
    Holder holder;
    private static LayoutInflater inflater=null;
    ListListener listListener;

    public AAdapter(Context context, List<Route> names,ListListener listListener) 
       this.routes=names;
        this.context=context;
        this.listListener=listListener;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    

    @Override
    public void onClick(View view) 
        listListener.clickListItem((Integer)view.getTag());
    

    private class Holder
    
        TextView tv;
    

    @Override
    public Route getItem(int position) 
        return routes.get(position);
    

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

    @Override
    public int getCount() 
        return routes.size();
    

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

        if (convertView==null)
            holder=new Holder();

            convertView=inflater.inflate(R.layout.custom_layout,null);

            holder.tv=(TextView)convertView.findViewById(R.id.textView);
            holder.tv.setOnClickListener(this);
            convertView.setTag(holder);

        else 
            holder=(Holder)convertView.getTag();
        

        holder.tv.setText(routes.get(position).getStudentName());
        holder.tv.setTag(position);

        if (!routes.get(position).colorRed)
            holder.tv.setBackgroundColor(Color.GREEN);
        else 
            holder.tv.setBackgroundColor(Color.RED);
        
        return convertView;
    

现在 MainActivity 类:

public class MainActivity extends AppCompatActivity implements ListListener

    AAdapter adapter;
    ListView lv;
    List<Route> myNames;
    ListListener listListener=MainActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView)findViewById(R.id.listnames);


        myNames=new ArrayList<>();

        /* DEMO DATA U NEED TO FETCH YOUR DATA */
        myNames.add(new Route("FIRST",false));
        myNames.add(new Route("Second",false));
        myNames.add(new Route("Third",false));
        myNames.add(new Route("Fourth",false));
        myNames.add(new Route("Fifth",false));

        myNames.add(new Route("FIRST",false));
        myNames.add(new Route("Second",false));
        myNames.add(new Route("Third",false));
        myNames.add(new Route("Fourth",false));
        myNames.add(new Route("Fifth",false));

        myNames.add(new Route("FIRST",false));
        myNames.add(new Route("Second",false));
        myNames.add(new Route("Third",false));
        myNames.add(new Route("Fourth",false));
        myNames.add(new Route("Fifth",false));

        myNames.add(new Route("FIRST",false));
        myNames.add(new Route("Second",false));
        myNames.add(new Route("Third",false));
        myNames.add(new Route("Fourth",false));
        myNames.add(new Route("Fifth",false));


        adapter = new AAdapter(this, myNames,listListener);
        lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    

    @Override
    public void clickListItem(int position) 
        if(myNames.get(position).colorRed)
           myNames.get(position).colorRed=false;
        else 
            myNames.get(position).colorRed=true;
        
        adapter.notifyDataSetChanged();
    

【讨论】:

当然,很高兴与您合作。在下一个问题出现之前见! 我也是。再见 你能帮我this problem 我不知道该问题的任何解决方案。

以上是关于如何切换 ListView 项目颜色并保存?的主要内容,如果未能解决你的问题,请参考以下文章

如何根据位置更改listView项目的颜色

单击后如何禁用 ListView 项目?

如何在颤动/飞镖的 ListView 项目中更改背景颜色

Android如何在每个listview列表项上添加图标并更改文本颜色、背景颜色

ListView:如何从列表中访问视图以更改背景颜色?

在 Android 上更改 ListView 项目的背景颜色