Giter Site home page Giter Site logo

monsterbrain / recyclerviewtableviewandroid Goto Github PK

View Code? Open in Web Editor NEW
89.0 7.0 19.0 604 KB

A sample Android Studio Project showing using a Recyclerview as a Table View.

Java 19.32% Kotlin 80.68%
android demo-app tableview native android-studio kotlin recyclerview

recyclerviewtableviewandroid's Introduction

TableView using Recyclerview (Android)

A sample Android Studio Project showing using a Recyclerview as a Table View.

How the Height of Row works

The Height of row is determined by the cell, which has maximum multiline content. In this sample that cell is given wrap_content and other cells are given match_parent. I've got to find a way to dynamically check this later.

Upgraded to Kotlin (38% code reduction :))

(older java version can be found here

Here's the preview

TableView Demo Gif

Code Snippets

Here's the Recyclerview xml in the main layout.

<!-- Table View-->
<HorizontalScrollView
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerViewMovieList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:nestedScrollingEnabled="false"
    tools:listitem="@layout/table_list_item" />

</HorizontalScrollView>

Here's the Recyclerview Adapter onBinding (all other stuffs are the same)

Kotlin Version

val rowPos = holder.adapterPosition

if (rowPos == 0) {
    // Header Cells. Main Headings appear here
    holder.itemView.apply {
        setHeaderBg(txtMovieName)
        txtMovieName.text = "Name"
    }
} else {
    val modal = movieList[rowPos - 1]

    holder.itemView.apply {
        setContentBg(txtMovieName)
        txtMovieName.text = modal.movieName
    }
}
Click to see the example in Java.
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    RowViewHolder rowViewHolder = (RowViewHolder) holder;

    int rowPos = rowViewHolder.getAdapterPosition();

    if (rowPos == 0) {
        // Header Cells. Main Headings appear here
        rowViewHolder.txtRank.setBackgroundResource(R.drawable.table_header_cell_bg);
        rowViewHolder.txtMovieName.setBackgroundResource(R.drawable.table_header_cell_bg);
        rowViewHolder.txtYear.setBackgroundResource(R.drawable.table_header_cell_bg);
        rowViewHolder.txtCost.setBackgroundResource(R.drawable.table_header_cell_bg);

        rowViewHolder.txtRank.setText("Rank");
        rowViewHolder.txtMovieName.setText("Name");
        rowViewHolder.txtYear.setText("Year");
        rowViewHolder.txtCost.setText("Budget (in Millions)");
    } else {
        MovieModal modal = movieList.get(rowPos-1);

        // Content Cells. Content appear here
        rowViewHolder.txtRank.setBackgroundResource(R.drawable.table_content_cell_bg);
        rowViewHolder.txtMovieName.setBackgroundResource(R.drawable.table_content_cell_bg);
        rowViewHolder.txtYear.setBackgroundResource(R.drawable.table_content_cell_bg);
        rowViewHolder.txtCost.setBackgroundResource(R.drawable.table_content_cell_bg);

        rowViewHolder.txtRank.setText(modal.getRank()+"");
        rowViewHolder.txtMovieName.setText(modal.getMovieName());
        rowViewHolder.txtYear.setText(modal.getYear()+"");
        rowViewHolder.txtCost.setText(modal.getBudgetInMillions()+"");
    }
}

@Override
public int getItemCount() {
    return movieList.size()+1; // one more to add header row
}

Motivation

While searching for TableView plugins, I found out that many of the Tableviews are really hard to setup and customize. So had to make one using Recyclerview, for adjusting the row height based on the content text.

Some Useful Tableview Plugins

TableRecycleView Plugin by HYY-yu

recyclerviewtableviewandroid's People

Contributors

monsterbrain avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

recyclerviewtableviewandroid's Issues

ClickListener

Hi,
i am using your TableView in my App. Really like it!!!
I would like to let the user edit a row by clicking on it.

Is it possible to include a ClickListener on the rows to start the Update Activity?

Is there a way to merge two cells horizontally/vertically ?

So I am working on my project and I am using your method to show data from a json array in a table, I need to merge two cells in the header both horizontally and vertically, and cannot think of an idea of how to do so. is there any way out?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.