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

时间:2017-04-23 12:39:27

标签: android listview sharedpreferences background-color

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

我的CategoryAdapter类:

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:

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();

2 个答案:

答案 0 :(得分:0)

使用状态列表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)); 

答案 1 :(得分:0)

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

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);
          }
     }

我希望这会对你有所帮助。