2013-04-10 40 views
7

Tôi có Chế độ xem lưới động. Có nghĩa là nội dung của nó thay đổi. Vì vậy, nếu số lượng mục tăng nó làm cho cuộn dọc. Tôi muốn biến nó thành cuộn ngang.Làm cách nào để cuộn chế độ xem lưới theo chiều ngang không theo chiều dọc trong Android?

Vui lòng đề xuất một số giải pháp cho việc này.

+0

http://stackoverflow.com/questions/5725745/horizontal-scrolling-grid-view – Nermeen

+0

Tôi đã thử cách này. Nhưng nó đã yêu cầu nội dung cố định trong lưới. –

Trả lời

0

Bạn có thể thử đặt GridView trong một số HorizontalScrollView. Bạn có thể thử đặt chiều cao cố định thành GridView bên trong HorizontalScrollView.

Sau đó, bạn có thể tính toán động số cột của GridView dựa trên nội dung của bạn và sử dụng phương pháp setNumColumns(int) để đặt.

+0

Ý tưởng tuyệt vời! Chắc chắn sẽ thử nó. – marienke

+3

Nó không hoạt động. Nó chỉ cố gắng để crunch tất cả các mục vào khung nhìn, nhưng không có cuộn ngang xuất hiện – midnight

+0

Bạn có thể cố gắng thiết lập chiều rộng cố định để 'GridView' bên trong' HorizontalScrollView'. – jaibatrik

1

bạn có thể sử dụng thư viện của bên thứ ba two-way grid view. nó hoạt động tuyệt vời cho cuộn ngang hoặc bạn có thể sử dụng RecyclerView

2
 <HorizontalScrollView 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@+id/seatLegendLayout"> 

     <FrameLayout 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:id="@+id/linearLayout_gridtableLayout" 
       android:layout_width="900dp" 
       android:layout_height="match_parent" 
       android:orientation="horizontal"> 

       <GridView 
        android:id="@+id/gridView1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_margin="4dp" 
        android:columnWidth="100dp" 
        android:gravity="center" 
        android:numColumns="9" 
        android:horizontalSpacing="1dp" 
        android:scrollbarAlwaysDrawHorizontalTrack="true" 
        android:scrollbarAlwaysDrawVerticalTrack="true" 
        android:scrollbars="horizontal" 
        android:stretchMode="none" 
        android:verticalSpacing="1dp"> 

       </GridView> 


      </LinearLayout> 
     </FrameLayout> 
    </HorizontalScrollView> 
+0

Tôi sử dụng cấu trúc lồng nhau của bạn và thêm adapter như doc nói, nhưng chỉ có 3 hình ảnh xuất hiện dưới nhau và drig không thể cuộn được, bạn có biết tại sao? https://developer.android.com/guide/topics/ui/layout/gridview.html –

+0

không hoạt động ... cập nhật plz –

-1

Dưới đây là một số giải pháp (Cập nhật):

Bao gồm này để hoạt động xml của bạn:

<HorizontalScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="none" 
    android:id="@+id/title_horizontalScrollView" 
    android:layout_margin="1dp" 
    android:fillViewport="false"> 

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

     <GridView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/horizontal_gridView" 
      android:layout_gravity="center"/> 
    </LinearLayout> 
</HorizontalScrollView> 

Tạo mục lưới tùy chỉnh trong thư mục layout:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:id="@+id/grid_item" 
    android:layout_width="120dp" 
    android:layout_height="30dp" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:gravity="center" 
    android:text="here is the text" 
    android:textAlignment="center" 
    android:textSize="15sp" 
    android:textStyle="bold" /> 
</RelativeLayout> 

hơn bạn cần phải cấu hình nó từ mã (vì nó là cách tốt hơn cho việc này) và cũng cảm thấy nó với bộ chuyển đổi:

public class MainActivity extends AppCompatActivity { 

public GridView gridView; 

String[] models = {"Some text here", "and here" ,"and also here","one here","hello","here too"," and here"}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    gridView = (GridView)findViewById(R.id.horizontal_gridView); 
    gridViewSetting(); 
} 


private void gridViewSetting() { 

    // this is size of your list with data 
    int size = models.length; 
    // Calculated single Item Layout Width for each grid element .. for me it was ~100dp 
    int width = 100 ; 

    // than just calculate sizes for layout params and use it 
    DisplayMetrics dm = new DisplayMetrics(); 
    this.getWindowManager().getDefaultDisplay().getMetrics(dm); 
    float density = dm.density; 

    int totalWidth = (int) (width * size * density); 
    int singleItemWidth = (int) (width * density); 

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
      totalWidth, LinearLayout.LayoutParams.MATCH_PARENT); 

    gridView.setLayoutParams(params); 
    gridView.setColumnWidth(singleItemWidth); 
    gridView.setHorizontalSpacing(2); 
    gridView.setStretchMode(GridView.STRETCH_SPACING); 
    gridView.setNumColumns(size); 
    GridAdapter adapter = new GridAdapter(MainActivity.this, models); 
    gridView.setAdapter(adapter); 

} } 

Đây là ví dụ về bộ điều hợp tùy chỉnh:

public class GridAdapter extends BaseAdapter { 

private String[] modelsName; 

private Context context; 

private LayoutInflater inflater; 

public GridAdapter(Context con, String[] names) { 
    this.modelsName = names; 
    this.context = con; 
} 

@Override 
public int getCount() { 
    return modelsName.length; 
} 

@Override 
public Object getItem(int i) { 
    return modelsName[i]; 
} 

@Override 
public long getItemId(int i) { 
    return i; 
} 

@Override 
public View getView(int i, View view, ViewGroup viewGroup) { 

    View gridView = view; 

    if(view == null) { 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     gridView = inflater.inflate(R.layout.custom_layout, null); 
    } 

    TextView text = (TextView) gridView.findViewById(R.id.grid_item); 

    text.setText(modelsName[i]); 

    return gridView; 
} } 

Tôi lấy giải pháp này từ here.

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