在Android Gridview中突出显示项目

时间:2017-07-02 10:32:00

标签: java android gridview

我花了很多时间搜索S.O.但没有结果。

我的Android应用中有一个Gridview。我想有可能检查多个项目,这可以通过

工作
gridview.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);

然而,当我选择一个项目时,没有图形显示它被选中...这是我的问题:如何使***能够使项目的背景为任何颜色?

尝试使用listSelector ...不起作用!

我的XML:     

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads='http://schemas.android.com/apk/res-auto'
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.rollercoders.smartkedex.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/background"/>

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:listSelector="@drawable/list_selector"
    />

    <com.google.android.gms.ads.AdView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/AdView"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize='BANNER'
        ads:adUnitId="@string/banner_ad_unit_id"/>

</RelativeLayout>

我的JAVA代码

    final GridView gridview = (GridView) findViewById(R.id.gridview);
    ImageAdapter adapter = new ImageAdapter(this);
    gridview.setAdapter(adapter);
    gridview.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Intent i = new Intent(getApplicationContext(), PokemonDetails.class);
            i.putExtra("id", position);
            startActivity(i);
        }
    });

    gridview.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

            int selectCount = gridview.getCheckedItemCount();
            switch (selectCount) {
                case 1:
                    mode.setSubtitle("One item selected");
                    break;
                default:
                    mode.setSubtitle("" + selectCount + " items selected");
                    break;
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.setTitle("Select Items");
            mode.setSubtitle("One item selected");
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    });

我的ImageAdapter类

class ImageAdapter extends BaseAdapter {
private Context mContext;

ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
Integer[] mThumbIds = {/*here there are all the referenced images*/}

0 个答案:

没有答案