All image settings in Android ViewPager listen to different images displayed in the same activity,

Source: Internet
Author: User

All image settings in Android ViewPager listen to different images displayed in the same activity,

See the previous article for monitoring all image settings in Android ViewPager.

In order to save time, the two-layer menu only displays the corresponding image activity after clicking any image.

The key point is how to pass parameters to the displayed activity for the image corresponding to each click.

Because only one activity is started, you need to notify the activity of the image to be displayed and the code of the image to be displayed.

demoimageview = (ImageView) findViewById(R.id.demoimageView);demoimageview.setImageResource(imageid);

That is to pass the ID of the displayed image, but all the images in ViewPager are set in the layout.

How can I set the corresponding image to be displayed? If I write the image to be displayed in the layout, I just need to read it when I use it.

There is an android: tag available after querying the information, and then there are SetTag () and GetTag (), which can be put on everything, but SetTag () must be in

Code execution, do not want to execute every image once, just want to add android: tag, but refer to the following example to find android: tag

It seems that only strings can be placed.

Http://blog.csdn.net/xxyy888/article/details/47019143

Write android: tag = "@ drawable/tuijian_01" GetTag () and convert it to int, but an error occurs in the past.

After multiple tests, it is determined to use a string to find the method for searching image resource IDs through strings.

Http://blog.csdn.net/wo550415979/article/details/8786577

Http://www.tuicool.com/articles/7vmaUrv

Final Solution

Layout

android:id="@+id/page1_item1"android:tag="tuijian_d01"

Tuijian_d01 is the name of the image to be displayed with the corresponding ID. Set the listener first, use the ID to determine and then GetTag to obtain the string.

Search for the image resource ID, pass the parameter, and start the activity. The Code is as follows:

The listening code. All clicks start the same activity.

@ Override public void onClick (View view) {switch (view. getId () {case R. id. mainpagelogo: mOpenTabHost. setCurrentTab (1); break; default: // Toast. makeText (this, "Click OK" + view. getId (), Toast. LENGTH_SHORT ). show (); Intent intent = new Intent (this, DemoImageViewActivity. class); // obtain the Tag Object tag = view from the layout. getTag (); // exit if (Tag = null) {break;} String test = tag if no tag is set. toString (); // ctx. getPackageName () // Context ctx = getBaseContext (); // you can use a string to find the image resource ID. If this error occurs, the preceding method int imageid = getResources () is used (). getIdentifier (test, "drawable", this. getPackageName (); // upload the obtained ID to the next active intent. putExtra ("ImageID", imageid); startActivity (intent); break ;}}

 

Started activities

public class DemoImageViewActivity extends Activity {    private ImageView demoimageview;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_demo_image_view);        Intent intent = getIntent();        int imageid = intent.getIntExtra("ImageID", 0);        if (imageid == 0) {            finish();        }        demoimageview = (ImageView) findViewById(R.id.demoimageView);        demoimageview.setImageResource(imageid);        demoimageview.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {              finish();            }        });    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.