在库视图中更改选定的视图背景颜色

时间:2016-03-09 04:47:31

标签: android android-layout android-intent

我想更改所选视图的背景颜色,但在下面的代码之后我得到了错误。

当用户选择画廊上一个视图的其他视图变为默认颜色但现在我得到以下错误

当我在图库视图中更改选定项目时出现错误

gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

        @SuppressLint("ResourceAsColor")
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // if(position != save)
            // parent.getChildAt(position).setBackgroundResource(R.drawable.state_selected);

            view.setBackgroundColor(Color.GREEN);
            if (save != -1) {
                parent.getChildAt(save).setBackgroundColor(Color.RED);
            }

            save = position;

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }

    });

日志

java.lang.NullPointerException
    at com.example.csglobal.MainActivity$2.onItemSelected(MainActivity.java:119)
    at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
    at android.widget.AdapterView.selectionChanged(AdapterView.java:879)
    at android.widget.Gallery.selectionChanged(Gallery.java:546)
    at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1043)
    at android.widget.Gallery.setSelectionToCenterChild(Gallery.java:592)
    at android.widget.Gallery.trackMotionScroll(Gallery.java:396)
    at android.widget.Gallery$FlingRunnable.run(Gallery.java:1504)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

它对我来说很好,试试这个: -

MainActivity.java: -

公共类MainActivity扩展了Activity {

            int save = -1;

Integer[] imageIDs = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
                    R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7 };

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

                // Note that Gallery view is deprecated in Android 4.1---
                Gallery gallery = (Gallery) findViewById(R.id.gallery1);
                gallery.setAdapter(new ImageAdapter(this));     

                gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view,
                            int position, long id) {
                        // TODO Auto-generated method stub
                        view.setBackgroundColor(Color.GREEN);

                        if (save != -1) {
                            parent.getChildAt(save).setBackgroundColor(Color.RED);
                        }

                        save = position;
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                        // TODO Auto-generated method stub

                    }

                });
            }

公共类ImageAdapter扩展了BaseAdapter {

私有上下文上下文;       private int itemBackground;

  public ImageAdapter(Context c) {
                    context = c;
                    // sets a grey background; wraps around the images
                    TypedArray a = obtainStyledAttributes(R.styleable.MyGallery);
                    itemBackground = a.getResourceId(
                            R.styleable.MyGallery_android_galleryItemBackground, 0);
                    a.recycle();
                }

                // returns the number of images
                public int getCount() {
                    return imageIDs.length;
                }

                // returns the ID of an item
                public Object getItem(int position) {
                    return position;
                }

                // returns the ID of an item
                public long getItemId(int position) {
                    return position;
                }

                // returns an ImageView view
                public View getView(int position, View convertView, ViewGroup parent) {
                    ImageView imageView = new ImageView(context);
                    imageView.setImageResource(imageIDs[position]);
                    imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
                    imageView.setBackgroundResource(itemBackground);
                    return imageView;
                }
            }
        }

activity_main.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"
            tools:context="${relativePackage}.${activityClass}"
            android:orientation="vertical" >

            <Gallery
                android:id="@+id/gallery1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <ImageView
                android:id="@+id/image1"
                android:layout_width="215dp"
                android:layout_height="315dp"
                android:layout_gravity="center_horizontal"
                android:background="#cfcfcf"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:src="@drawable/pic1" />

        </LinearLayout>

在res-&gt; values-&gt; attrs.xml中添加以下行: -

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyGallery">
    <attr name="android:galleryItemBackground" />
    </declare-styleable>  
</resources>
相关问题