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!
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.