5

アイテムのロングクリックリスナーを追加します。これにより、選択した画像が壁紙として設定されます。Webから画像を取得し、グリッドビューで表示しています。私のグリッドアクティビティを以下に示します。フルスクリーン画像を表示するためのオンクリックリスナーがすでにあります。

public class ImageGridActivity extends BaseActivity {

String[] imageUrls;

DisplayImageOptions options;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.ac_image_grid);

    Bundle bundle = getIntent().getExtras();
    imageUrls = bundle.getStringArray(Extra.IMAGES);

    options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.stub_image)
            .showImageForEmptyUri(R.drawable.image_for_empty_url)
            .cacheInMemory().cacheOnDisc()
            .bitmapConfig(Bitmap.Config.RGB_565).build();

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter());

    // Set Long-Clickable
    gridView.setLongClickable(true);
    gridView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @SuppressLint("NewApi")
        public boolean onItemLongClick(AdapterView<?> parent, View arg1,
                int position, long arg3) {
            // TODO Auto-generated method stub
            ImageAdapter i = (ImageAdapter) parent.getAdapter();
            Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
                    (int) i.getItemId(position));

            // Get the WallpaperManager
            WallpaperManager myWallpaperManager = WallpaperManager
                    .getInstance(getApplicationContext());

            try {
                // Set the clicked bitmap
                myWallpaperManager.setBitmap(mBitmap);
                Toast.makeText(ImageGridActivity.this, "Wallpaper set",
                        Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                Toast.makeText(ImageGridActivity.this, "Error setting wallpaper",
                        Toast.LENGTH_SHORT).show();
            }

            return false;
        }
    });

    gridView.setOnItemClickListener(new OnItemClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            startImageGalleryActivity(position);
        }
    });

    gridView.setOnScrollListener(new PauseOnScrollListener(true, true));

}

private void startImageGalleryActivity(int position) {
    Intent intent = new Intent(this, ImagePagerActivity.class);
    intent.putExtra(Extra.IMAGES, imageUrls);
    intent.putExtra(Extra.IMAGE_POSITION, position);
    startActivity(intent);
}

public class ImageAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return imageUrls.length;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ImageView imageView;
        if (convertView == null) {
            imageView = (ImageView) getLayoutInflater().inflate(
                    R.layout.item_grid_image, parent, false);
        } else {
            imageView = (ImageView) convertView;
        }

        imageLoader.displayImage(imageUrls[position], imageView, options);
        return imageView;
    }
}

logcat

     01-22 16:00:35.101: E/AndroidRuntime(29785): FATAL EXCEPTION: main
01-22 16:00:35.101: E/AndroidRuntime(29785): android.content.res.Resources$NotFoundException: Resource ID #0x2
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.content.res.Resources.getValue(Resources.java:1105)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:554)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:630)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at com.nostra13.example.universalimageloader.ImageGridActivity$1.onItemLongClick(ImageGridActivity.java:67)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.widget.AbsListView.performLongPress(AbsListView.java:2622)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2572)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.os.Handler.handleCallback(Handler.java:608)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.os.Looper.loop(Looper.java:156)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.app.ActivityThread.main(ActivityThread.java:4987)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at java.lang.reflect.Method.invokeNative(Native Method)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at java.lang.reflect.Method.invoke(Method.java:511)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at dalvik.system.NativeStart.main(Native Method)

アンドロイドマニフェスト

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nostra13.example.universalimageloader"
    android:versionCode="26"
    android:versionName="1.7.0" >

    <uses-sdk
        android:minSdkVersion="5"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.SET_WALLPAPER"/>

    <application
        android:name=".UILApplication"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name" 
        android:allowBackup="True"
        >
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".ImageGridActivity"
            android:label="@string/ac_name_image_grid" />
        <activity
            android:name=".ImagePagerActivity"
            android:label="@string/ac_name_image_pager" />


        <!-- Widget -->
        <receiver android:name=".widget.UILWidgetProvider">
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_provider" />

            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
4

6 に答える 6

14

OnItemLong listener代わりに使用してくださいOnItemClick Listener

それ以外の:

gridView.setOnItemClickListener(new OnItemClickListener() {
        @SuppressLint("NewApi")
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            startImageGalleryActivity(position);
        }
    });

使用する

 gridView.setOnItemLongClickListener(new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
            int position, long arg3) {
       startImageGalleryActivity(position);
        return false;
    }
});

更新しました

OnItemClickListener

 gridview.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
                  Toast.makeText(test2.this, "Click Listener", Toast.LENGTH_SHORT).show(); 
                   // open the pager activity
                  }    });

OnItemLongClickListener

gridview.setOnItemLongClickListener(new OnItemLongClickListener() {

                    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                            int position, long arg3) {
                        Toast.makeText(test2.this, "LONG PRESS", Toast.LENGTH_SHORT).show();
                        //set the image as wallpaper 
                        return true;
                    }
                }); 
于 2013-01-21T09:39:13.787 に答える
1

代わりにこれを使用してください:

    gridView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            return false;
        }
    });
于 2013-01-21T09:36:59.480 に答える
1

コードにこの部分を記述して、長く設定します。gridviewをクリックします

    GridView  gridView =  (GridView) findViewById(R.id.gridview);
    //Set Long-Clickable
    gridView.setLongClickable(true);
    gridView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View view,
                int position , long id) {
            // TODO Auto-generated method stub
                       Toast.makeText(getApplicationContext(), "Long Click", Toast.LENGTH_SHORT).show();
                            /*
                             *You Can use parameters like position,view or id to
                             *Customize your action
                             */   
            return false;
        }
    });

リスナーをクリックします

gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
    Toast.makeText(getApplicationContext(), "Click", Toast.LENGTH_SHORT).show();
        }
    });

壁紙を設定するには許可を設定する必要があると思います

   <uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission> 

注: log catを見て、Resources$NotFoundExceptionリソースが正しく追加されたことを確認してください。間違ったフォルダにあるか、 これを確認してください

于 2013-01-21T09:41:12.487 に答える
1

まず、この行を変更します。

public class ImageGridActivity extends BaseActivity implements OnItemLongClickListener

次に、このコードをアクティビティに追加します...

 GridView gridView = (GridView) findViewById(R.id.gridview);
 gridView.setOnItemLongClickListener(this);

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View v, int pos,long id) 
    {
        System.out.println("~~~~~~Long Item Clicked");
        return false;
    }

お役に立てれば :)

于 2013-01-21T09:58:29.997 に答える
1

これを試して。onItemLongClickでtrueを返し、onItemClickListenerは呼び出されません:)

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
.
.
.
return true;
}
于 2014-01-15T00:05:13.087 に答える
0

使用できます

gridView.setOnLongClickListener(new OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    // TODO Auto-generated method stub
                    return false;
                }
            });
于 2013-01-21T09:37:35.373 に答える