私は小さなAndroidアプリに取り組んでいます。この Android アプリに必要なものの一部は、水平方向と垂直方向の両方にスクロール可能なグリッドを用意することです。ただし、一番左の列は固定する必要があります (常に画面に表示され、水平スクロールの一部ではありません)。同様に、上部のヘッダー行を固定する必要があります (垂直スクロールの一部ではありません)。
上記があまり意味をなさない場合、この図はこれを明確に説明することを願っています:
キー:
- 白: まったくスクロールしない
- 青: 縦にスクロール
- 赤: 横スクロール
- 紫: 縦横両方にスクロール
これらの次元の 1 つを実行するのは簡単で、私はそうしました。ただし、これらのディメンションの両方を機能させるのに問題があります。(つまり、下の部分をすべて青にすることも、右の部分をすべて赤にすることもできますが、上記のようにはできません) 私が持っているコードは以下のとおりで、基本的には次のようになります。
結果_グリッド.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/lightGrey">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="@id/summaryTableLayout"
android:layout_weight="0.1"
android:layout_marginBottom="50dip"
android:minHeight="100dip">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableLayout
android:id="@+id/frozenTable"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="2dip"
android:layout_marginLeft="1dip"
android:stretchColumns="1"
/>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/frozenTable"
android:layout_marginTop="2dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="1dip">
<TableLayout
android:id="@+id/contentTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"/>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_weight="0.1"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/backButton"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Return"/>
</LinearLayout>
</RelativeLayout>
Java コード:
private boolean showSummaries;
private TableLayout summaryTable;
private TableLayout frozenTable;
private TableLayout contentTable;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_grid);
Button backButton = (Button)findViewById(R.id.backButton);
frozenTable = (TableLayout)findViewById(R.id.frozenTable);
contentTable = (TableLayout)findViewById(R.id.contentTable);
ArrayList<String[]> content;
// [Removed Code] Here I get some data from getIntent().getExtras() that will populate the content ArrayList
PopulateMainTable(content);
}
private void PopulateMainTable(ArrayList<String[]> content) {
// [Removed Code] There is some code here to style the table (so it has lines for the rows)
for (int i = 0; i < content.size(); i++){
TableRow frozenRow = new TableRow(this);
// [Removed Code] Styling of the row
TextView frozenCell = new TextView(this);
frozenCell.setText(content.get(i)[0]);
// [Removed Code] Styling of the cell
frozenRow.addView(frozenCell);
frozenTable.addView(frozenRow);
// The rest of them
TableRow row = new TableRow(this);
// [Renoved Code] Styling of the row
for (int j = 1; j < content.get(0).length; j++) {
TextView rowCell = new TextView(this);
rowCell.setText(content.get(i)[j]);
// [Removed Code] Styling of the cell
row.addView(rowCell);
}
contentTable.addView(row);
}
}
これは次のようになります。
少し横スクロールするとこんな感じ
垂直にスクロールすると、このように表示されます。ヘッダーが失われることに注意してください。これは問題です!
最後に 2 つの注意事項があります。
まず、これがどこかに既に存在しないとは信じられません。(私は Android を所有していないので、これを実行できるアプリを探すことができませんでした)。ただし、StackOverflow内とインターネット全体で少なくとも2日間検索して、やりたいことを提供してくれるGridViewまたはTableLayoutのソリューションを探しましたが、まだ解決策を見つけていません。それを見逃してしまったことは恥ずかしいことですが、誰かがこれを行う方法を説明しているリソースを知っていれば、私は感謝します!
次に、2 つの LinearLayouts を追加して、これに対する解決策を「強制」しようとしました。作成します。このコードを投稿することはできますが、これはすでにかなり長いので、私の言いたいことが明らかであることを願っています。これは部分的には機能しましたが、ここでの問題は、ヘッダーとコンテンツの列が一列に並んでいないことです。TableRows 内の TextViews で getWidth() と setMinimumWidth() を使用したかったのですが、ここで説明したように、onCreate 中はこのデータにアクセスできませんでした (onPostCreate 内でもアクセスできませんでした)。これを機能させる方法を見つけることができませんでした。この領域での解決策も素晴らしいでしょう!
最後までここまでたどり着いたのなら、あなたに敬意を表します!