2013-04-10 9 views
7

Ich habe eine dynamische Rasteransicht. Bedeutet, dass sein Inhalt variiert. Wenn also die Anzahl der Elemente zunimmt, wird vertikal geblättert. Ich möchte es als horizontale Schriftrolle machen.Wie Rasteransicht horizontal nicht vertikal in Android?

Bitte schlagen Sie eine Lösung vor.

+0

http://StackOverflow.com/Questions/5725745/Horizontal-Scrolling-Grid-view – Nermeen

+0

Ich habe dies versucht. Aber es wurde nach festen Inhalten im Raster gefragt. –

Antwort

0

Sie können versuchen, die GridView in eine HorizontalScrollView zu setzen. Sie können versuchen, die GridView innerhalb der HorizontalScrollView feste Höhe zu setzen.

Dann können Sie die Anzahl der Spalten des GridView basierend auf Ihrem Inhalt dynamisch berechnen und setNumColumns(int) Methode verwenden, um es festzulegen.

+0

Tolle Idee! Werde es definitiv versuchen. – marienke

+3

Es funktioniert nicht. Es versucht nur, alle Elemente in das Ansichtsfenster zu zerlegen, aber es erscheint kein horizontaler Bildlauf. – midnight

+0

Sie können versuchen, eine feste Breite auf die 'GridView' innerhalb der' HorizontalScrollView' zu setzen. – jaibatrik

1

können Sie die Bibliothek von Drittanbietern two-way grid view verwenden. es funktioniert gut für horizontales Scrollen oder Sie können 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

Ich benutze Ihre verschachtelte Struktur und fügte Adapter wie Dokument sagt, aber nur 3 Bild erscheint unter einander und drig ist nicht scrollbar, weißt du Warum? https://developer.android.com/guide/topics/ui/layout/gridview.html –

+0

funktioniert nicht ... update plz –

-1

Hier einige Lösung (aktualisiert):

Fügen Sie diese xML in Ihre Aktivität:

<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> 

Erstellen von benutzerdefinierten Rasterelement in Layout-Ordner:

<?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> 

als Sie es von Code config müssen (weil es bessere Möglichkeit dafür ist) und es auch mit Adapter fühlen:

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); 

} } 

Dies ist Beispiel für benutzerdefinierte Adapter:

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; 
} } 

ich diese Lösung nahm von here.