Scrollview vertical and horizontal in android

前端 未结 11 862
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 05:30

I\'m really tired looking for a solution for vertical and horizontal Scrollview.

I read that there are not any views/layouts in the framework which implement this fe

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 06:05

    I found a better solution.

    XML: (design.xml)

    
    
      
                
        
    
    
    

    Java Code:

    public class Example extends Activity {
      private RelativeLayout container;
      private int currentX;
      private int currentY;
    
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.design);
    
        container = (RelativeLayout)findViewById(R.id.container);
    
        int top = 0;
        int left = 0;
    
        ImageView image1 = ...
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(left, top, 0, 0);               
        container.addView(image1, layoutParams);
    
        ImageView image2 = ...
        left+= 100;
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(left, top, 0, 0);               
        container.addView(image2, layoutParams);
    
        ImageView image3 = ...
        left= 0;
        top+= 100;
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(left, top, 0, 0);               
        container.addView(image3, layoutParams);
    
        ImageView image4 = ...
        left+= 100;     
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(left, top, 0, 0);               
        container.addView(image4, layoutParams);
      }     
    
      @Override 
      public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                currentX = (int) event.getRawX();
                currentY = (int) event.getRawY();
                break;
            }
    
            case MotionEvent.ACTION_MOVE: {
                int x2 = (int) event.getRawX();
                int y2 = (int) event.getRawY();
                container.scrollBy(currentX - x2 , currentY - y2);
                currentX = x2;
                currentY = y2;
                break;
            }   
            case MotionEvent.ACTION_UP: {
                break;
            }
        }
          return true; 
      }
    }
    

    That's works!!!

    If you want to load other layout or control, the structure is the same.

提交回复
热议问题