Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add own layout inflater on getView #30

Closed
agapaymarlo opened this issue May 18, 2016 · 19 comments
Closed

add own layout inflater on getView #30

agapaymarlo opened this issue May 18, 2016 · 19 comments
Assignees

Comments

@agapaymarlo
Copy link

i cant implement my public View getView on my own inside my adapter to inflate my own layout for the rows is there any way that i can make it? thanks

@ISchwarz23 ISchwarz23 self-assigned this May 19, 2016
@ISchwarz23
Copy link
Owner

Hi @agapaymarlo,

to get shure that I understood you correctly: You want to create a TableView with a custom Cell layout. Is this what you are trying to achive?

Best regards,
Ingo

@agapaymarlo
Copy link
Author

hi @ISchwarz23

no, i just want to implement it on the whole row, (inflate). right now, we can change only the colors per row.. but is there any way that i can inflate my xml layout? i try to overwrite the public final View getView() on my own adapter, but i cant, because its final.

thanks in advance
marlo

@ISchwarz23
Copy link
Owner

I'm not sure if I got you correctly, but if you want to have an example on how to inflate a custom layout in the TableDataAdapter have a look to the CarTableDataAdapter in the example app. To get the inflater simply use the getLayoutInflater() method.

If you want to set a background color for a specfic cell, simply set the color as the background of the view in the specific cell. I adapted the CarTableDataAdapter to show a color in the price column that indicates how expensive a car is:

@Override
public View getCellView(final int rowIndex, final int columnIndex, final ViewGroup parentView) {
    final Car car = getRowData(rowIndex);
    View renderedView = null;

    switch (columnIndex) {
        case 0:
            ...
        case 3:
           renderedView = renderPrice(car);
           break;
    }
    return renderedView;
}

private View renderPrice(final Car car) {
    final String priceString = PRICE_FORMATTER.format(car.getPrice()) + " €";

    final TextView textView = new TextView(getContext());
    textView.setText(priceString);
    textView.setPadding(20, 40, 20, 40);
    textView.setTextSize(TEXT_SIZE);

    if (car.getPrice() < 50000) {
        textView.setBackgroundColor(0xFF2E7D32);
    } else if (car.getPrice() > 100000) {
        textView.setBackgroundColor(0xFFC62828);
    }

    return textView;
}

@captrespect
Copy link

I think you just want to replace:
final LinearLayout rowView = new LinearLayout(getContext());
with a custom view. I'd refactor it into it's own method and override it. It'd be helpful for me too, sometimes I need the the row to be different based on the data, I'm not sure if I can do the with the colorizer stuff. I may submit a pull request for this.

@ISchwarz23
Copy link
Owner

But if you replace the LinearLayout for certain columns it will look no longer like a table This LinearLayout is used to have the columns. If you do this on your own, you also need to manage the column weights on your own. I don't think that this is a good approach.
To be sure that I understood you correctly and to find a solution for your issue, it would be perfect if you can provide a mockup showing the expected result.

@captrespect
Copy link

captrespect commented Jun 7, 2016

I was thinking something like

 protected LinearLayout createRow(rowIndex) {
     return new  new LinearLayout(getContext());
  }

Subclasses could override it to create their own and set any other props or inflate it from xml, it would still have to return a LinearLayout.

Right now I can get around by just doing my extra stuff in getView() something like this:

 if (columnIndex == 0 && rowHasBorder(data)) { //only need to do this once per row
      parentView.setBackground(getResources().getDrawable(R.drawable.bottom_border ));
  }

@ISchwarz23
Copy link
Owner

So, you want something like a TableDataRowColorizer but instead of giving a color you want to have the possibility to give a drawable?

@ISchwarz23
Copy link
Owner

Since version 2.1.0 the TableDataRowBackgroundProvider is available which replaces the TableDataRowColorizer. You have now the posibility to specify drawables as row background instead of only colors.

No you can achive your aimed behaviour:

public class BottomBorderBackgroundProvider implements TableDataRowBackgroundProvider<MyData> {

    @Override
    public Drawable getRowBackground(final int rowIndex, final MyData data) {
        if( hasBottomBorder( data ) ) {
            return getResources().getDrawable(R.drawable.bottom_border);
        } else {
            return getResources().getDrawable(R.drawable.default_background);
        }
    }

    private boolean hasBottomBorder(final MyData data) {
        // your logic goes here
    }

}

and set this to your table:

    tableView.setDataRowBackgroundProvider( new BottomBorderBackgroundProvider() );

@ahmadalibaloch
Copy link

ahmadalibaloch commented Aug 20, 2016

In previous version of the library I can easily overload the getViewand hence apply any clickListener for the row if needed plus I was also implementing a longclickListener to expand my row view. So you need to make getViewnot final please.

Currently I am reverting back to old version of the library that is 1.0.1 which lets the getViewoverride.

@ISchwarz23
Copy link
Owner

Hi Ahmad,

I really do not like to remove the final from the getView method as this would allow miss-use of the TableView. There has to be a very good reason for removing it.

Best regards,
Ingo

@ahmadalibaloch
Copy link

ahmadalibaloch commented Aug 21, 2016

Reason is pretty valid, I want to have a click listener on the whole row. In a rare case I also want to expand the row to show more information on long click listener. If you can provide some other click listener api or interface and favorably long click listener too,with index and row item access, than I can update to version two.

Usecase. We don't always use table view to just show information but sometime we also want to act on that row, clicking columns can be difficult on little screen.

In my use case I am showing a list of jobs to be completed in a project. Job status, name and description is in table row, now on clicking the job (row) user can go to a job detail page to act on the job and by long clicking users can expand row, to view some buttons below it, to change the job status quickly without details. So depending on the item case I would override the (row) view to make it expandable, or clickable or not.

@ISchwarz23
Copy link
Owner

Hi Ahmad,

as you said, as the screen is pretty small, I don't know if adding buttons inside the table is the best solution. Never the less, the listeners for clicking on data items have a look at 'TableDataClickListener' or for long clicks see 'TableDataLongClickListener'. There are a lot more listeners available.

Best regards,
Ingo

@ahmadalibaloch
Copy link

Thanks, I did not notice that, I think added recently. One problem is solved yet the expandable view or custom view problem not... :-)

@ISchwarz23
Copy link
Owner

The click listener was always there, only the long click listener was introduced in 2.2.0.
Custom views are also no issue. Just implement a custom TableDataAdapter. For the "expanding" of rows there is no native support by this library, but I could give you an example how you could realize it. :)

@ahmadalibaloch
Copy link

I work for DgHeating, UK and they really like the table view :-), I have already updated the tableView version to latest, That will be good if you could guide me to how can I expand row view on long click.

Another thing I want to know is how can I give heightand paddingto rows? I need to give padding to columns, before version 2 I was setting this to row which I would override in getView.. Is that the right solution? Documentation may be not complete..

@ISchwarz23
Copy link
Owner

What kind of TableDataAdapter do u use? The SimpleTableDataAdapter or a custom one?

@ahmadalibaloch
Copy link

Yes a simple TableDataAdapter with extended SortableTableView. Uploaded for you TableDataAdapter, TableView.

@ISchwarz23
Copy link
Owner

So, the height of a row is defined by the cell with the biggest height. So by defining your cells you define the rows passively. It's the same for the padding. You can not define the padding for the full row, but for the cells inside your row.

I will create an ExpandableTableDataAdapter for you at the weekend.

@ISchwarz23
Copy link
Owner

Have a look at the ExpandableTableDataAdapter. Just make your adapter extend the ExpandableTableDataAdapter and implement the ExpandableTableDataAdapter#getCollapsedCellView() and ExpandableTableDataAdapter#getExpandedCellView() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants