Create a tableview in Android: How to define the column-width in RecyclerView with fixed header?


In android java I want to implement a table view using a RecyclerView

The table should have a fixed header (that is always visible) and there should be columns with different with-s.

My current prototyp looks like this:

enter image description here TableActivity Screenshot

As you can see the widht of the three columns is the same. My goal: make width of column "ID"(1) smaller and width of column "greeting"(2) bigger.

(Also the header is not exactly over the column(2) but this is a different issue)

My question: is there a way to define seperate column width-s? I found no api to define the column-width.

I tried

> textView.setWidth(50 * (columnNumber+1));

but the columnWith in the gui remains the same.


I am using this xml layout activity_table.xml

	<HorizontalScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		>
		<LinearLayout
			android:orientation="vertical"
			android:layout_width="wrap_content"
			android:layout_height="match_parent">

			<!-- Header row -->
			<LinearLayout
				android:id="@+id/headerRow"
				android:orientation="horizontal"
				android:layout_width="match_parent"
				android:layout_height="wrap_content"
				android:background="#DDDDDD"/>

			<!-- Table rows -->
			<androidx.recyclerview.widget.RecyclerView
				android:id="@+id/tableRecycler"
				android:layout_width="match_parent"
				android:layout_height="match_parent"/>

		</LinearLayout>
	</HorizontalScrollView>

with this item_table_row.xml

	<?xml version="1.0" encoding="utf-8"?>
	<!-- res/layout/item_table_row.xml -->
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:id="@+id/rowContainer"
		android:orientation="horizontal"
		android:layout_width="match_parent"
		android:layout_height="wrap_content">
	</LinearLayout>

With this TableActivity looks like this:

	public class TableActivity extends AppCompatActivity {
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_table);

			recyclerView = findViewById(R.id.tableRecycler);
			headerRow = findViewById(R.id.headerRow);

			TableModelApi model = null;
			try {
				model = getTableModel();
			} catch (Exception e) {
				Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG);
				model = createSampleModel();
			}

			setupHeader(model);
			setupRecycler(model);
		}

		private void setupHeader(TableModelApi model) {
			headerRow.removeAllViews();

			String[] columns = model.getColumnNames();

			for (int i = 0; i < columns.length; i++) {
				String text = columns[i];
				TextView tv = TableHelper.createTextView(this, text, model.getColumnWidth(i));
				// tv.setTypeface(Typeface.DEFAULT_BOLD);
				headerRow.addView(tv);
			}
		}

		private void setupRecycler(TableModelApi model) {
			recyclerView.setLayoutManager(new LinearLayoutManager(this));
			recyclerView.setAdapter(new TableAdapter(model));
		}

		public TableModelApi getTableModel() throws IOException {		
			...		
		}

	}

and this TableAdapter

	public class TableAdapter extends RecyclerView.Adapter<TableAdapter.RowViewHolder> {
		@Override
		public RowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
			View view = LayoutInflater.from(parent.getContext())
					.inflate(R.layout.item_table_row, parent, false);
			return new RowViewHolder(view);
		}

		@Override
		public void onBindViewHolder(RowViewHolder holder, int position) {
			holder.bind(model, position);
		}

		@Override
		public int getItemCount() {
			return model.getRowCount();
		}

		static class RowViewHolder extends RecyclerView.ViewHolder {

			LinearLayout rowContainer;

			RowViewHolder(View itemView) {
				super(itemView);
				rowContainer = itemView.findViewById(R.id.rowContainer);
			}

			void bind(TableModelApi model, int rowIndex) {
				rowContainer.removeAllViews();

				Object[] row = model.getRow(rowIndex);

				for (int i = 0; i < row.length; i++) {
					Object cell = row[i];
					TextView tv = TableHelper.createTextView(itemView.getContext(), cell, model.getColumnWidth(i));

					rowContainer.addView(tv);
				}
			}
		}
	}

The complete sourcecode is available here : https://github.com/k3b/CsvViewer/tree/TableActivityDemo/app

0
May 26 at 6:00 PM
User Avatark3b
#java#android#android-recyclerview#column-width

No answer found for this question yet.