Android水平滚动视图[10] [10]

时间:2014-06-19 09:13:54

标签: android scroll horizontal-scrolling android-4.4-kitkat horizontalscrollview

我正在寻找[10] [10]数组的android水平scrollview意味着10 TextViews,它应该垂直显示,对于每个相应的垂直TextView,还会有9个TextView {1}} Horizontal Scrollview中的{1}} 我无法创建[10] [10]水平和垂直scorllview

的网格

1 个答案:

答案 0 :(得分:0)

如果必须使用GridView以[10] [10]格式显示100个元素,则只需向GridView添加100个元素的列表,并将网格视图限制为每行仅显示10个元素。  您可以通过将属性 android:numColumns =“10”添加到GridView来设置限制以显示每行10个元素。它会自动将元素设置为[10] [10]格式。

示例:

1 sample_activity.xml

<GridView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myGridView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:numColumns="10" />

2 item.xml

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

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_item"
    android:layout_gravity="center"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="default"
    android:textSize="20sp"
    android:focusable="false"/>
</TextView>

<LinearLayout>

3 MainActivity

public class MainActivity extends Activity {

private TextView text;
private GridView gridView;
private final String[] items = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
        "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sample_activity);

    text = (TextView) findViewById(R.id.feedback);

    gridView = (GridView) this.findViewById(R.id.myGridView);
    CustomGridAdapter gridAdapter = new CustomGridAdapter(MainActivity.this, items);
    gridView.setAdapter(gridAdapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            text.setText((String) (gridView.getItemAtPosition(position)));
            Log.i("ITEM_CLICKED", "" + (String) (gridView.getItemAtPosition(position)));
        }
    });

}
}

<强> CustomGridAdapter

public class CustomGridAdapter extends BaseAdapter {

private Context context;
private String[] items;
LayoutInflater inflater;

public CustomGridAdapter(Context context, String[] items) {
    this.context = context;
    this.items = items;
    inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item, null);
    }
    TextView textView = (TextView) convertView.findViewById(R.id.grid_item);
    textView.setText(items[position]);

    return convertView;
}

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

@Override
public Object getItem(int position) {
    return items[position];
}

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