当用户单击它时,如何设置和保存 ListView 中某些项目的背景颜色?

How can I set and save background color for some item in the ListView, when the user clicks on it?

我想在我的列表视图中更改某些项目的背景颜色,当用户单击它时。此外,我想将其保存在共享首选项中。我为 ListView 使用适配器。我该怎么办?

我的类别适配器class:

public class CategoryAdapter extends ArrayAdapter<Category> {

    private LayoutInflater mInflater;
    private int mResource;

    public CategoryAdapter(Context context, int resource, List<Category> categories) {
        super(context,resource,categories);

        mResource = resource;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView( int position, View convertView, ViewGroup parent )
    {
        View view = convertView == null ? mInflater.inflate( mResource, parent, false ) : convertView;
        TextView categoryTitle = (TextView) view.findViewById( R.id.name );

        Category item = getItem( position );

        categoryTitle.setText( item.getCategoryTitle() );

        return view;
    }

}

我想在哪里使用它:

CategorylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {

             }
        });

类别列表视图:

CategorylistView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,category){
           @Override
           public View getView(final int position, final View convertView, final ViewGroup parent) {
 if(catViewPreferences.getInt("scn",0)==position){
                   textView.setBackgroundColor(Color.BLUE);
               }
return textView;
           }
       });

OnItemClickListener:

CategorylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {

                final String category = (String) adapterView.getItemAtPosition(i);
                final String email = loginPreferences.getString("email","");
                final String password = loginPreferences.getString("password", "");

                final int position =  adapterView.getSelectedItemPosition();

                if(i<8) {
                    view.setBackgroundColor(getResources().getColor(R.color.colorLightBlue));
                }

                catViewPrefEditor.putInt("scn", position);
                catViewPrefEditor.commit();

使用状态列表Drawable https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

在您的布局文件中指定一个看起来像这样的可绘制文件

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/button_bg_focused"/>
    <item android:state_selected="true" android:drawable="@drawable/button_bg_selected"/>
    <item android:drawable="@drawable/button_bg_default"/>
</selector>

然后在button_bg_selected文件中写这样的东西

<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <corners  android:radius="22px"/>
    <solid android:color="@color/black"/>
</shape>

使用您制作的自定义适配器

CategorylistView.setAdapter(new CategoryAdapter(this,android.R.layout.simple_list_item_1,categories)); 

您可以通过在 onItemClick 方法上更改 view 的背景来实现:

public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
          view.setBackgroundColor(Color.BLUE);
     }

更新: 如果你想取消选择项目,你可以像这样更改顶部方法:

public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
          if(((ColorDrawable)view.getBackground()).getColor() == Color.Blue){
              //the item is selected...now u must deselect it
              view.setBackgroundColor(Color.WHITE);
          } else {
              view.setBackgroundColor(Color.BLUE);
          }
     }

希望对您有所帮助。