ScrollView vertical and horizontal

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

ScrollView vertical and horizontal

Postby kronos » Mon Jan 11, 2010 9:38 pm

Hi, i'm really tired looking for a solution for Scrollview vertical and horizontal.

I read that the any view/layout implements this feature in the framework but i need something like this.

I need to define a layout within other, the child layout must implement scrolling vertical/horizontal for moving.

Initially implemented a code that move the layout pixel by pixel, but i think that is not the right way.
I tried with ScrollView and HorizontalScrollView but any ones work like i want, because only implement vertical or horizontal scrolling.

Canvas is not my solution because i need attach listeners in someones child elements.

What can i do?
kronos
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 11, 2010 9:15 pm

Top

Postby kronos » Wed Jan 13, 2010 4:39 pm

I found a better solution.

XML: (design.xml)

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
  5.  
  6.   <FrameLayout android:layout_width="90px" android:layout_height="90px">
  7.  
  8.     <RelativeLayout android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="fill_parent">        
  9.  
  10.     </RelativeLayout>
  11.  
  12. </FrameLayout>
  13.  
  14. </FrameLayout>
  15.  
  16.  
  17.  
  18.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



Java Code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class Example extends Activity {
  3.  
  4.   private RelativeLayout container;
  5.  
  6.   private int currentX;
  7.  
  8.   private int currentY;
  9.  
  10.  
  11.  
  12.   protected void onCreate(Bundle savedInstanceState) {
  13.  
  14.     super.onCreate(savedInstanceState);
  15.  
  16.     setContentView(R.layout.design);
  17.  
  18.  
  19.  
  20.     container = (RelativeLayout)findViewById(R.id.container);
  21.  
  22.  
  23.  
  24.     int top = 0;
  25.  
  26.     int left = 0;
  27.  
  28.  
  29.  
  30.     ImageView image1 = ...
  31.  
  32.     RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  33.  
  34.     layoutParams.setMargins(left, top, 0, 0);              
  35.  
  36.     container.addView(image1, layoutParams);
  37.  
  38.  
  39.  
  40.     ImageView image2 = ...
  41.  
  42.     left+= 100;
  43.  
  44.     RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  45.  
  46.     layoutParams.setMargins(left, top, 0, 0);              
  47.  
  48.     container.addView(image2, layoutParams);
  49.  
  50.  
  51.  
  52.     ImageView image3 = ...
  53.  
  54.     left= 0;
  55.  
  56.     top+= 100;
  57.  
  58.     RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  59.  
  60.     layoutParams.setMargins(left, top, 0, 0);              
  61.  
  62.     container.addView(image3, layoutParams);
  63.  
  64.  
  65.  
  66.     ImageView image4 = ...
  67.  
  68.     left+= 100;    
  69.  
  70.     RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  71.  
  72.     layoutParams.setMargins(left, top, 0, 0);              
  73.  
  74.     container.addView(image4, layoutParams);
  75.  
  76.   }    
  77.  
  78.  
  79.  
  80.   @Override
  81.  
  82.   public boolean onTouchEvent(MotionEvent event) {
  83.  
  84.     switch (event.getAction()) {
  85.  
  86.         case MotionEvent.ACTION_DOWN: {
  87.  
  88.             currentX = (int) event.getRawX();
  89.  
  90.             currentY = (int) event.getRawY();
  91.  
  92.             break;
  93.  
  94.         }
  95.  
  96.  
  97.  
  98.         case MotionEvent.ACTION_MOVE: {
  99.  
  100.             int x2 = (int) event.getRawX();
  101.  
  102.             int y2 = (int) event.getRawY();
  103.  
  104.             container.scrollBy(currentX - x2 , currentY - y2);
  105.  
  106.             currentX = x2;
  107.  
  108.             currentY = y2;
  109.  
  110.             break;
  111.  
  112.         }  
  113.  
  114.         case MotionEvent.ACTION_UP: {
  115.  
  116.             break;
  117.  
  118.         }
  119.  
  120.     }
  121.  
  122.       return true;
  123.  
  124.   }
  125.  
  126. }
  127.  
  128.  
  129.  
  130.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


That's works!!!

If you want to load other layout or control, the structure is the same.
kronos
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 11, 2010 9:15 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests