2012-03-01 38 views
5

Tôi có CustomComponent lớp kéo dài ViewGroup.Thanh cuộn đến ViewGroup tùy chỉnh

Source code của CustomComponent:

 public class CustomComponent extends ViewGroup { 

      private static final String LOGTAG = "CustomComponent"; 

      private List<MenuItem> items; 

      private Context context; 

      private int screenWidth; 
      private int screenHeight; 
      private int cellWidth; 
      private int cellHeight; 
      private int duration; 
     private int space=7; 

     public CustomComponent(Context context) { 
      super(context); 
      this.context=context; 
     } 


      @Override 
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
      Log.v(LOGTAG, "on Measure called"); 
      screenWidth = MeasureSpec.getSize(widthMeasureSpec); 
       screenHeight = MeasureSpec.getSize(heightMeasureSpec); 
       cellHeight=screenHeight/AppConstants.HEIDHTCELLSCOUNT; 
       cellWidth=screenWidth/AppConstants.WIDTHCELLSCOUNT; 
       duration= cellHeight*2; 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec+duration); 

      } 


      @Override 
     protected void onLayout(boolean changed, int l, int t, int r, int b) { 
      Log.v(LOGTAG, "onLayout called"); 
      int childCount = this.getChildCount(); 
       for (int i = 0; i < childCount; i++) { 
         View child = getChildAt(i); 
          child.layout(items.get(i).getLeft(),items.get(i).getTop(),items.get(i).getRight(), items.get(i).getBottom()); 
     } 
     } 

public List<MenuItem> getItems() { 
     return items; 
    } 

    public void setItems(List<MenuItem> items) { 
     this.items = items; 
    } 
    } 

Xml bố trí:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:id="@+id/menu_layout" 
    android:scrollbars="vertical"> 
     <<package name>.CustomComponentandroid:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:id="@+id/menu_component" 
      android:scrollbars="vertical" 
      android:fadingEdge="vertical"/> 
</LinearLayout> 

tôi cần phải thêm cuộn dọc để ViewGroup này. Xin vui lòng giúp đỡ, tôi không có ý tưởng làm thế nào để giải quyết vấn đề này. enter image description here

+0

Mã từ [tại đây] (http://code.google.com/p/android-masonry/source/checkout) đã làm việc cho tôi bạn có thể cần một số chỉnh sửa cho phù hợp với bạn – almuneef

+0

Xin chào Natali, tôi biết câu hỏi này là quá già nhưng bạn đã nhận được giải pháp này. Nếu bạn có nó plz đăng nó. Tôi đang mắc kẹt trong tình huống tương tự – silwar

+0

Xin chào. Tôi không tìm thấy giải pháp nào cho nó, vì vậy tôi đã sử dụng Giao diện tương đối với Chế độ xem tùy chỉnh (hộp màu) làm chế độ xem con. – Natali

Trả lời

1

Bạn có thể cố gắng bao gồm bạn MenuComponent bên trong một ScrollView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:id="@+id/menu_layout" 
    android:scrollbars="vertical"> 
     <ScrollView android:id="@+id/ScrollView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <<package name>.MenuComponent android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:id="@+id/menu_component" 
      android:scrollbars="vertical" 
      android:fadingEdge="vertical"/> 
     </ScrollView> 
</LinearLayout> 
+0

Tôi đã thử câu hỏi này trước khi đặt câu hỏi này, nhưng với xml này tôi có màn hình đen. Viev grout của tôi được tạo nhưng tiếc là nó không được hiển thị. – Natali

+0

Bạn có thể thêm thông tin khác về sự cố của mình không? những gì bạn muốn đạt được với MenuComponent tùy chỉnh của bạn? Bạn đang gặp phải lỗi gì? –

+0

Tôi đã thêm lược đồ của thành phần của mình. Tôi cần thêm scroll ny 'ViewGroup' vào kích thước' duration'. – Natali

1

Hãy thử điều này. Bạn cần một LinearLayout bên trong một ScrollView. Đặt MenuComponent của bạn bên trong LinearLayout. Nó sẽ hoạt động.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:id="@+id/menu_layout" 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:id="@+id/linearLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" > 

      <<package name>.MenuComponent android:layout_height="wrap_content" 
       android:layout_width="fill_parent" 
       android:id="@+id/menu_component" 
       android:scrollbars="vertical" 
       android:fadingEdge="vertical"/> 
     </LinearLayout> 

    </ScrollView> 
</LinearLayout> 
+0

Thật không may tôi có cùng một 'màn hình balack'. :( – Natali

+0

Bạn có quản lý để giải quyết vấn đề không? – Shubhayu

+0

Không, tôi không biết làm thế nào để làm điều này. – Natali

0

bạn phải gọi awakenScrollBars(), mà sẽ kích hoạt các thanh cuộn để vẽ trên tùy chỉnh của bạn ViewGroup. (more details) và thanh cuộn dọc kích hoạt có đến mức khó tin setVerticalScrollBarEnabled()

Ngoài ra bạn cần phải ghi đè lên chức năng computeVerticalScrollExtent()computeVerticalScrollRange để đặt kích thước của ngón tay cái và phạm vi cuộn của thanh cuộn.

Các vấn đề liên quan