Ecco qualche soluzione (Aggiornato):
Includere questo per la vostra attività xml:
<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>
generano l'articolo griglia personalizzata nella cartella di 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>
di quanto è necessario config esso dal codice (perché è un modo migliore per questo) e anche sentire con adattatore:
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);
} }
questo è esempio di adattatore personalizzato:
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;
} }
ho preso questa soluzione da here.
http://stackoverflow.com/questions/5725745/horizontal-scrolling-grid-view – Nermeen
Ho provato questo. Ma stava chiedendo il contenuto fisso nella griglia. –