问题
I have a dynamic Grid View. Means its content varies. So, if the number of items increases it makes vertical scroll. I want to make it as horizontal scroll.
Please suggest some solution for this.
回答1:
You can try putting the GridView
in a HorizontalScrollView
. You may try to set fixed height to the GridView
inside the HorizontalScrollView
.
Then you can dynamically calculate the number of columns of the GridView
based on your content and use setNumColumns(int)
method to set it.
回答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>
回答3:
you can use third party library two-way grid view. it works great for horizontal scrolling or you can use RecyclerView
回答4:
You can use my logic for that have a look at this:
https://stackoverflow.com/a/33339420/5479863
回答5:
Here is some solution (Updated):
Include this to your activity 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>
Create custom grid item in layout folder:
<?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>
Than you need to config it from code (because it is better way for this) and also feel it with adapter:
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);
} }
This is example of custom 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;
} }
I took this solution from here.
来源:https://stackoverflow.com/questions/15919060/how-to-make-grid-view-scroll-horizontally-not-vertically-in-android