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"/>
Related
I have a RecyclerView inside BottomSheetDialogFragment. The RecyclerView items touch working normally when it's scrolling slowly.
But when the RecyclerView is scrolled fast and after the list stops (without touching), than touching on any item doesn't work on fast touch. It needs double touching.
See in the below example gif, when touching on Andhra Pradesh it's working fine. After slow scrolling, touching on Haryana also works fine. Then doing a fast scroll and touching on Punjab doesn't work on the first touch. Touching again it works.
Following is the code:
OperatorListDialogFragment.java
package com.*;
import *;
public class OperatorListDialogFragment extends BottomSheetDialogFragment{
private static final String ARG_NAME = "item_name";
private static final String ARG_LOGO = "item_logo";
private Listener mListener;
private String header;
private Context mContext;
public static OperatorListDialogFragment newInstance(String[] name, int[] logo, String header) {
final OperatorListDialogFragment fragment = new OperatorListDialogFragment();
final Bundle args = new Bundle();
args.putStringArray(ARG_NAME, name);
args.putIntArray(ARG_LOGO, logo);
args.putString("header", header);
fragment.setArguments(args);
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_operator_list_dialog_list_dialog, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
TextView headerTV = view.findViewById(R.id.title);
headerTV.setText(getArguments().getString("header"));
final RecyclerView recyclerView = view.findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(new OperatorAdapter(getArguments().getStringArray(ARG_NAME), getArguments().getIntArray(ARG_LOGO)));
view.findViewById(R.id.dismiss).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
final Fragment parent = getParentFragment();
if (parent != null) {
mListener = (Listener) parent;
} else {
mListener = (Listener) context;
}
}
#Override
public void onDetach() {
mListener = null;
super.onDetach();
}
public interface Listener {
void onFilterSelected(String selected, String selectedQuery);
}
private class ViewHolder extends RecyclerView.ViewHolder {
final TextView text;
ImageView logo;
ViewHolder(LayoutInflater inflater, ViewGroup parent) {
// TODO: Customize the item layout
super(inflater.inflate(R.layout.fragment_operator_list_dialog_list_dialog_item, parent, false));
text = itemView.findViewById(R.id.tv_operator_name);
logo = itemView.findViewById(R.id.iv_recharge_provider_icon);
}
}
private class OperatorAdapter extends RecyclerView.Adapter<ViewHolder> {
private String[] mNames;
private int[] mLogos;
OperatorAdapter(String[] name, int[] logo) {
mNames = name;
mLogos = logo;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.text.setText(mNames[position]);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("clicked", "" + position);
}
});
}
#Override
public int getItemCount() {
return mNames.length;
}
}
}
dialog.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_height="match_parent"
android:layout_width="match_parent"
>
<ImageView
android:focusable="true"
android:clickable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="Close"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:id="#+id/dismiss"
android:padding="14dp"
android:src="#drawable/ic_close_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/title"
app:layout_constraintTop_toTopOf="#id/dismiss"
app:layout_constraintBottom_toBottomOf="#id/dismiss"
app:layout_constraintLeft_toRightOf="#id/dismiss"
android:padding="14dp"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
tools:text="Select operator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View
app:layout_constraintTop_toBottomOf="#id/dismiss"
android:background="#969696"
android:layout_width="match_parent"
android:layout_height="0.5dp"/>
<androidx.recyclerview.widget.RecyclerView
app:layout_constraintTop_toBottomOf="#id/dismiss"
app:layout_constraintBottom_toBottomOf="parent"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
app:layout_constrainedHeight="true"
android:paddingTop="#dimen/list_item_spacing_half"
android:paddingBottom="#dimen/list_item_spacing_half"
tools:context=".fragments.OperatorListDialogFragment"
tools:listitem="#layout/fragment_operator_list_dialog_list_dialog_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:gravity="center_vertical"
android:orientation="horizontal"
android:id="#+id/ll_operator_list_wrapper"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:visibility="gone"
android:layout_marginLeft="16dp"
android:id="#+id/iv_recharge_provider_icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginVertical="16dp"
android:layout_centerVertical="true"
android:src="#drawable/ic_bsnl_logo"
tools:visibility="visible"/>
<TextView
android:padding="16dp"
android:textColor="#212121"
android:textSize="14sp"
android:ellipsize="end"
android:id="#+id/tv_operator_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="BSNL"
android:layout_toRightOf="#+id/iv_recharge_provider_icon"
android:layout_centerInParent="true"/>
<View
android:layout_below="#id/iv_recharge_provider_icon"
android:id="#+id/divider0"
android:background="#eeeeee"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginLeft="16dp"
android:layout_toRightOf="#+id/iv_recharge_provider_icon"
/>
</RelativeLayout>
add android:nestedScrollingEnabled="false" in RecyclerView
I'm using Recyclerview in one of my activity, but I'm not able to show any data in recyclerView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="56dp"
android:background="#color/colorPrimary"
android:id="#+id/toolBar">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_back_light"
android:layout_gravity="center"
android:layout_marginStart="16dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sushi"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:textSize="18sp"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/chatMessages"
android:clipToPadding="false"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:layout_below="#+id/toolBar"
android:layout_above="#+id/messageInputView"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:id="#+id/messageInputView">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:background="#D9D9D9"
android:id="#+id/border"
android:layout_alignParentTop="true"/>
<EditText
android:id="#+id/messageInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#color/transparent"
android:layout_toLeftOf="#+id/messageSendButton"
android:hint="Type a message..."
android:textColorHint="#80282a2b"
android:inputType="textAutoCorrect|textAutoComplete|textMultiLine|textCapSentences"/>
<ImageView
android:id="#+id/messageSendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_send"
android:onClick="onClick"
android:padding="4dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:background="#drawable/send_message_background"/>
</RelativeLayout>
</RelativeLayout>
All callback methods of RecyclerView.Adapter<RecyclerView.ViewHolder> not calling on calling notifydatasetchanged().
Adapter code here :
public class ChatMessagesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static String TAG = "ChatMessagesAdapter";
private ArrayList<ChatMessages> mChatMessages;
private Context mContext;
public ChatMessagesAdapter(ArrayList<ChatMessages> chatMessages, Context context){
mContext = context.getApplicationContext();
mChatMessages = chatMessages;
Log.d("ChatActivity Adapter", String.valueOf(mChatMessages.size()));
setHasStableIds(true);
}
public synchronized void refreshData(ArrayList<ChatMessages> feeds){
Log.d("ChatActivity Adapter", String.valueOf(feeds.size()));
mChatMessages.clear();
mChatMessages.addAll(feeds);
Log.d("ChatActivity Adapter", String.valueOf(mChatMessages.size()));
notifyDataSetChanged();
}
class IncomingMessageHolder extends RecyclerView.ViewHolder {
#BindView(R.id.messageText) TextView mMessageText;
#BindView(R.id.messageTime) TextView mMessageTime;
IncomingMessageHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
}
}
class OutgoingMessageHolder extends RecyclerView.ViewHolder {
#BindView(R.id.messageText) TextView mMessageText;
#BindView(R.id.messageTime) TextView mMessageTime;
OutgoingMessageHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
}
}
#Override
public int getItemViewType(int position) {
Log.d("ChatActivity Adapter","getItemViewType "+mChatMessages.get(position).getMessageStatus());
if (mChatMessages.get(position).getMessageStatus() == 2){
return 1;
} else {
return 0;
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d("ChatActivity Adapter","onCreateViewHolder");
switch (viewType){
case 0 :
View outgoingMessageView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_outcoming_text_message,parent,false);
return new OutgoingMessageHolder(outgoingMessageView);
default :
View incomingMessageView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_incoming_text_message, parent, false);
return new IncomingMessageHolder(incomingMessageView);
}
}
#Override
public void onBindViewHolder(#NonNull final RecyclerView.ViewHolder holder, int position) {
Log.d("ChatActivity Adapter","onBindViewHolder");
switch (holder.getItemViewType()){
case 0:
OutgoingMessageHolder outgoingMessageHolder = (OutgoingMessageHolder) holder;
outgoingMessageHolder.mMessageText.setText(mChatMessages.get(position).getMessage());
outgoingMessageHolder.mMessageTime.setText(
UtilsFuntions.getDurationString(mChatMessages.get(position).getTimeStamp())
);
break;
case 1:
IncomingMessageHolder incomingMessageHolder = (IncomingMessageHolder) holder;
incomingMessageHolder.mMessageText.setText(mChatMessages.get(position).getMessage());
incomingMessageHolder.mMessageTime.setText(
UtilsFuntions.getDurationString(mChatMessages.get(position).getTimeStamp())
);
break;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemCount() {
Log.d("ChatActivity Adapter", String.valueOf(mChatMessages.size()));
return mChatMessages.size();
}
}
Code for initalizing recyclerView:
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mLayoutManager.setItemPrefetchEnabled(true);
mChatMessages.setItemViewCacheSize(0);
mChatMessages.setDrawingCacheEnabled(true);
mChatMessages.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
mChatMessages.setLayoutManager(mLayoutManager);
mChatMessages.setHasFixedSize(true);
mChatMessagesAdapter = new ChatMessagesAdapter(mChatItems,
getApplicationContext());
mChatMessages.addOnScrollListener(listerOnScroll);
mChatMessages.setAdapter(mChatMessagesAdapter);
ChatMessages chatMessage = new ChatMessages();
chatMessage.setMessage(mChatMessage.getText().toString().trim());
chatMessage.setMessageStatus(2);
chatMessage.setTimeStamp(System.currentTimeMillis());
mChatItems.add(chatMessage);
mChatMessagesAdapter.refreshData(mChatItems);
But when even I touch recyclerview or try to scroll recyclerview all callbacks called and recyclerview get populated. I don't know why this is happening. Please help me to solve this
I have recyclerview with cardview to load data and there are two columns, the code work fine but I need to hide the first column with switch widget. I have trying some code but none work for me. Can someone help me. I'm new in android. Thanks
Here is the screenshot
Below is my code
MyFragment.java
public class MyFragment extends Fragment {
public MyFragment() {
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup viewGroup,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_recyclerview, viewGroup, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mobile = getResources().getStringArray(R.array.mobile);
desktop = getResources().getStringArray(R.array.desktop);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
MyAdapter adapter = new MyAdapter(getContext());
recyclerView.setAdapter(adapter);
// this code below didn't work. i thought it's because different
// layout between fragment and adapter but i don't know to solve it
final TextView tvMobile = (TextView) view.findViewById(R.id.tv_mobile);
Switch mSwitch = (Switch) view.findViewById(R.id.switch_item);
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
tvMobile.setVisibility(View.GONE);
} else {
tvMobile.setVisibility(View.VISIBLE);
}
}
});
}
}
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private LayoutInflater inflater;
public static String[] mobile;
public static String[] desktop;
public MyAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
#NonNull
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_cardview, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder holder, int position) {
holder.tvMobile.setText(mobile[position]);
holder.tvDesktop.setText(desktop[position]);
}
#Override
public int getItemCount() {
return mobile.length;
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tvMobile;
private TextView tvDesktop;
MyViewHolder(View itemView) {
super(itemView);
tvMobile = (TextView) itemView.findViewById(R.id.tv_mobile);
tvDesktop = (TextView) itemView.findViewById(R.id.tv_desktop);
}
}
}
fragment_recyclerview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_light"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="#dfe7f2"
android:gravity="end"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingRight="16dp"
android:paddingTop="5dp">
<Switch
android:id="#+id/switch_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Hide Mobile Row"
android:textColor="#color/text_color"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_divider" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp" />
</LinearLayout>
item_cardview.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
card_view:cardBackgroundColor="#color/background_dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#drawable/bg_card_view" />
<TextView
android:id="#+id/tv_mobile"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="romaji"
android:textColor="#color/black_1"
android:textSize="16sp" />
<TextView
android:id="#+id/tv_desktop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="arti"
android:textColor="#color/black_1"
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
In your adapter change like this
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
public static boolean isMobileHided = false; //add this flag
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder holder, int position) {
holder.tvMobile.setText(mobile[position]);
holder.tvDesktop.setText(desktop[position]);
if (isMobileHided) {
holder.tvMobile.setVisblity(View.GONE);
}
else{
holder.tvMobile.setVisblity(View.VISIBLE);
}
}
}
In your switch listener(From activity/fragments)
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//update the switch button status to adapter flag variable
adapter.isMobileHided = isChecked;
//refresh the adapter
adapter.notifyDataSetChanged();
}
});
I want to use view holder pattern in my firebase list adapter. The firebase adapter looks a bit different than the ones with converter view because here in populateView method, the view parameter can't be null and always have a value, so I'm looking for the best way to implement the view holder pattern.
Here is my adapter class:
public class NewsAdapter extends FirebaseListAdapter<News> {
public NewsAdapter(Activity activity, Class<News> modelClass, int modelLayout, Query ref) {
super(activity, modelClass, modelLayout, ref);
}
#Override
protected void populateView(View view, final News news, int i) {
final Context context = view.getContext();
final String title = news.getTitle();
final String photoUrl = news.getPhotoUrl();
// Create views and assign values
ImageView ivPhoto = (ImageView) view.findViewById(R.id.iv_news_photo);
if (photoUrl != null)
// Load the image using Glide
Glide.with(context)
.load(photoUrl)
.into(ivPhoto);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_news_title);
tvTitle.setText(title)
}
}
Here is View Holder
public class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView title,desc,points,remaining;
public ImageView image;
public ItemClickListener clickListener;
public TaskViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.task_title);
desc = (TextView) itemView.findViewById(R.id.task_description);
points = (TextView) itemView.findViewById(R.id.task_point);
remaining = (TextView) itemView.findViewById(R.id.task_remaining_entries);
image = (ImageView) itemView.findViewById(R.id.task_image);
itemView.setOnClickListener(this);
}
public void setClickListener(ItemClickListener clickListener) {
this.clickListener = clickListener;
}
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "clicked on task item", Toast.LENGTH_SHORT).show();
clickListener.onClick(view,getAdapterPosition(),false);
}
}
and here is what i'm doing to load data in RecyclerView
public void loadData(){
database = FirebaseDatabase.getInstance();
tasks = database.getReference("Tasks");
adapter = new FirebaseRecyclerAdapter<TaskPOJO, TaskViewHolder>(TaskPOJO.class, R.layout.task_item, TaskViewHolder.class, tasks) {
#Override
protected void populateViewHolder(TaskViewHolder viewHolder, final TaskPOJO model, int position) {
Log.wtf("TEstScript1", "populateViewHolder: "+model.getTitle() );
viewHolder.title.setText(model.getTitle());
viewHolder.desc.setText(model.getDescripttion()+"\n");
viewHolder.remaining.setText(model.getRemaining()+"/"+model.getPoint());
viewHolder.points.setText("Points "+model.getRemaining());
Glide.with(viewHolder.title.getContext())
.load(model.getImage())
.into(viewHolder.image);
viewHolder.setClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(getActivity(), "" + model.getTitle(), Toast.LENGTH_SHORT).show();
Intent TaskPOJODetail = new Intent(getActivity(), Main.class);
TaskPOJODetail.putExtra("value","detail");
TaskPOJODetail.putExtra("taskId",adapter.getRef(position).getKey());
startActivity(TaskPOJODetail);
}
});
}
};
recyclerViewTasks.setAdapter(adapter);
}
here is my task_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:elevation="8dp"
android:clipChildren="true"
android:clipToPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="110dp"
android:layout_height="130dp"
android:id="#+id/task_image"
android:src="#drawable/test_tast"
android:scaleType="centerCrop"
/>
<View
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="#color/colorAccent"
android:layout_alignParentRight="true"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/task_image"
android:orientation="vertical"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title of Task"
android:textSize="18sp"
android:textColor="#color/colorAccent"
android:id="#+id/task_title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30/300"
android:textSize="18sp"
android:layout_marginRight="16dp"
android:textColor="#color/colorAccent"
android:layout_alignParentRight="true"
android:id="#+id/task_remaining_entries"
/>
</RelativeLayout>
<me.biubiubiu.justifytext.library.JustifyTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Loren Ipsum is Simply dummy text of the printing and type settin industry. Developers and designers frequently use this\n "
android:layout_marginRight="16dp"
android:maxLines="3"
android:id="#+id/task_description"
android:lineSpacingMultiplier="1.1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Point 2345345"
android:textColor="#android:color/white"
android:background="#color/colorAccent"
android:layout_gravity="right"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:layout_marginTop="8dp"
android:id="#+id/task_point"
/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
hope this will help you.
You can find below example as a reference,
public class RecipeHolder extends RecyclerView.ViewHolder{
private static final String TAG = RecipeHolder.class.getSimpleName();
public TextView recipeName;
public ImageView recipeImage;
public RecipeHolder(View itemView) {
super(itemView);
recipeName = (TextView)itemView.findViewById(R.id.recipe_name);
recipeImage = (ImageView)itemView.findViewById(R.id.recipe_image);
}
}
And create your custom adapter
public class RecipeAdapter extends FirebaseRecyclerAdapter<RecipeObject, RecipeHolder>{
private static final String TAG = RecipeAdapter.class.getSimpleName();
private Context context;
public RecipeAdapter(Class<RecipeObject> modelClass, int modelLayout, Class<RecipeHolder> viewHolderClass, DatabaseReference ref, Context context) {
super(modelClass, modelLayout, viewHolderClass, ref);
this.context = context;
}
#Override
protected void populateViewHolder(RecipeHolder viewHolder, RecipeObject model, int position) {
viewHolder.recipeName.setText(model.getRecipename());
Glide.with(context).load(model.getRecipeImageUrl()).into(viewHolder.recipeImage);
}
}
For more reference you can refer FirebaseRecyclerView Adapter
I am trying to populate a ListView and display it under Fragment. I see that the count is returning correctly (greater than 0) after calling the adapter. Initially I had an issue with getView not being called but it was in a way resolved by changing layout_height to wrap_content as suggested by one of the article. I see that getView is now called while swiping for one Fragment to another. But getView doesn't get called when the Fragment is initially loaded. and also I don't ListView is displayed on screen regardless of any action.
First Fragment:
public class OneFragment extends Fragment {
public OneFragment() {
// Required empty public constructor
}
//private ListView mListView;
GridView grid;
String[] web = {
"Doctors","Movies","Dentists"} ;
int[] imageId = {
R.mipmap.doctors,
R.mipmap.dentists,
R.mipmap.restaurants
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
CustomGrid adapter = new CustomGrid(view.getContext(), iconSize);
GridView gridview = (GridView) view.findViewById(R.id.grid);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(view.getContext(), "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
ListItemMainFragment nextFrag = new ListItemMainFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(((ViewGroup) getView().getParent()).getId(), nextFrag);
ft.setTransition(FragmentTransaction.TRANSIT_NONE);// it will anim while calling fragment.
ft.addToBackStack(null); // it will manage back stack of fragments.
ft.commit();
}
});
return view;
}
}
Next Fragment:
public class ListItemMainFragment extends Fragment implements Item.DataListener {
private ListView mListView;
private ItemAdapter adapter;
public ListItemMainFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_item_main, container, false);
mListView = (ListView) view.findViewById(R.id.item_list_view);
mListView.setAdapter(adapter);
//final String DBReference = "all/" + position + "/" + web[+position];
final String DBReference = "all/0/Doctors";
Item.getRecipesFromDB(ListItemMainFragment.this, DBReference);
return view;
}
#Override
public void newDataReceived(ArrayList<Item> itemList) {
adapter = new ItemAdapter(getActivity(), itemList);
mListView.setAdapter(adapter);
Log.d("records ", String.valueOf((adapter.getCount())));
}
}
Adapter:
public class ItemAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private ArrayList<Item> mDataSource;
public ItemAdapter(Context context, ArrayList<Item> items) {
Log.d("MyApp", "I am here1000A");
mContext = context;
mDataSource = items;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//1
#Override
public int getCount() {
return mDataSource.size();
}
//2
#Override
public Object getItem(int position) {
return mDataSource.get(position);
}
//3
#Override
public long getItemId(int position) {
return position;
}
//4
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get view for row item
View rowView = mInflater.inflate(R.layout.list_item_detail, parent, false);
Log.d("MyApp", "I am here1000");
TextView titlePrefixTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_title_prefix);
TextView titleTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_title);
TextView specialityTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_speciality);
TextView addressTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_address);
TextView distanceTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_distance);
TextView timingsTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_timings);
TextView contactTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_contact);
// Get thumbnail element
//ImageView thumbnailImageView =
// (ImageView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_thumbnail);
Item item = (Item) getItem(position);
titlePrefixTextView.setText(String.valueOf(position+1) + ".");
titleTextView.setText(item.title);
specialityTextView.setText(item.speciality);
addressTextView.setText(item.address);
distanceTextView.setText(item.distance);
timingsTextView.setText(item.timings);
contactTextView.setText(item.contact);
return rowView;
}
}
Item.java:
public class Item {
public String title;
public String speciality;
public String timings;
public String address;
public String contact;
public String distance;
public interface DataListener {
void newDataReceived(ArrayList<Item> itemList);
}
public Item () {
// empty default constructor, necessary for Firebase to be able to deserialize blog posts
}
public String getTitle() {
return title;
}
public String getSpeciality() { return speciality; }
public String getTimings() {
return timings;
}
public String getAddress() { return address; }
public String getContact() { return contact; }
public String getDistance() { return distance; }
public static void getRecipesFromDB(final DataListener dataListener, String DBReference){
final ArrayList<Item> itemList = new ArrayList<>();
final DatabaseReference mDatabase;
final Item item = new Item();
mDatabase = FirebaseDatabase.getInstance().getReference(DBReference);
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
System.out.println("There are " + snapshot.getChildrenCount() + " recipes");
Log.d("MyApp", "I am here100");
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Item post = postSnapshot.getValue(Item.class);
Item item = new Item();
item.title = post.getTitle();
item.speciality = post.getSpeciality();
item.timings = post.getTimings();
item.address = post.getAddress();
item.contact = post.getContact();
item.distance = post.getDistance();
itemList.add(item);
}
// Transaction complete, sending to listener
dataListener.newDataReceived(itemList);
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getMessage());
}
});
}
}
ListView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.ferainc.kudla.ListItemMainFragment">
<ListView
android:id="#+id/item_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
ListView Item detail:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#android:color/holo_green_light"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:id="#+id/list_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#android:color/holo_green_light">
<TextView
android:id="#+id/item_list_title_prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:textSize="18sp"
android:textColor="#android:color/holo_red_light"
tools:text="Title" />
<TextView
android:id="#+id/item_list_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:layout_toEndOf="#id/item_list_title_prefix"
android:textSize="18sp"
android:textColor="#android:color/holo_red_light"
tools:text="Title" />
<TextView
android:id="#+id/item_list_speciality"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_title"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:textColor="#000000" />
<View
android:id="#+id/center_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/item_list_speciality"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/item_list_contact_prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/center_divider"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:text="Ph:"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/center_divider"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#id/item_list_contact_prefix"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_address_prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_contact"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:text="Address:"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_contact"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#id/item_list_address_prefix"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_distance_prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_address"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:text="Distance:"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_address"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#id/item_list_distance_prefix"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_timings_prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_distance"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:text="Timings:"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_timings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_distance"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#id/item_list_timings_prefix"
android:textColor="#fff092b0" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
I am not sure what you wanted by Item.getRecipesFromDB(ListItemMainFragment.this, DBReference);
as there is no code regarding "Items" it your Fragment.
But it think it doesn't populate your ListView.
So after ListView initialisation you have to create an adapter (it is missing in your onCreateView). And then set it onto ListView.
You also may try to call newDataReceived() from onCreteView.
But I suggest you to create the Adapter in onCreteView and then in newDataReceived() you can just update data and call notifyDataSetChanged() instead of creating new one each time