Android GridView突出显示所选项目

时间:2015-08-31 20:33:05

标签: android android-gridview

我正在尝试为我构建一个基本的颜色选择器,我会在其中显示10种颜色的列表供用户选择。

我决定使用GridView来保存我的情况下的颜色列表和使用ShapeDrawables的ImageView(希望听到更好的解决方案)。

我无法弄清楚如何在点击时突出显示所选项目以及如何同时保留一个项目(单选模式)。

知道怎么做?

CODE:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

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

        GridView gridView = (GridView) findViewById(R.id.mygrid);
        gridView.setAdapter(new MyAdapter(this));
        gridView.setOnItemClickListener(this);
    }

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

        // Selected color
        int color = (int) adapterView.getItemAtPosition(i);
        // Selected view
        ImageView bla = (ImageView) view;
    }

    public class MyAdapter extends BaseAdapter {

        private final Context c;

        public MyAdapter(Context c) {
            this.c = c;
        }

        @Override
        public int getCount() {
            return colors.length;
        }

        @Override
        public Object getItem(int i) {
            return colors[i];
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            ImageView ivColor;

            if (view == null) {

                LinearLayout.LayoutParams lpParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                lpParams.gravity = Gravity.CENTER;

                ivColor = new ImageView(c);
                ivColor.setLayoutParams(lpParams);
            } else {

                ivColor = (ImageView) view;
            }

            ShapeDrawable shape = new ShapeDrawable(new OvalShape());
            shape.getPaint().setColor(colors[i]);
            shape.setIntrinsicWidth(180);
            shape.setIntrinsicHeight(180);

            ivColor.setImageDrawable(shape);

            return ivColor;
        }

        private int[] colors = {
                Color.BLUE, Color.BLACK, Color.CYAN, Color.GREEN, Color.YELLOW, Color.DKGRAY, Color.MAGENTA, Color.RED
        };
    }
}

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <GridView
        android:id="@+id/mygrid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:columnWidth="64dp"
        android:horizontalSpacing="8dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="8dp" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

GridView上有一个“setSelection”方法,可用于维护当前选定的项目位置。您可以在后台或布局中的某个位置使用选择器来更改外观:

http://developer.android.com/guide/topics/resources/color-list-resource.html

你想要的是“state_selected”。因此,如果您尝试设置背景颜色,请创建一个这样的可绘制xml文件,并将其设置为颜色样本的背景。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:state_selected="true" android:color="hex_color" />
  <item android:state_selected="false" android:color="hex_color_unselected" />
</selector>

您可以执行许多选项,例如在选定的样本周围放置边框等等......但这是基本想法。