Tôi đang làm việc trên một ứng dụng Android nhỏ. Một phần của những gì tôi cần cho ứng dụng android này là có một mạng lưới có thể cuộn ngang và dọc. Tuy nhiên, cột ngoài cùng bên trái cần phải được cố định (luôn luôn trên màn hình và không phải là một phần của cuộn ngang). Tương tự, dòng tiêu đề trên cần phải được đông lạnh (không phải là một phần của cuộn dọc)Tạo bảng/lưới có cột cố định và tiêu đề cố định
bức tranh này sẽ hy vọng mô tả rõ điều này nếu ở trên không làm quá nhiều ý nghĩa:
chìa khóa:
- trắng: không di chuyển ở tất cả
- Blue: cuộn theo chiều dọc
- đỏ: cuộn theo chiều ngang
- Tím: cuộn cả theo chiều dọc và chiều ngang
Để làm một trong những khía cạnh là đủ dễ dàng, và tôi đã làm như vậy. Tuy nhiên, tôi gặp khó khăn trong việc đưa cả hai thứ nguyên này vào hoạt động. (ví dụ, tôi có thể lấy phần dưới cùng là tất cả màu xanh, hoặc tôi có thể nhận được phần bên phải là tất cả màu đỏ, nhưng không hoàn toàn như trên) Mã tôi có bên dưới và về cơ bản sẽ sản xuất như sau:
result_grid.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>
mã 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);
}
}
Đây là những gì nó trông giống như:
Vì vậy, đây là những gì nó trông giống như với một chút cuộn ngang
Đây là những gì nó trông giống như khi di chuyển theo chiều dọc, lưu ý rằng bạn bị mất các tiêu đề! Đây là một vấn đề!
Hai điều cuối cùng cần lưu ý!
Trước hết, tôi không thể tin rằng điều này không tồn tại ở một nơi nào đó. (Tôi không sở hữu một chiếc Android, vì vậy tôi đã không thể tìm kiếm các ứng dụng có thể làm điều này). Tuy nhiên, tôi đã tìm kiếm ít nhất hai ngày trong StackOverflow và trên Internet để tìm kiếm một giải pháp cho GridView hoặc TableLayout sẽ cung cấp cho tôi những gì tôi muốn làm và chưa tìm ra giải pháp. Như xấu hổ như tôi sẽ được cho đã bỏ lỡ nó, nếu ai đó biết về một nguồn tài nguyên ra có mô tả làm thế nào để làm điều này, tôi sẽ biết ơn! Thứ hai, tôi đã cố gắng "buộc" một giải pháp cho điều này, trong đó tôi đã thêm hai LinearLayouts, một trong đó nắm bắt phần "Tiêu đề" của lưới mà tôi muốn tạo và một phần khác cho phần "nội dung" ở dưới cùng của lưới tôi muốn tạo. Tôi có thể đăng mã này, nhưng điều này đã khá dài và tôi hy vọng rằng điều tôi ngụ ý là hiển nhiên.Điều này một phần làm việc nhưng vấn đề ở đây là các tiêu đề và cột nội dung không bao giờ xếp hàng. Tôi muốn sử dụng getWidth() và setMinimumWidth() trên TextViews trong TableRows, nhưng như được mô tả here dữ liệu này không thể truy cập được trong quá trình onCreate (và cũng không thể truy cập được trong onPostCreate). Tôi đã không thể tìm ra cách để có được điều này để làm việc, và một giải pháp trong lĩnh vực này cũng sẽ tuyệt vời!
Nếu bạn đã thực hiện điều này từ đầu đến cuối, danh tiếng dành cho bạn!
+1 để viết đẹp –
@Kevek: "Tôi không thể tin rằng điều này không tồn tại ở đâu đó" - nó có thể tồn tại, nhưng tôi không biết nó được đóng gói như một thành phần tái sử dụng được, là mã nguồn mở. – CommonsWare
@CommonsWare có thể rất tốt, nhưng vì tôi thường đánh dấu các ghi chú của các thuật toán/thuật toán UI mà tôi mong đợi sử dụng trong tương lai hoặc suy nghĩ về nền tảng/ngôn ngữ tôi hy vọng rằng có thể ai đó tương tự, và đã tìm thấy điều này trước đây. Có thể không! – Kevek