I'm using FirebaseUI FirestorePagingAdapter for recycler view adapter and the recycler view uses GridLayout type with 2 columns . The recycler view item block cantains 1 image view and few others textView , when I scroll down the list and scroll back up top the image view size decreases , this bug continues every time I scroll down and back up .
It also happens when I update the adapter .
Below is the code for it . Btw I'm using Navigation Component UI.
private FirestorePagingAdapter<ProductModel, HomePagingHolder> pagingAdapter;
private FirestorePagingOptions<ProductModel> pagingOptions;
private final PagingConfig config = new PagingConfig(15, 10, false);
POJO class
public class ProductModel {
private boolean availability;
private DocumentReference category_reference;
private boolean customizable;
private String delivery_fee;
private String description;
private String max_order_quantity;
private String name;
private String price;
private List<String> thumbnails;
private String productID;
public ProductModel(){}
public ProductModel(boolean availability, DocumentReference category_reference, boolean customizable,
String delivery_fee, String description, String max_order_quantity, String name, String price, List<String> thumbnails) {
this.availability = availability;
this.category_reference = category_reference;
this.customizable = customizable;
this.delivery_fee = delivery_fee;
this.description = description;
this.max_order_quantity = max_order_quantity;
this.name = name;
this.price = price;
this.thumbnails = thumbnails;
}
public boolean isAvailability() {
return availability;
}
public void setAvailability(boolean availability) {
this.availability = availability;
}
public DocumentReference getCategory_reference() {
return category_reference;
}
public void setCategory_reference(DocumentReference category_reference) {
this.category_reference = category_reference;
}
public String getProductID() {
return productID;
}
public void setProductID(String productID) {
this.productID = productID;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public boolean isCustomizable() {
return customizable;
}
public void setCustomizable(boolean customizable) {
this.customizable = customizable;
}
public String getDelivery_fee() {
return delivery_fee;
}
public void setDelivery_fee(String delivery_fee) {
this.delivery_fee = delivery_fee;
}
public String getMax_order_quantity() {
return max_order_quantity;
}
public void setMax_order_quantity(String max_order_quantity) {
this.max_order_quantity = max_order_quantity;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public List<String> getThumbnails() {
return thumbnails;
}
public void setThumbnails( List<String> thumbnails) {
this.thumbnails = thumbnails;
}
}
ViewHolder class
static class HomePagingHolder extends RecyclerView.ViewHolder {
private final TextView pName;
private final TextView pPrice;
private final ImageView pImg;
private final TextView pThumbnailCount;
public HomePagingHolder(#NonNull View itemView) {
super(itemView);
pName = itemView.findViewById(R.id.productName);
pPrice = itemView.findViewById(R.id.productPrice);
pImg = itemView.findViewById(R.id.productImg);
pThumbnailCount = itemView.findViewById(R.id.thumbnail_count_txt);
}
}
Inside the Fragment
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
...
...
if (pagingOptions == null) {
getProductFromFireStore(false);
initFirestoreUIAdapter();
}
}
Getting the data
private void getProductFromFireStore(boolean check) {
pagingOptions = new FirestorePagingOptions.Builder<ProductModel>().setLifecycleOwner(this).setQuery(productCollection,
config, snapshot -> {
ProductModel model = snapshot.toObject(ProductModel.class);
if (model != null) {
model.setProductID(snapshot.getId());
}
return Objects.requireNonNull(model);
}).build();
if (check) {
pagingAdapter.updateOptions(pagingOptions);
}
}
productCollection inside the setQuery(...) points to a Collection reference inside Firestore.
Adapter
private void initFirestoreUIAdapter() {
pagingAdapter = new FirestorePagingAdapter<ProductModel, HomePagingHolder>(pagingOptions) {
#Override
protected void onBindViewHolder(#NonNull HomePagingHolder holder, int position, #NonNull ProductModel model) {
holder.pName.setText(model.getName());
holder.pPrice.setText(String.format("%s %s", getResources().getString(R.string.rupee_symbol), model.getPrice()));
holder.pThumbnailCount.setText(String.valueOf(model.getThumbnails().size()));
Glide.with(requireContext()).load(model.getThumbnails().get(0)).into(holder.pImg);
holder.itemView.setOnClickListener(view -> {
bundle.putString(Fire.FIELD_PRODUCT_ID, model.getProductID());
NavHostFragment.findNavController(HomeFragment.this).navigate(R.id.action_home_to_productFragment2, bundle);
});
}
#NonNull
#Override
public HomePagingHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(requireContext()).inflate(R.layout.block_home_product_item, parent, false);
return new HomePagingHolder(v);
}
};
}
Recycler view item block
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/product_item_ripple"
android:clickable="true"
android:focusable="true">
<com.google.android.material.card.MaterialCardView
android:id="#+id/imgCard"
android:layout_width="180dp"
android:layout_height="220dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:cardBackgroundColor="#color/colorSurface"
app:cardCornerRadius="8dp"
app:cardElevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:cardCornerRadius="8dp"
app:cardElevation="0dp">
<ImageView
android:id="#+id/productImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#color/colorSurface"
android:src="#drawable/log_in_lounge_bg"
tools:ignore="ContentDescription" />
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/thumbnail_count_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/white_txt_bg"
android:drawablePadding="4dp"
android:elevation="4dp"
android:fontFamily="#font/nunito"
android:text="3"
app:drawableEndCompat="#drawable/ic_multiple_image" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<TextView
android:id="#+id/productName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ellipsize="end"
android:fontFamily="#font/montserrat"
android:maxLines="2"
android:text="Arm chair blue color"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="#+id/imgCard"
app:layout_constraintStart_toStartOf="#+id/imgCard"
app:layout_constraintTop_toBottomOf="#+id/imgCard" />
<TextView
android:id="#+id/productPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="#font/nunito"
android:text="TextView"
android:textColor="#color/colorTextMini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/productName"
app:layout_constraintStart_toStartOf="#+id/productName"
app:layout_constraintTop_toBottomOf="#+id/productName" />
</androidx.constraintlayout.widget.ConstraintLayout>
Item block
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/product_item_ripple"
android:clickable="true"
android:focusable="true">
<com.google.android.material.card.MaterialCardView
android:id="#+id/imgCard"
android:layout_width="180dp"
android:layout_height="220dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:cardBackgroundColor="#color/colorSurface"
app:cardCornerRadius="8dp"
app:cardElevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/productImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_centerInParent="true"
android:background="#color/colorSurface"
android:src="#drawable/log_in_lounge_bg"
app:shapeAppearance="#style/IsthmusShapeableImageStyle"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/thumbnail_count_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/white_txt_bg"
android:drawablePadding="4dp"
android:elevation="4dp"
android:fontFamily="#font/nunito"
tools:text="3"
app:drawableEndCompat="#drawable/ic_multiple_image" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<TextView
android:id="#+id/productName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ellipsize="end"
android:fontFamily="#font/montserrat"
android:maxLines="2"
tools:text="Arm chair blue color"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="#+id/imgCard"
app:layout_constraintStart_toStartOf="#+id/imgCard"
app:layout_constraintTop_toBottomOf="#+id/imgCard" />
<TextView
android:id="#+id/productPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="#font/nunito"
tools:text="TextView"
android:textColor="#color/colorTextMini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/productName"
app:layout_constraintStart_toStartOf="#+id/productName"
app:layout_constraintTop_toBottomOf="#+id/productName" />
</androidx.constraintlayout.widget.ConstraintLayout>
Related
I follow the online tutorial to build the Parsing JSON for Recycler View application. However, it does not show anything in the application. I don't know what happen about it. Can anyone help me to figure out the problem? Thank you.
enter image description here
MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<net.smallacademy.songslist.Song> songs;
private static String JSON_URL = "http://starlord.hackerearth.com/studio";
Adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.songsList);
songs = new ArrayList<>();
extractSongs();
}
private void extractSongs() {
RequestQueue queue = Volley.newRequestQueue(this);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, JSON_URL, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject songObject = response.getJSONObject(i);
net.smallacademy.songslist.Song song = new net.smallacademy.songslist.Song();
song.setTitle(songObject.getString("song").toString());
song.setArtists(songObject.getString("artists".toString()));
song.setCoverImage(songObject.getString("cover_image"));
song.setSongURL(songObject.getString("url"));
songs.add(song);
} catch (JSONException e) {
e.printStackTrace();
}
}
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
adapter = new Adapter(getApplicationContext(),songs);
recyclerView.setAdapter(adapter);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("tag", "onErrorResponse: " + error.getMessage());
}
});
queue.add(jsonArrayRequest);
}
}
Song.java
public class Song {
private String title;
private String artists;
private String coverImage;
private String songURL;
public Song(){}
public Song(String title,String artists,String coverImage,String songURL){
this.title = title;
this.artists = artists;
this.coverImage = coverImage;
this.songURL = songURL;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtists() {
return artists;
}
public void setArtists(String artists) {
this.artists = artists;
}
public String getCoverImage() {
return coverImage;
}
public void setCoverImage(String coverImage) {
this.coverImage = coverImage;
}
public String getSongURL() {
return songURL;
}
public void setSongURL(String songURL) {
this.songURL = songURL;
}
}
Adapter.java
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
LayoutInflater inflater;
List<net.smallacademy.songslist.Song> songs;
public Adapter(Context ctx, List<net.smallacademy.songslist.Song> songs) {
this.inflater = LayoutInflater.from(ctx);
this.songs = songs;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_list_layout,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.songTitle.setText(songs.get(position).getTitle());
holder.songArtists.setText(songs.get(position).getArtists());
Picasso.get().load(songs.get(position).getCoverImage()).into(holder.songCoverImage);
}
#Override
public int getItemCount() {
return songs.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView songTitle, songArtists;
ImageView songCoverImage;
public ViewHolder(#NonNull View itemView) {
super(itemView);
songTitle = itemView.findViewById(R.id.songTitle);
songArtists = itemView.findViewById(R.id.songArtist);
songCoverImage = itemView.findViewById(R.id.coverImage);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/songsList"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
custom_list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/coverImage"
android:layout_width="75dp"
android:layout_height="75dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_background"
tools:ignore="VectorDrawableCompat" />
<TextView
android:id="#+id/songTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="Song Title"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/coverImage"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:layout_marginLeft="16dp" />
<TextView
android:id="#+id/songArtist"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Song Artist"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/coverImage"
app:layout_constraintTop_toBottomOf="#+id/songTitle"
android:layout_marginLeft="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Please transfer this below code into onCreate method
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
and then change this both tag of Recyclerview
android:layout_width="match_parent"
android:layout_height="wrap_content"
My RecyclerView's item layout has a ViewPager on top and some views below it. ViewPager works fine, swipes good. But the recycler item click is not working on clicking the ViewPager (I've set click on the entire item). But if I click the content below the ViewPager, Click works.
here's the RecyclerView's list item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:orientation="horizontal"
xmlns:attrs="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#android:color/white"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="6dp"
card_view:cardUseCompatPadding="true"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="0dp" android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintDimensionRatio="H,4:2"/>
<com.rd.PageIndicatorView
android:id="#+id/pageIndicatorView"
android:layout_width="wrap_content"
android:layout_height="20dp"
app:piv_animationType="scale"
app:piv_dynamicCount="true"
app:piv_interactiveAnimation="true"
app:piv_selectedColor="#color/whiteSmoke"
app:piv_unselectedColor="#color/white25Transparent"
app:piv_viewPager="#id/viewPager"
attrs:piv_padding="2dp"
attrs:piv_radius="4dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="#id/viewpager" android:layout_marginBottom="0dp"/>
<com.codeairs.wattamatte.customcontrols.textview.MPRegularTextView
android:id="#+id/name"
android:layout_width="0dp" android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/viewpager" android:layout_marginTop="6dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginStart="12dp" android:layout_marginLeft="12dp"
android:layout_marginEnd="12dp" android:layout_marginRight="12dp"
android:text="Cosy Duplex - 42m2" android:textColor="#color/stPatricksBlue"/>
<com.codeairs.wattamatte.customcontrols.textview.MPMediumTextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/RoomDimensTextView"
app:layout_constraintTop_toBottomOf="#id/name" android:layout_marginTop="6dp"
app:layout_constraintStart_toStartOf="#id/name"
android:text="3p"/>
<com.codeairs.wattamatte.customcontrols.textview.MPMediumTextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/RoomDimensTextView"
app:layout_constraintTop_toBottomOf="#id/name" android:layout_marginTop="6dp"
app:layout_constraintStart_toEndOf="#id/tv1"
android:layout_marginStart="12dp" android:layout_marginLeft="12dp"
android:text="2ch"/>
<com.codeairs.wattamatte.customcontrols.textview.MPMediumTextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/RoomDimensTextView"
app:layout_constraintTop_toBottomOf="#id/name" android:layout_marginTop="6dp"
app:layout_constraintStart_toEndOf="#id/tv2"
android:layout_marginStart="12dp" android:layout_marginLeft="12dp"
android:text="4m"/>
<com.codeairs.wattamatte.customcontrols.textview.MPRegularTextView
android:id="#+id/location"
android:layout_width="0dp" android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/tv1" android:layout_marginTop="6dp"
app:layout_constraintStart_toStartOf="#id/tv1" app:layout_constraintEnd_toEndOf="parent"
android:text="Villeurbanne" android:textColor="#color/darkSlateBlue"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="4dp"/>
<com.codeairs.wattamatte.customcontrols.textview.MPBoldTextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="12dp"
android:text="450€ C.C" android:textColor="#color/stPatricksBlue" android:textSize="17sp"/>
<View
android:id="#+id/toggleBG"
android:layout_width="36dp" android:layout_height="36dp"
android:background="#drawable/solid_white_circle"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
app:layout_constraintTop_toBottomOf="#id/viewpager"
app:layout_constraintBottom_toBottomOf="#id/viewpager"/>
<ToggleButton
android:id="#+id/favoritesToggleButton"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="#drawable/bg_fav_toggle"
android:textOn=""
android:textOff=""
app:layout_constraintTop_toTopOf="#id/toggleBG"
app:layout_constraintBottom_toBottomOf="#id/toggleBG"
app:layout_constraintStart_toStartOf="#id/toggleBG"
app:layout_constraintEnd_toEndOf="#id/toggleBG"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
RecylerView Adapter
public class RoomsListRecyclerViewAdapter extends RecyclerView.Adapter<RoomsListRecyclerViewAdapter.ViewHolder> {
private final List<Room> mValues;
private final List<String> favRoomsIds;
private final OnListFragmentInteractionListener mListener;
private final Context mContext;
public RoomsListRecyclerViewAdapter(List<Room> items, List<String> favRoomsIds,
OnListFragmentInteractionListener listener, Context mContext) {
mValues = items;
mListener = listener;
this.favRoomsIds = favRoomsIds;
this.mContext = mContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_rooms_list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.imgPager.setAdapter(new ImageAdapter(mContext, holder.mItem.property.pictures));
holder.pageIndicatorView.setCount(holder.mItem.property.pictures.size());
holder.pageIndicatorView.setSelection(0);
holder.imgPager.addOnPageChangeListener(getPageChangeListener(holder));
holder.name.setText(holder.mItem.property.name);
holder.location.setText(holder.mItem.property.address.city);
holder.price.setText(MessageFormat.format("{0} € C.C", holder.mItem.property.price));
holder.tv1.setText(MessageFormat.format("{0}p", holder.mItem.property.personCount));
holder.tv2.setText(MessageFormat.format("{0}ch", holder.mItem.property.pieces));
holder.tv3.setText(MessageFormat.format("{0}m²", holder.mItem.property.surface));
holder.favToggle.setChecked(favRoomsIds.contains(holder.mItem.property.id));
holder.favToggle.setOnCheckedChangeListener((buttonView, isChecked) ->
toggleFavorite(holder.mItem.property.id, isChecked));
holder.mView.setOnClickListener(v -> {
if (null != mListener) {
mListener.onListFragmentInteraction(holder.mItem, favRoomsIds.contains(holder.mItem.property.id));
}
});
}
#Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
final View mView;
Room mItem;
ViewPager imgPager;
TextView name, location, price, tv1, tv2, tv3;
ToggleButton favToggle;
PageIndicatorView pageIndicatorView;
ViewHolder(View view) {
super(view);
mView = view;
imgPager = view.findViewById(R.id.viewpager);
name = view.findViewById(R.id.name);
location = view.findViewById(R.id.location);
price = view.findViewById(R.id.price);
tv1 = view.findViewById(R.id.tv1);
tv2 = view.findViewById(R.id.tv2);
tv3 = view.findViewById(R.id.tv3);
favToggle = view.findViewById(R.id.favoritesToggleButton);
pageIndicatorView = view.findViewById(R.id.pageIndicatorView);
}
#Override
public String toString() {
return super.toString() + " '" + "'";
}
}
private void toggleFavorite(String propertyId, boolean fav) {
if (fav)
favRoomsIds.add(propertyId);
else
favRoomsIds.remove(propertyId);
JsonObjectRequest request = NetworkUtils.toggleRoomFavoritesRequest(
mContext, propertyId, fav, this::handleResponse, this::handleError);
VolleySingleton.getInstance(mContext).addToRequestQueue(request);
}
private void handleResponse(JSONObject response) {
Log.d("RoomFav.Response", response.toString());
}
private void handleError(VolleyError error) {
}
public void clear() {
mValues.clear();
notifyDataSetChanged();
}
public void addItems(List<Room> rooms) {
mValues.addAll(rooms);
notifyDataSetChanged();
}
private ViewPager.OnPageChangeListener getPageChangeListener(ViewHolder holder) {
return new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
holder.pageIndicatorView.setSelected(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
}
}
pager adapter
public class ImageAdapter extends PagerAdapter {
private Context mContext;
private List<String> mItems;
public ImageAdapter(Context context, List<String> items) {
mContext = context;
mItems = items;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup collection, int position) {
LayoutInflater inflater = LayoutInflater.from(mContext);
ImageView img = (ImageView) inflater.inflate(R.layout.layout_image, collection, false);
String url = mItems.get(position);
if (!TextUtils.isEmpty(url))
Picasso.get().load(ApiPaths.IMG_PATH_PREFIX + url).into(img);
collection.addView(img);
return img;
}
#Override
public void destroyItem(#NonNull ViewGroup collection, int position, #NonNull Object view) {
collection.removeView((View) view);
}
#Override
public int getCount() {
return mItems.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
}
pager item
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/img"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:clickable="false"/>
Hi I ma trying to build an app that has a list of events retrieved from a firebase database but when I run it it displays just the first event stored.
there is the code I used to generate the list:
private RecyclerView recyclerView;
private FirebaseRecyclerAdapter<Event,EventViewHolder> firebaseRecyclerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendario);
recyclerView = (RecyclerView) findViewById(R.id.eventiList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Query query = FirebaseDatabase.getInstance().getReference("Eventi");
FirebaseRecyclerOptions<Event> options =
new FirebaseRecyclerOptions.Builder<Event>().setQuery(query, Event.class).build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, EventViewHolder>(options) {
#NonNull
#Override
public EventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.event_layout, parent, false);
return new EventViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull EventViewHolder holder, final int position, #NonNull Event model) {
holder.setDay(model.getData().substring(0,2));
holder.setMonth(model.getMese());
holder.setName(model.getNome());
holder.setLuogo(model.getLuogo());
holder.setendStart(model.getOra_inizio()+ " - "+ model.getOra_fine());
holder.mview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class EventViewHolder extends RecyclerView.ViewHolder
{
View mview;
public EventViewHolder(View itemView)
{
super(itemView);
mview=itemView;
}
public void setDay(String day)
{
TextView mtext= (TextView)mview.findViewById(R.id.day);
mtext.setText(day);
}
public void setName(String name)
{
TextView maut=(TextView)mview.findViewById(R.id.Event_Name);
maut.setText(name);
}
public void setMonth(String month)
{
TextView maut=(TextView)mview.findViewById(R.id.month);
maut.setText(month);
}
public void setLuogo(String luogo)
{
TextView maut=(TextView)mview.findViewById(R.id.luogo);
maut.setText(luogo);
}
public void setendStart(String endStart)
{
TextView maut=(TextView)mview.findViewById(R.id.start_end);
maut.setText(endStart);
}
}
#Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
//mAdapter.stopListening();
firebaseRecyclerAdapter.stopListening();
}
this is the code of the model class
public class Event {
private String Nome;
private String data;
private String descrizione;
private String luogo;
private String mese;
private String ora_fine;
private String ora_inizio;
private String type;
public Event()
{
}
public Event(String nome, String data, String descrizione, String luogo, String mese, String ora_fine, String ora_inizio, String type) {
Nome = nome;
this.data = data;
this.descrizione = descrizione;
this.luogo = luogo;
this.mese = mese;
this.ora_fine = ora_fine;
this.ora_inizio = ora_inizio;
this.type = type;
}
public String getNome() {
return Nome;
}
public String getData() {
return data;
}
public String getDescrizione() {
return descrizione;
}
public String getLuogo() {
return luogo;
}
public String getMese() {
return mese;
}
public String getOra_fine() {
return ora_fine;
}
public String getOra_inizio() {
return ora_inizio;
}
public String getType() {
return type;
}
}
this is the Calendar activity layout
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Calendario"
android:id="#+id/Calendar_draw">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/eventiList"></android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
This is the code for the recycler view row
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="119dp"
android:background="#color/darkBlue">
<TextView
android:id="#+id/day"
android:layout_width="86dp"
android:layout_height="66dp"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/Event_Name"
android:layout_marginStart="18dp"
android:textColor="#color/white"
android:textSize="50dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="18dp" />
<TextView
android:id="#+id/month"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/day"
android:layout_marginBottom="11dp"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_alignLeft="#+id/day" />
<TextView
android:id="#+id/Event_Name"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="124dp"
android:layout_marginTop="13dp"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_alignParentLeft="true"
android:layout_marginLeft="124dp" />
<TextView
android:id="#+id/start_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="135dp"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/white"
android:textStyle="italic" />
<TextView
android:id="#+id/luogo"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignStart="#+id/start_end"
android:layout_below="#+id/day"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/white"
android:textStyle="italic" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
As you can see from the image that follows it displays just one event and I have 5 in the database
Display
The code worked fine for other lists that are in the app I don't understand why it is not working for this one hope I can get an answer here, thanks
Edit: this is the Database tree
Database Tree
Problem Solved, the layout_height parameter was set to match_parent in the event_layout LinearLayout and CardView. I set it to wrap_content on both and it worked.
I am posting this question because I haven't found any proper solution related to my problem.
I am trying to retrieve my sensor data from Firebase but I am unable to do it. Please point out where am I doing wrong
Try.java
public class Try extends AppCompatActivity {
Button button;
ListView list;
DatabaseReference databaseTry;
List<TryObjectClass> tryObjectList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_try);
databaseTry =
FirebaseDatabase.getInstance().getReference("sensor_data");
button = (Button) findViewById(R.id.button);
list = (ListView) findViewById(R.id.list);
//list.setVisibility(View.INVISIBLE);
tryObjectList = new ArrayList<>();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
protected void onStart() {
super.onStart();
databaseTry.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
tryObjectList.clear();
for (DataSnapshot TrySnapshot : dataSnapshot.getChildren()){
TryObjectClass tryObjectClass =
TrySnapshot.getValue(TryObjectClass.class);
tryObjectList.add(tryObjectClass);
}
TryAdapter adapter = new TryAdapter(Try.this, tryObjectList);
list.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
This is my object class
TryObjectClass.java
public class TryObjectClass {
private String date;
private String time;
private String humidity;
private String motion;
private String distance;
private String temperature;
public TryObjectClass(){
}
public TryObjectClass(String date, String time, String humidity, String
motion, String distance, String temperature) {
this.date = date;
this.time = time;
this.humidity = humidity;
this.motion = motion;
this.distance = distance;
this.temperature = temperature;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public String getMotion() {
return motion;
}
public void setMotion(String motion) {
this.motion = motion;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
}
This is my adapter class
TryAdapter.java
public class TryAdapter extends ArrayAdapter<TryObjectClass> {
private Activity context;
private List<TryObjectClass> objectList;
public TryAdapter(#NonNull Activity context, List<TryObjectClass>
objectList) {
super(context, R.layout.new_list,0);
this.context = context;
this.objectList = objectList;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull
ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.new_list,null,true);
TextView date = (TextView) listViewItem.findViewById(R.id.date);
TextView distance = (TextView)
listViewItem.findViewById(R.id.distance);
TextView humidity = (TextView)
listViewItem.findViewById(R.id.humidity);
TextView motion = (TextView) listViewItem.findViewById(R.id.motion);
TextView temperature = (TextView)
listViewItem.findViewById(R.id.temperature);
TextView time = (TextView) listViewItem.findViewById(R.id.time);
TryObjectClass tryObjectClass = objectList.get(position);
date.setText(tryObjectClass.getDate());
distance.setText(tryObjectClass.getDistance());
humidity.setText(tryObjectClass.getHumidity());
motion.setText(tryObjectClass.getMotion());
temperature.setText(tryObjectClass.getTemperature());
time.setText(tryObjectClass.getTime());
return super.getView(position, convertView, parent);
}
}
try_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Try"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Retrieved data"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:padding="10dp"
android:layout_marginLeft="90dp"
android:layout_marginTop="20dp"/>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click here to retrieve the data"/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
This is my list item
new_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date -"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#000"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Distance -"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#000"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Humidity -"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#000"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/humidity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Motion -"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#000"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/motion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Temperature -"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#000"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time -"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#000"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
This is my database
smartborder-cef61
sensor_data
100
date: 1875
distance: "50M"
humidity: 4573
motion: "yes"
temperature: "30c"
time: 7377200
200
date: 23111996
distance: "50M"
humidity: 45
motion: "yes"
temperature: "30C"
time: 73775
I found something, try to put your setAdapter() outside your listener.
Inside your listener you will put this code:
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot TrySnapshot : dataSnapshot.getChildren()){
TryObjectClass tryObjectClass = TrySnapshot.getValue(TryObjectClass.class);
tryObjectList.add(tryObjectClass);
}
adapter.notifyDataSetChanged();
}
And try to put your declared things in onCreate method, like your ValueEventListener, so you can add it in onStart and remove it in onDestroy when your activity have not to listen your changes anymore when your activity is destroyed. Here's part of my fragment ContatsFragment, it is working fine:
public class ContatsFragment extends Fragment {
private ListView listView;
private ArrayAdapter adapter;
private ArrayList<Contat> contats;
private DatabaseReference databaseReference;
private ValueEventListener valueEventListenerContatos;
private Context context;
public ContatsFragment() {
// Required empty public constructor
}
#Override
public void onStart() {
super.onStart();
context = getActivity();
databaseReference.addValueEventListener(valueEventListenerContatos);
}
#Override
public void onStop() {
super.onStop();
databaseReference.removeEventListener(valueEventListenerContatos);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
contats = new ArrayList<>();
View view = inflater.inflate(R.layout.fragment_contats, container, false);
listView = (ListView) view.findViewById(R.id.lv_contatos);
adapter = new ContatAdapter(getActivity(), contats);
listView.setAdapter(adapter);
Preferences preferences = new Preferences(getActivity());
String loggedUserId = preferences.getLoggedUserId();
databaseReference = FirebaseConfig.getFirebaseReference();
databaseReference = databaseReference.child("contats")
.child(loggedUserId);
valueEventListenerContatos = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
contats.clear();
for (DataSnapshot dados : dataSnapshot.getChildren()) {
Contat contat = dados.getValue(Contat.class);
contats.add(contat);
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
return view;
}
}
I have got the solution...I made mistake in the giving the list object name...
In my android application, I have below classes. I want to bind these classes in UI to display the UI content based on object data.
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="java.util.Map"/>
<import type="java.util.List"/>
<variable name="itemContainerInformation" type="com.myApp.Models.ItemsContainerInformation"/>
</data>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/cardview_light_background"
tools:context="com.myApp.Fragments.DetailedViewFragment"
android:id="#+id/layout_detailed_view">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{itemInformation.t1Caption}"
android:textAlignment="textStart"
android:gravity="start"
android:id="#+id/textView_day_t1_header"
android:layout_below="#+id/textView_date_header"
android:textStyle="normal|bold"
android:textSize="20sp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:textColor="#color/cardview_light_background"
android:background="#color/colorPrimary"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{itemContainerInformation.currentDateHeaderCaption}"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:textAlignment="center"
android:textColor="#color/colorAccent"
android:background="#color/colorPrimaryDark"
android:id="#+id/textView_date_header"
android:textStyle="normal|bold"
android:layout_marginTop="10dp"
android:textSize="24sp" />
<LinearLayout android:orientation="horizontal"
android:id="#+id/linear_layout_information"
android:layout_below="#+id/textView_day_t1_header"
android:background="#color/fall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout android:orientation="vertical"
android:id="#+id/linear_layout_day_information_list"
android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="0dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{itemContainerInformation.itemInformations[0].Caption}"
android:textAlignment="textStart"
android:gravity="start"
android:id="#+id/textView_day_information1"
android:textStyle="normal|bold"
android:textSize="16sp"
android:layout_marginTop="30dp"
android:textColor="#color/colorPrimary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{itemContainerInformation.itemInformations[1].Caption}"
android:textAlignment="textStart"
android:gravity="start"
android:id="#+id/textView_day_information2"
android:textStyle="normal|bold"
android:textSize="16sp"
android:textColor="#color/colorPrimary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{itemContainerInformation.itemInformations[2].Caption}"
android:textAlignment="textStart"
android:gravity="start"
android:id="#+id/textView_day_information3"
android:textStyle="normal|bold"
android:textSize="16sp"
android:textColor="#color/colorPrimary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{itemContainerInformation.itemInformations[3].Caption}"
android:textAlignment="textStart"
android:gravity="start"
android:id="#+id/textView_day_information4"
android:textStyle="normal|bold"
android:textSize="16sp"
android:textColor="#color/colorPrimary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{itemContainerInformation.itemInformations[4].Caption}"
android:textAlignment="textStart"
android:gravity="start"
android:id="#+id/textView_day_information5"
android:textStyle="normal|bold"
android:textSize="16sp"
android:textColor="#color/colorPrimary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" "
android:textAlignment="textStart"
android:gravity="start"
android:id="#+id/textView_day_information6"
android:textStyle="normal|bold"
android:layout_marginBottom="20dp"
android:textSize="16sp"
android:textColor="#color/colorPrimary" />
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:id="#+id/linear_layout_day"
android:layout_weight="1" android:layout_height="match_parent" android:layout_width="0dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/linear_layout_day_information_list"
android:text="#{itemContainerInformation.currentDate}"
android:textAlignment="viewEnd"
android:gravity="end"
android:id="#+id/textView_day"
android:textStyle="normal|bold"
android:textSize="100dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:textColor="#color/colorPrimary" />
</LinearLayout>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:id="#+id/linear_layout_information_bottom"
android:layout_below="#+id/linear_layout_information"
android:background="#color/fall"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{itemContainerInformation.itemInformations[5].Caption}"
android:textAlignment="viewEnd"
android:gravity="end"
android:id="#+id/textView_day_information_bottom1"
android:textStyle="normal|bold"
android:textSize="16sp"
android:layout_marginTop="0dp"
android:textColor="#color/colorPrimary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{itemContainerInformation.itemInformations[6].Caption}"
android:textAlignment="viewEnd"
android:gravity="end"
android:id="#+id/textView_day_information_bottom2"
android:layout_below="#+id/textView_day_information_bottom1"
android:textStyle="normal|bold"
android:textSize="16sp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:textColor="#color/colorPrimary" />
</LinearLayout>
ItemInformation.java
public class ItemInformation extends BaseObservable {
private String caption;
private int lineIndex;
private int lineFontSize;
private int lineFontColor;
private int lineIndentation;
private int imageIndex;
public ItemInformation()
{
}
#Bindable
public String getCaption() { return caption; }
public void setCaption(String caption) {
this.caption = caption;
notifyPropertyChanged(BR.caption);
}
#Bindable
public int getLineIndex() { return lineIndex; }
public void setLineIndex(int lineIndex) {
this.lineIndex = lineIndex;
notifyPropertyChanged(BR.lineIndex);
}
#Bindable
public int getLineFontSize() { return lineFontSize; }
public void setLineFontSize(int lineFontSize) {
this.lineFontSize = lineFontSize;
notifyPropertyChanged(BR.lineFontSize);
}
#Bindable
public int getLineFontColor() { return lineFontColor; }
public void setLineFontColor(int lineFontColor) {
this.lineFontColor = lineFontColor;
notifyPropertyChanged(BR.lineFontColor);
}
#Bindable
public int getLineIndentation() { return lineIndentation; }
public void setLineIndentation(int lineIndentation) {
this.lineIndentation = lineIndentation;
notifyPropertyChanged(BR.lineIndentation);
}
#Bindable
public int getImageIndex() { return imageIndex; }
public void setImageIndex(int imageIndex) {
this.imageIndex = imageIndex;
notifyPropertyChanged(BR.imageIndex);}
public static int GetColorInformation(String fontcolor)
{
return Color.parseColor(fontcolor);
//int desiredColour = getResources().getColor(getResources().getIdentifier("my_color", "color"
}
}
ItemContainerInformation.java
public class ItemContainerInformation extends BaseObservable {
private List<ItemInformation> itemInformations = new ArrayList<ItemInformation>();
private String currentDate;
private String currentDateHeaderCaption;
private String t1Caption;
private int rowIndex;
private int columnIndex;
private int currentMonth;
private int currentYear;
public ItemContainerInformation()
{
Calendar calendar = Calendar.getInstance();
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
}
public ItemContainerInformation(String currentDate) {
this.currentDate = currentDate;
}
#Bindable
public String getCurrentDate() {
return currentDate;
}
public void setCurrentDate(String currentDate) {
this.currentDate = currentDate;
notifyPropertyChanged(BR.currentDate);
}
#Bindable
public String getCurrentDateHeaderCaption() {
return currentDateHeaderCaption;
}
public void setCurrentDateHeaderCaption(String currentDateHeaderCaption) {
this.currentDateHeaderCaption = currentDateHeaderCaption;
notifyPropertyChanged(BR.currentDateHeaderCaption);
}
#Bindable
public String getT1Caption() {
return t1Caption;
}
public void setT1Caption(String t1Caption) {
this.t1Caption = t1Caption;
notifyPropertyChanged(BR.t1Caption);
}
#Bindable
public int getRowIndex() {
return rowIndex;
}
public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex;
notifyPropertyChanged(BR.rowIndex);
}
#Bindable
public int getColumnIndex() {
return columnIndex;
}
public void setColumnIndex(int columnIndex) {
this.columnIndex = columnIndex;
notifyPropertyChanged(BR.columnIndex);
}
#Bindable
public int getCurrentMonth() {
return currentMonth;
}
public void setCurrentMonth(int currentMonth) {
this.currentMonth = currentMonth;
notifyPropertyChanged(BR.currentMonth);
}
#Bindable
public int getCurrentYear() {
return currentYear;
}
public void setCurrentYear(int currentYear) {
this.currentYear = currentYear;
notifyPropertyChanged(BR.currentYear);
}
#Bindable
public List<ItemInformation> getItemInformations() {
return itemInformations;
}
public void setItemInformations(List<ItemInformation> itemInformations) {
this.itemInformations = itemInformations;
notifyPropertyChanged(BR.itemInformations);
}
}
DetailedViewFagment
public class DetailedViewFragment extends Fragment {
ItemContainerInformation currentInormationItem;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_detailed_view, container, false);
if(currentInormationItem == null)
{
currentInormationItem = new ItemContainerInformation("100");
List<ItemInformation> dailyItems = new ArrayList<ItemInformation>();
for(int i = 0; i < 10; i++)
{
ItemInformation item = new ItemInformation();
item.setCaption("Caption_");
dailyItems.add(item);
}
currentDayInormationItem.setItemInformations(dailyItems);
}
FragmentDetailedDayViewBinding binding = DataBindingUtil.inflate(
inflater, R.layout.fragment_detailed_view, container, false);
View view = binding.getRoot();
//here data must be an instance of the class MarsDataProvider
binding.setDateInformation(currentDayInormationItem);
return v;
}
// This method is used to update the object from 'ParentItem'
// So when a UI click happens in the ParentFragment this method is called from ParentFragment
// which updates the objects. Now this updated object () will have new values which i want to update in the UI inside detailed view fragment.
public void UpdateDetailedDayView(DateInformation dayInormationItem)
{
// Should update all textviews in the child view but not working???
currentDayInormationItem = dayInormationItem;
}
}
ParentViewFragment
public class ParentViewFragment extends Fragment {
// This is inside the parent fragment class which has multiple items populated using Adapter and ViewHolder
// On item click of the UI i want to update the 'DetailedViewFragment'.
MyRecyclerViewAdapter.OnItemClickListener onItemClickListener = new MyRecyclerViewAdapter.OnItemClickListener() {
#Override
public void onItemClick(ItemInformation itemInformation) {
Toast.makeText(getContext(), "Item Clicked --- " + itemInformation.getCurrentDate(), Toast.LENGTH_LONG).show();
// On click i am creating new fragment and updating it
DetailedViewFragment detailedView = new DetailedViewFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.month_detailed_view_container, detailedView).commit();
detailedView.UpdateDetailedDayView(itemInformation);
}
};
}
Basically i have different textviews in UI and respective UI properties are stored in object like indentation, foreground color, font size etc. How I can bind this information to a textview?
I am not able to view the text using this line, can someone help me to correct my mistake in the below line and display the text using the specific identation and forground color.
android:text="#{itemContainerInformation.itemInformations[0].Caption}"
How I can bind the object and update the UI based on object data?
Thanks.