0

I have been developing my first Android app the past days, using this guide: Material Design Guide from Google.

I have decided to go for the Tile fragments as my choice, but the problem is that the content in these tiles are static / the content in tile 1 is the same as in tile 2, 3, 4 and so on. How do I change this so that each tile has unique content?

Any help would be greatly appreciated!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Winston Smith
  • 131
  • 1
  • 4
  • 11

1 Answers1

0

Normally you would have some data that you would want to present in this tiled list format. This would normally be passed into the ContentAdapter so that you can use it to fill each tile in. At the minute all the content is being set in XML, not in your adapter.

If you want to change the images for each you need to add an id attribute to the item_tile layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:padding="@dimen/tile_padding">

    <ImageView
        !--Add the id for the ImageView-->
        android:id="@+id/tile_image"
        android:layout_width="match_parent"
        android:layout_height="@dimen/tile_height"
        android:scaleType="centerCrop"
        android:src="@drawable/paris" />

    ...
</RelativeLayout>

Then you should change the ViewHolder class in the TileContentFragment so that we can get hold of the ImageView and TextView in the item_tile layout.

public static class ViewHolder extends RecyclerView.ViewHolder {

    ImageView tileImage;
    TextView tileTitle;

    public ViewHolder(View itemView) {
        super(itemView);
        this.tileImage = (ImageView) itemView.findViewById(R.id.tile_image);
        this.tileTitle = (TextView) itemView.findViewById(R.id.tile_title);
    }
}

Then just for example purposes lets set each tile's title to "Hello":

public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {

    Other class methods...

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tile, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tileTitle.setText("Hello");
        //If you had images for the different tile you could set them here too
        //holder.tileImage.setImageResource([THE ID OF YOUR IMAGE HERE])
    }
}

Hope this helps.

Mike Scamell
  • 600
  • 1
  • 6
  • 14
  • Thanks! I'll try it out and report back when I've done so – Winston Smith Apr 08 '16 at 10:00
  • QUESTION #1: Isn't "TextView tileText" in "public static class ViewHolder.." supposed to be "TextView tileTitle", as it is in "tileTitle = (TextView) tileItem.findViewById..." in "public ViewHolder(LayoutInflater..) below? /// I also got a couple of errors when running the app: ERROR #1: In "public ViewHolder() { error: constructor ViewHolder in class ViewHolder cannot be applied to given types; required: View found: no arguments reason: actual and formal argument lists differ in length" //// ERROR #2: Also in "public ViewHolder() { "error: call to super must be first statement in constructor" – Winston Smith Apr 09 '16 at 09:26
  • Yes you are correct on the variable name, i was doing it in the browser so there was a chance for errors! – Mike Scamell Apr 10 '16 at 09:39
  • I've updated the ViewHolder class code, try that out and get back to me. Apologies again I couldn't do it in Android Studio at the time. – Mike Scamell Apr 10 '16 at 09:56
  • The updated ViewHolder class code seems to work now, thanks! // A new error occured after updating the code, though. ERROR #1: In "public static class ContentAdapter extends RecyclerView...", under "@Override public ViewHolder onCreateViewHolder(...)", I get an error saying "error: constructor ViewHolder in class ViewHolder cannot be applied to given types; required: View,ImageView,TextView found: LayoutInflater, ViewGroup reason: actual and formal argument lists differ in length". This error occurs under "return new ViewHolder(LayoutInflater.from(...)" Can't figure out how to fix it. – Winston Smith Apr 10 '16 at 14:08
  • Ok try removing the "ImageView tileImage, TextView tileTitle" from the constructor code, i thought they may not be needed.I've updated my answer to be clearer. – Mike Scamell Apr 10 '16 at 14:10
  • Updated my code to the newest suggestion, but I still get an error in `public ViewHolder onCreateViewHolder() { return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);`, saying **error: constructor ViewHolder in class ViewHolder cannot be applied to given types; required: View found: LayoutInflater,ViewGroup reason: actual and formal argument lists differ in length** _// Thanks a lot for all the help so far! Sorry it's taking so long._ – Winston Smith Apr 11 '16 at 11:55
  • Ah yes totally forgot you'd have to update 'onCreateViewHolder()'. I've updated the answer. This should finally get you sorted! Sorry I've only just managed to clone the code from Github. Makes it much easier to help out! – Mike Scamell Apr 11 '16 at 12:07
  • _Worked perfectly! Thanks for all the help :)_ By the way, while I've got you here; under `public void onBindViewHolder) { holder.tileImage.setImageResource([ID OF IMG])`, exactly how do I set the ID of an image? **Also, since you set each tile's title to "Hello", how do I make each tile have a unique/different title?** _Hopefully this will be my last question, but thanks anyways!!_ – Winston Smith Apr 11 '16 at 13:00
  • You normally would either set them via a url (after making a web call to get a list of images) or you could put your images in the drawable folder and reference them from there. E.g. if you put a image of paris called "paris.png" in the drawable folder you would reference it like this `holder.tileImage.setImageResource(R.drawable.paris);` – Mike Scamell Apr 11 '16 at 13:04
  • To change the title of each tile you could use an ArrayList of Strings that holds the text for the tile list. Then in `onBindViewHolder()` you would you get the text for the tile based on the current position in the list. E.g. if you had an ArrayList of Strings called tileTitles, it would be `holder.tileTitle.setText(tileTitles.get(position))` – Mike Scamell Apr 11 '16 at 13:08
  • Thanks for all the help! Really appreciate it :) – Winston Smith Apr 11 '16 at 13:11
  • I'd appreciate if you'd mark my answer as right as well ;) – Mike Scamell Apr 11 '16 at 13:23
  • Totally forgot! Did it now :) – Winston Smith Apr 11 '16 at 13:32
  • Thank you much appreciated :) good luck with your adventure into the world of Android! – Mike Scamell Apr 11 '16 at 13:32
  • I see that under `public ContentAdapter() {` there's a value `tileTitles = new ArrayList();`. For the ArrayList for titles, should I place this here or do you recommend placing it somewhere else? – Winston Smith Apr 11 '16 at 13:45
  • Yep that's fine there, you may want to make a method so you can set the `ArrayList()`, otherwise if you're just playing around you could set the Strings statically. – Mike Scamell Apr 11 '16 at 13:47
  • Okay, thanks once again. Any suggestions to how I would do that? Still a beginner, so sorry if this is getting out of hand. – Winston Smith Apr 11 '16 at 13:51
  • You could use this http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line and then do all the adding to the list in your constructor, again only if you're playing with the code. Otherwise i would create a set method for the list. – Mike Scamell Apr 11 '16 at 13:54