简体   繁体   中英

I have a grid view of images.I am selecting one image from it and showing it in activity2.xml.Now I want to drag this image view .How to do this?

I have one grid.class from where I am selecting an image and showing it in activity_main.xml. Whenever I chose an image its id will be "imageChosen". I am able to drag any image taken from drawable folder but not this dynamically chosen image,with image id.

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_vertical_margin" >
<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/image1"
    android:src="@mipmap/ic_launcher"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
   </RelativeLayout>

I don't really understand what you are saying but assuming you have a grid adapter with some images , so you select one and you want that image to be shown in main activity; all you need is this:

1) create an array with all the images refs :

public class Images{
    // references to our images
    private Integer[] mIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
} 

2) On your item click in grid view send the index to the displayActivity:

 gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Intent mInDisplay= new Intent(MainActivity.this, DisplayActivity.class);
                mInDisplay.putExtra("Index", position);
                startActivity(mInDisplay);
            }
        });

3) And in the DisplayActivity display the img :

 //onCreate()
        Bundle bundle=getIntent().getExtras();
                int index=bundle.getInt("Index");      
                ImageView mImage = (ImageView) findViewById(R.id.Img);
                mImage.setImageResource(Images.mIds[index]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM