2013-08-31 47 views
-1

如何从一个滚动视图特定iamge ...如何从滚动视图中检索特定图像?

在我的应用我创建了两个活动....活动1,活性2及其相应的XML1,XML2

 ACTIVITY 1: 
      package com.scroll; 

      import android.os.Bundle; 
      import android.app.Activity; 
       import android.content.Intent; 
      import android.view.View; 
      import android.view.View.OnClickListener; 
      import android.widget.Button; 

      public class MainActivity extends Activity { 


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

    Button b1=(Button)findViewById(R.id.button1); 
    b1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 

      Intent in=new Intent(MainActivity.this,Act.class); 
      startActivity(in); 

     } 
    }); 

        Button b2=(Button)findViewById(R.id.button2); 
        b2.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Intent in=new Intent(MainActivity.this,Act.class); 
    startActivity(in); 

       } 
         }); 

         Button b3=(Button)findViewById(R.id.button3); 
        b3.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Intent in=new Intent(MainActivity.this,Act.class); 
    startActivity(in); 

       } 
         }); 
       } 
       } 

活动2: package com.scroll;

   import android.os.Bundle; 
       import android.app.Activity; 

     public class Act extends Activity { 

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

      } 

     } 


       XML 1: 

      <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" 
      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=".MainActivity" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_x="56dp" 
      android:layout_y="75dp" 
       android:text="Button" /> 

       <Button 
       android:id="@+id/button1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
        android:layout_x="56dp" 
       android:layout_y="75dp" 
         android:text="Button" /> 

        <Button 
       android:id="@+id/button1" 
      android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
         android:layout_x="56dp" 
        android:layout_y="75dp" 
        android:text="Button" /> 

        </LinearLayout> 


      XML 2: 

     <AbsoluteLayout 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" 
       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=".MainActivity" > 

        <HorizontalScrollView 
        android:id="@+id/horizontalScrollView1" 
          android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
          android:layout_x="-11dp" 
           android:layout_y="-2dp" > 

           <LinearLayout 
         android:layout_width="match_parent" 
          android:layout_height="match_parent" 
            android:orientation="horizontal" > 

      <ImageView 
       android:id="@+id/imageView1" 
      android:layout_width="566dp" 
      android:layout_height="wrap_content" 
         android:layout_x="-14dp" 
          android:layout_y="-6dp" 
          android:src="@drawable/bear" /> 

            <ImageView 
          android:id="@+id/imageView2" 
           android:layout_width="566dp" 
          android:layout_height="wrap_content" 
             android:layout_x="-14dp" 
         android:layout_y="-6dp" 
        android:src="@drawable/fox" /> 

          <ImageView 
        android:id="@+id/imageView3" 
        android:layout_width="566dp" 
        android:layout_height="wrap_content" 
          android:layout_x="-14dp" 
          android:layout_y="-6dp" 
       android:src="@drawable/goat" /> 
        </LinearLayout> 
        </HorizontalScrollView> 

WEN我选择按钮2,IMAGEVIEW2应开通涡旋VIEW ...和文我选择按钮3,IMAGEVIEW3应开通...如何做到这一点? 亲切帮我解决这个问题....

+0

你好,请避免CAPS,这是不好的。 –

+0

@LeventeKurusa确实。但是,当你在它的时候,你也可以完全删除“请帮助”和类似的东西 - 从询问OP想要帮助的问题已经很明显了:7 –

回答

0

首先避免使用绝对布局。这是不可取的。在intent.put extra()中放入一些分类器,在第二个活动中,可以基于intent.getextra()检索分类器。基于此,您可以切换图像视图的可见性。

对于按钮2的ex :: onclicklistener,启动intent.put extra(“whichButton”,“button2”)。 在创建第二个活动时检查 如果(intent.has extra(“whichbutton”)){String which button = intent.getextra}。 然后基于按钮值切换图像视图的可见性。

+0

我用作你说的,但是我点击button2 .. scrollview从第一个图像(熊)..我需要从第二个图像(狐狸)开始滚动视图....如何实现达? – user2558185

+0

计算scrollx位置并使用scrollview.scroll(scrollx)。您的scrollx可以是imageview1的宽度 – Ritaban