我有一个动态网格视图。意味着其内容各不相同因此,如果项目数量增加,则会进行垂直滚动。我想把它作为水平滚动。
请为此提出一些解决方案。
答案 0 :(得分:3)
<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>
答案 1 :(得分:1)
您可以使用第三方库two-way grid view。它非常适合水平滚动,或者您可以使用RecyclerView
答案 2 :(得分:0)
您可以尝试将GridView
放入HorizontalScrollView
。您可以尝试将固定高度设置为GridView
内的HorizontalScrollView
。
然后,您可以根据您的内容动态计算GridView
的列数,并使用setNumColumns(int)
方法进行设置。
答案 3 :(得分:0)
你可以使用我的逻辑来看看:
答案 4 :(得分:-1)
以下是一些解决方案(已更新):
将此信息包含在您的活动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>
在布局文件夹中创建自定义网格项:
<?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>
你需要从代码中配置它(因为这是更好的方法) 并且还可以通过适配器感受它:
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);
} }
这是自定义适配器的示例:
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;
} }
我从here获得此解决方案。