I have a recycler inside fragment in view pager.
The problem that it put likes on user accounts in UI randomly but in DB everything is fine.
In logs, I see that random is not influence on BD. So the bug is only in UI part. After the refresh of the list, it can appear again and after that disappear.
I would like to share code but I already don't have an I idea where the problem could be. It might be in adapter/refresh list listener / XML or any other places. Please let me know what part of the code you need and I will provide it. Maybe it is a bug of recycler as itself and I can't fix it.
Adapter class code:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.UserViewHolder>{
private List<FsUser> fsUserList = new ArrayList<>();
private OnItemClickListener.OnItemClickCallback onItemClickCallback;
private OnItemClickListener.OnItemClickCallback onChatClickCallback;
private OnItemClickListener.OnItemClickCallback onLikeClickCallback;
private Context context;
public SearchAdapter(OnItemClickListener.OnItemClickCallback onItemClickCallback,
OnItemClickListener.OnItemClickCallback onChatClickCallback,
OnItemClickListener.OnItemClickCallback onLikeClickCallback) {
this.onItemClickCallback = onItemClickCallback;
this.onChatClickCallback = onChatClickCallback;
this.onLikeClickCallback = onLikeClickCallback;
}
public void addUsers(List<FsUser> userList) {
fsUserList.addAll(userList);
notifyItemRangeInserted(fsUserList.size() - userList.size(), fsUserList.size());
}
public void clearData(){
fsUserList.clear();
notifyDataSetChanged();
}
#NonNull
#Override
public UserViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, parent, false);
return new UserViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull UserViewHolder holder, int position) {
FsUser fsUser = fsUserList.get(position);
holder.bind(fsUser, position);
}
#Override
public int getItemCount() {
return fsUserList.size();
}
public String getLastItemId(){
return fsUserList.get(fsUserList.size() - 1).getUid();
}
class UserViewHolder extends RecyclerView.ViewHolder {
RelativeLayout container;
ImageView imageView, like, chat;
TextView name, country;
private LottieAnimationView animationView;
UserViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
container = itemView.findViewById(R.id.item_user_container);
imageView = itemView.findViewById(R.id.user_img);
like = itemView.findViewById(R.id.search_btn_like);
chat = itemView.findViewById(R.id.search_btn_chat);
name = itemView.findViewById(R.id.user_name);
country = itemView.findViewById(R.id.user_country);
animationView = itemView.findViewById(R.id.lottieAnimationView);
}
void bind(FsUser fsUser, int position){
ViewCompat.setTransitionName(imageView, fsUser.getName());
if (FirebaseUtils.isUserExist() && fsUser.getUid() != null) {
new FriendRepository().isLiked(fsUser.getUid(), flag -> {
if (flag) {
like.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite));
animationView.setVisibility(View.VISIBLE);
}
});
}
if(fsUser.getUid() != null) {
chat.setOnClickListener(new OnItemClickListener(position, onChatClickCallback));
like.setOnClickListener(new OnItemClickListener(position, onLikeClickCallback));
}
imageView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
if(fsUser.getImage().equals("default")){
Glide.with(context).load(context.getResources().getDrawable(R.drawable.default_avatar)).into(imageView);
} else {
Glide.with(context).load(fsUser.getImage()).thumbnail(0.5f).into(imageView);
}
name.setText(fsUser.getName());
country.setText(fsUser.getCountry());
ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f).setDuration(500);
animator.addUpdateListener(valueAnimator ->
animationView.setProgress((Float) valueAnimator.getAnimatedValue()));
if (animationView.getProgress() == 0f) {
animator.start();
} else {
animationView.setProgress(0f);
}
}
}
}
And xml file of RecyclerView item:
<?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"
android:id="#+id/item_user_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="#dimen/user_cv_width"
android:layout_height="#dimen/user_cv_height"
android:layout_margin="#dimen/dp4"
android:elevation="#dimen/dp4">
<RelativeLayout
android:id="#+id/item_user_main_relative_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/item_user_top_relative_container"
android:layout_width="#dimen/user_rl_width"
android:layout_height="#dimen/user_rl_height">
<ImageView
android:id="#+id/user_img"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="#drawable/default_avatar" />
<RelativeLayout
android:id="#+id/item_user_top_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/user_item_bg"
android:orientation="vertical">
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/dp4"
android:textColor="#android:color/white"
android:textSize="#dimen/medium_text_size" />
<TextView
android:id="#+id/user_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_marginStart="#dimen/dp4"
android:textSize="#dimen/medium_text_size" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/item_user_bottom_relative_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dp12">
<ImageView
android:id="#+id/search_btn_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/heart_outline"
android:contentDescription="#string/search_btn_like_desc"/>
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lottieAnimationView"
android:visibility="gone"
android:layout_width="#dimen/lottie_animation_view_size"
android:layout_height="#dimen/lottie_animation_view_size"
app:lottie_loop="true"
app:lottie_autoPlay="true"
app:lottie_fileName="like.json"/>
</RelativeLayout>
<ImageView
android:id="#+id/search_btn_chat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dp12"
android:layout_weight="1"
android:src="#drawable/message_outline" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Override this two methods inside your adapter
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
Related
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 tried below code to implement an expend and collapse content using recyclerview(listview) a link
final boolean isExpanded = position==mExpandedPosition;
holder.details.setVisibility(isExpanded?View.VISIBLE:View.GONE);
holder.itemView.setActivated(isExpanded);
if (isExpanded)
previousExpandedPosition = position;
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1:position;
notifyItemChanged(previousExpandedPosition);
notifyItemChanged(position);
}
});
In my case when i click the particular position in recyclerview its expanding but it goes above the recyclerview.I cant see the full expanded content.i can see only partial content.In this case i need to scroll the recyclerview to view the full content.But i am searching for a solution to view the content without scroll the recyclerview. If i click another position in recyclerview that should be placed over an recyclerview.
Hear is My Code
public class CommonFragment extends Fragment {
#BindView(R.id.news_lists)
RecyclerView news_lists;
#BindView(R.id.nested_scroll)
NestedScrollView nested_scroll;
ArrayList<String> Names;
ArrayList<String> responseProducts = null;
NewsListAdaspters mAdapter;
Context mContext;
String position_name;
public CommonFragment() {
}
public static CommonFragment getInstance(String position, ArrayList<String> response) {
CommonFragment fragmentDummy = new CommonFragment();
Bundle args = new Bundle();
args.putStringArrayList("Types", response);
args.putString("position", position);
fragmentDummy.setArguments(args);
return fragmentDummy;
}
#Override
public void setArguments(Bundle args) {
super.setArguments(args);
this.responseProducts = args.getStringArrayList("Types");
this.position_name = args.getString("position");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
View view;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_common, container, false);
ButterKnife.bind(this, view);
} catch (InflateException ignored) {
}
mContext = getActivity();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
news_lists.setLayoutManager(mLayoutManager);
news_lists.setNestedScrollingEnabled(false);
Names = new ArrayList<>();
Names.clear();
for (int i = 0; i < 15; i++) {
Names.add("News Details " + i);
}
Log.e("position_name==>", "" + position_name);
getList();
return view;
}
private void getList() {
if (responseProducts.size() > 0) {
mAdapter = new NewsListAdaspters(mContext, responseProducts, position_name);
news_lists.setAdapter(mAdapter);
}
}
public class NewsListAdaspters extends RecyclerView.Adapter<NewsListAdaspters.MyViewHolder> {
private ArrayList<String> data;
private Context context;
private String name;
int mExpandedPosition = -1;
int previousExpandedPosition = -1;
NewsListAdaspters(Context context, ArrayList<String> maps, String selectedFragmentName) {
this.context = context;
this.data = maps;
this.name = selectedFragmentName;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.common_view, parent, false);
final MyViewHolder holder = new MyViewHolder(itemView);
return holder;
}
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, #SuppressLint("RecyclerView") final int position) {
holder.news_description.setText(data.get(position));
holder.news_title.setText(name);
final boolean isExpanded = position == mExpandedPosition;
Log.e("isExpanded=====>", "" + isExpanded);
holder.expend_layout.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
holder.itemView.setActivated(isExpanded);
if (isExpanded)
previousExpandedPosition = position;
holder.news_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, Activity_NewsFullDetails.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
holder.expand_click_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1 : position;
notifyItemChanged(previousExpandedPosition);
notifyItemChanged(position);
}
});
holder.share_fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "WORKING!!!!", Toast.LENGTH_SHORT).show();
}
});
holder.share_whatsapp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "WORKING!!!!", Toast.LENGTH_SHORT).show();
}
});
holder.share_twet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "WORKING!!!!", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return data.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.news_title)
public TextView news_title;
#BindView(R.id.news_hour)
public TextView news_hour;
#BindView(R.id.news_description)
public TextView news_description;
#BindView(R.id.news_image)
public ImageView news_image;
#BindView(R.id.expand_click_layout)
RelativeLayout expand_click_layout;
#BindView(R.id.expend_layout)
public LinearLayout expend_layout;
#BindView(R.id.full_title)
public TextView full_title;
#BindView(R.id.txt_description)
public TextView txt_description;
#BindView(R.id.share_twet)
public ImageView share_twet;
#BindView(R.id.share_whatsapp)
public ImageView share_whatsapp;
#BindView(R.id.share_fb)
public ImageView share_fb;
MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#03000000">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:fitsSystemWindows="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="#+id/news_lists"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
ITEM XML
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:layout_margin="2dp"
android:animateLayoutChanges="true"
android:background="#drawable/comment_background"
android:stateListAnimator="#animator/comment_selection"
android:elevation="3dp"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/news_image"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_margin="2dp"
android:background="#drawable/icon_card"
android:scaleType="fitXY" />
<RelativeLayout
android:id="#+id/expand_click_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/news_image"
android:layout_alignTop="#+id/news_image"
android:layout_toEndOf="#+id/news_image"
android:layout_toRightOf="#+id/news_image">
<TextView
android:id="#+id/news_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_layout"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:maxEms="3"
android:maxLines="4"
android:padding="4dp"
android:text=""
android:textColor="#color/black_color"
android:textSize="18sp" />
<RelativeLayout
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_margin="4dp">
<TextView
android:id="#+id/news_hour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/center_text"
android:layout_toStartOf="#+id/center_text"
android:maxLines="2"
android:text="Hours"
android:textColor="#color/black_color"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/center_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/center_text"
android:layout_toRightOf="#+id/center_text"
android:gravity="end"
android:text="News Title"
android:textColor="#color/black_color"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/expend_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/news_image"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/full_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Title Text"
android:textColor="#color/black_color"
android:textSize="20sp" />
<TextView
android:id="#+id/txt_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/large_text1"
android:textColor="#color/black_color"
android:textSize="18sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:orientation="horizontal">
<ImageView
android:id="#+id/share_fb"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/share_facebook" />
<ImageView
android:id="#+id/share_whatsapp"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/share_whatsapp" />
<ImageView
android:id="#+id/share_twet"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/share_tweet" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Try to use NestedScrollView instead of ScrollView and set below to your activity :-
recyclerView.setNestedScrollingEnabled(false);
For more information you can refer below stackoverflow links:-
How to use RecyclerView inside NestedScrollView?
Recyclerview inside ScrollView not scrolling smoothly
I am using cardview with recycler view.But earlier I used the same code to do things without any problems.But now I am using fragments. Now I am getting the distance between the cardviews are greater.
my recycler view adapter
public static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView label;
TextView dateTime;
ImageView imageView;
ProgressBar progressBar;
public DataObjectHolder(final View itemView, final Context activity, final ArrayList<DataObject> myDataset) {
super(itemView);
label = (TextView) itemView.findViewById(R.id.textView);
//dateTime = (TextView) itemView.findViewById(R.id.thot);
imageView=(ImageView)itemView.findViewById(R.id.iproductimg);
progressBar=(ProgressBar)itemView.findViewById(R.id.progress);
Log.i(LOG_TAG, "Adding Listener");
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
myClickListenerhi.onItemClick(getLayoutPosition(),v);
}
}
public MyRecyclerViewAdapter(Activity context, ArrayList<DataObject> myDataset,MyClickListener myClickListener1) {
mDataset = myDataset;
activityContext=context;
myClickListenerhi=myClickListener1;
}
#Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cardview, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view,activityContext,mDataset);
return dataObjectHolder;
}
#Override
public void onBindViewHolder(final DataObjectHolder holder, int position) {
Context context=null;
holder.label.setText(mDataset.get(position).getmText1());
// holder.dateTime.setText(mDataset.get(position).getmText2());
Drawable placeholder = holder.imageView.getContext().getResources().getDrawable(R.drawable.placeholder);
holder.imageView.setImageDrawable(placeholder);
//new ImageDownloaderTask(holder.imageView).execute(mDataset.get(position).getUrl());
mDataset.get(position).setPosition(position);
Picasso.with(activityContext)
.load(mDataset.get(position).getUrl())
.placeholder(R.drawable.placeholder)
.into(holder.imageView, new Callback() {
#Override
public void onSuccess() {
holder.progressBar.setVisibility(View.INVISIBLE);
}
#Override
public void onError() {
}
});
}
public void addItem(DataObject dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
#Override
public int getItemCount() {
return mDataset.size();
}
public interface MyClickListener {
void onItemClick(int position, View v);
}
}
my cardview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
>
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="156dp"
android:layout_height="242dp"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp"
card_view:contentPadding="5dp"
card_view:cardBackgroundColor="#ffffff">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:id="#+id/iproductimg"
android:src="#drawable/placeholder"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:scaleType="fitXY" />
<ProgressBar
android:id="#+id/progress"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible"
android:indeterminate="false" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/firstheadtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
The error
I solved this by changing Cardview LinearLayout attribute from
android:layout_width="match_parent"
android:layout_height="match_parent"
to
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Edit:
Main problem is that the recyclerviews child layout or item layout rootview attribute is match_parent.
change it attribut to wrap_content
android:layout_width="wrap_content"
android:layout_height="wrap_content"
In this case : in the layout of cardview:
remove :layout_margin="5dp"
add :
margin_top = "3dp"
margin_left = "5dp"
margin_right = "5dp"
I have this layout in two apps, one inside a RecyclerView and the other in a root activity layout.
Here the layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:background="#CFCFCF"
android:minHeight="250dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_dark"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_bright"
android:layout_above="#+id/commerceTextView"
>
<ImageView
android:src="#drawable/imagen"
android:id="#+id/commerceImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/commerceTextView"
android:gravity="center"
android:textStyle="bold"
android:textSize="24sp"
android:textColor="#F1F1F1"
android:background="#color/colorPrimary"
android:layout_alignParentBottom="true"
android:paddingTop="10dp"
android:text="Best food ever"
android:paddingBottom="10dp"/>
</RelativeLayout>
</FrameLayout>
Here the adapter
public class CommercesAdapter extends RecyclerView.Adapter<CommercesAdapter.CommercesViewHolder> {
private final Context context;
private final ImageLoader loader;
private List<CommerceEntity> commercesList;
#Inject
public CommercesAdapter(Context context, ImageLoader loader) {
this.context = context;
this.loader = loader;
}
public void setData(List<CommerceEntity> commercesList) {
this.commercesList = commercesList;
}
#Override
public CommercesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context)
.inflate(R.layout.list_comerces_item, parent, false);
return new CommercesViewHolder(view);
}
#Override
public void onBindViewHolder(CommercesViewHolder holder, int position) {
CommerceEntity commerce = commercesList.get(position);
String imageUri = commerce.getImageUri();
String name = commerce.getName();
// holder.commerceTypeName.setText(name);
//loader.bind(holder.commerceImage, imageUri);
}
#Override
public int getItemCount() {
return commercesList.size();
}
public static class CommercesViewHolder extends RecyclerView.ViewHolder {
public ImageView commerceImage;
public TextView commerceTypeName;
public CommercesViewHolder(View itemView) {
super(itemView);
commerceImage = (ImageView) itemView.findViewById(R.id.commerceImageView);
commerceTypeName = (TextView) itemView.findViewById(R.id.commerceTextView);
}
}
Here the RecyclerView
And here in a root activity layout
Someone knows why this happen? if I add android:centerInParent"true" to the nested FrameLayout the image appears but i don't understand why.
In your adapter, uncomment the two lines that you have commented out;
`
// holder.commerceTypeName.setText(name);
//loader.bind(holder.commerceImage, imageUri);
`
I am trying to implement an Activity similar to Twitter's tweet page. When a tweet is a reply to another tweet, if you scroll up, you can see the original tweet being replied to, which is initially hidden above the screen. Here is the code I have:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="#layout/toolbar"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/in_reply_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
>
<ImageView
android:id="#+id/user_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:scaleType="fitXY"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<TextView
android:id="#+id/real_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:textSize="18sp"
android:layout_toRightOf="#id/user_image"
android:layout_toEndOf="#id/user_image"
/>
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="0dp"
android:layout_below="#id/real_name"
android:layout_toRightOf="#id/user_image"
android:layout_toEndOf="#id/user_image"
android:textSize="15sp"
/>
<TextView
android:id="#+id/tweet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#id/user_image"
android:textSize="18sp"
/>
</RelativeLayout>
</LinearLayout>
Unfortunately, in this code only the original tweet shows up and the RecyclerView remains hidden for some reason. Could someone help me out how to position them in such a way that the RecyclerView is hidden above the screen and is visible only if you scroll up?
Decided to expand on my comment above with code. You can have multiple ViewHolders in one RecyclerView.Adapter, each can have different layouts (R.layout.original_tweet and R.layout.other_tweets here). getItemViewType() sets which viewholder to show. I didn't test or run the code so there might be a few bugs, but it should give you an idea at least.
public class MyAdapter
extends RecyclerView.Adapter {
private static final int ORIGINAL_TWEET = 0;
private static final int OTHER_TWEETS = 1;
private List<String> mTweets;
public MyAdapter(List<String> tweets) {
mTweets = tweets;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (ORIGINAL_TWEET == viewType) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.original_tweet, parent, false);
return new OriginalTweetViewHolder(view);
} else {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.other_tweets, parent, false);
return new OtherTweetsViewHolder(view);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof OriginalTweetViewHolder) {
// populate with tweet data
} else if (holder instanceof OtherTweetsViewHolder) {
// populate with tweet data
}
}
#Override
public int getItemCount() {
return mTweets.size();
}
#Override
public int getItemViewType(int position) {
if (position == mTweets.size() - 1) {
// At bottom position
return ORIGINAL_TWEET;
} else {
return OTHER_TWEETS;
}
}
}
public static class OriginalTweetViewHolder extends RecyclerView.ViewHolder {
// ViewHolder fields
public OriginalTweetViewHolder(View view) {
super(view);
}
}
public static class OtherTweetsViewHolder extends RecyclerView.ViewHolder {
// ViewHolder fields
public OtherTweetsViewHolder(View view) {
super(view);
}
}