Animate recycler view items left-right to tell user of swipe options - android

I have implemented a gesture to hint user of the swipe options that we have in our iOS app. Its looking like-
I am new to Android app development. I have implemented everything like the screen except the animation. How do I add same animation in Android application for recycler view items?

Finally, I implemented it myself.
Here is item_layout.xml-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="#+id/leftView"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/main_layout"
android:layout_alignBottom="#id/main_layout"
android:layout_alignParentStart="true"
android:background="#color/darkGray" />
<View
android:id="#+id/rightView"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/main_layout"
android:layout_alignBottom="#+id/main_layout"
android:layout_alignParentEnd="true"
android:background="#color/darkRed" />
<RelativeLayout id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</RelativeLayout>
</RelativeLayout>
Here is the RecyclerView.Adapter implementation-
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
....
....
if (position == 0) {
ObjectAnimator animationLeft = ObjectAnimator.ofFloat(holder.swipeLayout, "translationX", 0f, 80f, 0f, -80f, 0f);
animationLeft.setDuration(1500);
animationLeft.start();
}
}
Hope this helps someone!

Related

Android: TextView Alignment in RecyclerView [duplicate]

I have designed a layout with map fragment and recyclerView.
Each recyclerView item is cardview (I have specified give the xml layout).
The problem is RecyclerView item doesn't fill screen width.
img here
I tried to change layout_width to fill_parent, match_parent ... but it can't help
Here is the layout for each item
<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 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:id="#+id/cardView">
<LinearLayout
android:id="#+id/locationItemView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="120px"
android:layout_height="120px"
android:id="#+id/imgIcon"
android:background="#drawable/image_bg"
android:layout_margin="5dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true" />
<LinearLayout
android:id="#+id/layoutInfo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Location Name"
android:textColor="#d5c645"
android:textStyle="bold"
android:textSize="20dp"
android:padding="3dp" />
<TextView
android:id="#+id/txtAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Location Address"
android:textSize="16dp"
android:padding="3dp" />
<TextView
android:id="#+id/txtDistance"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="Location Distance"
android:textSize="14dp"
android:textStyle="italic"
android:padding="2dp"
android:textAlignment="viewEnd" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
and the main_layout
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
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">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="0px" android:id="#+id/map" tools:context=".Main"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_weight=".6"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/locationList"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight=".4"
android:divider="#FEFFCC"
android:dividerHeight="1dp" />
</LinearLayout>
<ListView
android:id="#+id/navdrawer"
android:layout_width="250px"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="?attr/colorPrimaryDark"
android:choiceMode="singleChoice"
android:divider="#android:color/white"
android:dividerHeight="1dp"
android:drawSelectorOnTop="false"/>
</android.support.v4.widget.DrawerLayout>
Hope anyone can help me. I am stuck with it for 3 days.
Thank you.
====================================
Edit on 19/11/15
I don't thing the problem is my itemView layout because when I change it to GridLayout it still doesn't fill the width.
Here is my CustomAdapter for RecyclerView
public class LocationDetailsAdapter extends RecyclerView.Adapter<LocationDetailsViewHolder> {
Context _context;
ArrayList<LocationDetails> _data;
public LocationDetailsAdapter(Context _context, ArrayList<LocationDetails> _object) {
this._context = _context;
_data = _object;
}
#Override
public LocationDetailsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location,null );
LocationDetailsViewHolder _viewHolder = new LocationDetailsViewHolder(_v);
return _viewHolder;
}
#Override
public void onBindViewHolder(LocationDetailsViewHolder locationDetailsViewHolder, int i) {
LocationDetails _location = _data.get(i);
locationDetailsViewHolder._imgType.setImageResource(R.drawable.repair_img);
locationDetailsViewHolder._locationName.setText(_location.get_locationName());
locationDetailsViewHolder._locationAddress.setText(_location.get_locationAddress() + ", " + _location.get_district() + ", " + _location.get_province());
locationDetailsViewHolder._distance.setText(String.valueOf(_location.get_distance()) + " km");
locationDetailsViewHolder._locationName.setOnClickListener(clickListener);
locationDetailsViewHolder._imgType.setOnClickListener(clickListener);
locationDetailsViewHolder._locationAddress.setOnClickListener(clickListener);
locationDetailsViewHolder._locationName.setTag(locationDetailsViewHolder);
locationDetailsViewHolder._imgType.setTag(locationDetailsViewHolder);
locationDetailsViewHolder._locationAddress.setTag(locationDetailsViewHolder);
}
#Override
public int getItemCount() {
return (null != _data ? _data.size() : 0);
}
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
LocationDetailsViewHolder _holder = (LocationDetailsViewHolder)v.getTag();
int _pos = _holder.getPosition();
int _id = _data.get(_pos).get_id();
Intent _intent = new Intent(CommonFields._context, ItemView.class);
_intent.putExtra("LocationID",_id);
_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
CommonFields._context.startActivity(_intent);
}
};
}
and here I get RecylerView in main
_locationView = (RecyclerView)findViewById(R.id.locationList);
_locationView.setLayoutManager(new LinearLayoutManager(this));
_adapter = new LocationDetailsAdapter(Main.this, CommonFields._locationData);
_locationView.setAdapter(_adapter);
When inflating a View from a LayoutInflater, you need to pass a parent parameter in order for layout_* attributes to be used. That's because these attributes need to create the correct LayoutParams class. That means that you can't use inflate(R.layout.*, null), but must instead pass a ViewGroup for the second parameter. In most cases, you also want to use the three-parameter version of the method and pass false as the third parameter. If this is omitted or true then the View is immediately added to the parent, which causes problems in places like onCreateViewHolder() because the framework is designed to perform this operation later instead. For more details, see this answer.
In your case, you have the line
View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location,null );
You should change it to
View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location, viewGroup, false );
You should create View like this
#Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
// create a new view
View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.card_view, viewGroup, false);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
Maybe my answer has a different approach, but it works to occupy the entire width of the screen and not break the elements when passing through the recycling view.
Adapter adapterBalanceInquiry = new Adapter(getActivity(), list);
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(adapterBalanceInquiry);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
recyclerView.post(new Runnable() {
#Override
public void run() {
try {
int dx = (recyclerView.getWidth() - recyclerView.getChildAt(0).getWidth());
recyclerView.scrollBy(-dx, 0);
LinearSnapHelper snapHelper = new LinearSnapHelper();
recyclerView.setOnFlingListener(null);
snapHelper.attachToRecyclerView(recyclerView);
}catch (Exception e){
e.printStackTrace();
}
}
});

RecyclerView doesn't expand underneath the row

I'm trying to obtain an effect similar to this on my RecyclerView
I've tried to Google I/O approach that is described on similar questions here. My adapter code looks as follows.
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
ViewHolder itemViewHolder = (ViewHolder) holder;
final boolean isExpanded = position == mExpandedPosition;
itemViewHolder.mNotesTV.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
holder.itemView.setActivated(isExpanded);
holder.itemView.setOnClickListener(v -> {
mExpandedPosition = isExpanded ? -1 : position;
notifyItemChanged(position);
});
}
private class ViewHolder extends RecyclerView.ViewHolder {
TextView mNotesTV;
ViewHolder(View itemView) {
super(itemView);
mNotesTV = itemView.findViewById(R.id.notesTV);
}
}
The xml file for the TextView I'm trying to expand looks like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/notesTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Details" />
</RelativeLayout>
The xml file with the RecyclerView looks like this
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:clickable="true"
android:src="#drawable/ic_add_white_24dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/historyLV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" />
</android.support.design.widget.CoordinatorLayout>
And what I get, instead of the effect shown above (RecyclerView item expands and underneath are the desired items), I just get my TextView inside the actual row instead of below.
If you don't already have a layout for your viewholder, make one like the one below. Change the IDs and colors according to your preference:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/alwaysVisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:gravity="center"
android:padding="#dimen/activity_horizontal_margin"
android:text="02 Mar, 2018"
android:textColor="#android:color/holo_blue_bright" />
<TextView
android:id="#+id/conditionallyVisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_dark"
android:gravity="center"
android:padding="#dimen/activity_horizontal_margin"
android:text="Details"
android:textColor="#android:color/white"
android:visibility="gone" />
</LinearLayout>
Next, inflate it in your adapter like this (assuming the above layout is called item_layout:
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_layout, parent, false);
return new ViewHolder(itemView);
}
In your viewholder class, get both the TextViews in the above layout like this:
private class ViewHolder extends RecyclerView.ViewHolder {
TextView mDateTV;
TextView mDetailsTV;
ViewHolder(View itemView) {
super(itemView);
mDateTV = itemView.findViewById(R.id.alwaysVisible);
mDetailsTV = itemView.findViewById(R.id.conditionallyVisible);
}
}
In onBindViewHolder, do the following:
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
ViewHolder itemViewHolder = (ViewHolder) holder;
final boolean isExpanded = position == mExpandedPosition;
itemViewHolder.mDetailsTV.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
holder.itemView.setActivated(isExpanded);
holder.itemView.setOnClickListener(v -> {
mExpandedPosition = isExpanded ? -1 : position;
notifyItemChanged(position);
});
}
That's all. I hope you can get this to work.
Maybe your requirement is make expandable recyclerview.
You can check this library https://github.com/thoughtbot/expandable-recycler-view

Message view from both side in android

I'm working on a chatting application. All I did is i can get api response successfully and can show them serially like this
This is id of sender and reciver
This is the messages
For doing this I created an adapter which code is something like this.
public class Single_chat_adapter extends RecyclerView.Adapter<Single_chat_adapter.Single_chat_adapterViewHolder>{
private List<Datum2> data;
private int rowLayout;
private Context context;
public Single_chat_adapter(List<Datum2> data, int rowLayout, Context context) {
this.data = data;
this.rowLayout = rowLayout;
this.context = context;
}
#Override
public Single_chat_adapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card2, parent, false);
return new Single_chat_adapterViewHolder(view); }
#Override
public void onBindViewHolder(Single_chat_adapterViewHolder holder, int position) {
holder.single_msg.setText(data.get(position).getMsg());
}
#Override
public int getItemCount() {
return data.size();
}
public class Single_chat_adapterViewHolder extends RecyclerView.ViewHolder {
TextView single_msg;
public Single_chat_adapterViewHolder(View itemView) {
super(itemView);
single_msg =itemView.findViewById(R.id.userNameTV);
}
}
}
Here I use a single view which is card2.xml. But all I need to do is set senders message in the left side and receiver message in other side.
What To do?
To achieve what you have explained, Create two views in card2.xml(one at the left and the other in the right). I have created one for you.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:layout_marginBottom="6dp"
android:layout_marginTop="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="40dp"
android:id="#+id/avatar"
android:background="#drawable/circle_stroke"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:src="#drawable/useric"
android:layout_height="40dp" />
<RelativeLayout
android:layout_toEndOf="#+id/avatar"
android:id="#+id/msg_back"
android:layout_marginBottom="5dp"
android:layout_gravity="center_vertical"
android:background="#drawable/message_bubble_accent"
android:layout_width="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:textSize="17sp"
android:id="#+id/user_text"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Hello world how are you?"
android:layout_height="wrap_content" />
<TextView
android:textSize="12sp"
android:layout_below="#+id/user_text"
android:id="#+id/chat_time"
android:textColor="#color/dark"
android:text="3:33pm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<ImageView
android:layout_width="40dp"
android:id="#+id/avatar2"
android:layout_alignParentEnd="true"
android:layout_below="#+id/msg_back"
android:background="#drawable/circle_stroke"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:src="#drawable/useric"
android:layout_height="40dp" />
<RelativeLayout
android:layout_toStartOf="#+id/avatar2"
android:layout_below="#+id/msg_back"
android:id="#+id/msg_back2"
android:layout_gravity="center_vertical"
android:background="#drawable/message_bubble_white"
android:layout_width="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:textSize="17sp"
android:id="#+id/user_text2"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Hello world how are you?"
android:layout_height="wrap_content" />
<TextView
android:layout_below="#+id/user_text2"
android:id="#+id/chat_time2"
android:textColor="#color/dark"
android:text="3:33pm"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_alignParentEnd="true"
android:layout_below="#+id/user_text2"
android:text="#string/sent"
android:width="20dp"
android:textAppearance="?android:textAppearanceSmall"
android:layout_width="20dp"
android:layout_height="20dp"
android:textColor="#android:color/holo_green_light"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
Modify your onBindViewHolder and add the condition that will check if the message is coming from another user or not. Like this...
#Override
public void onBindViewHolder(Single_chat_adapterViewHolder holder, int position) {
Datum2 datum = data.get(position);
holder.single_msg.setText(datum.getMsg());
int msgId = datum.getMsgId();
if (msgId == datum.getUserMsgId) {
//Do everything pertaining to this user here
holder.rightBubble.setText(single_msg);
//holder.rightAvatar.setText(single_msg) //For setting image
} else {
holder.leftBubble.setText(single_msg);
}
}
Make sure you reference leftBubble and rightBubble from you ViewHolder, and set the current userMsgid from the activity that is using this adapter.
You have to create 2 viewtype and two xml for them additionally and load them in a single adapter. Follow the link may help you.
Android Chat Tutorial: Building a Messaging UI
1) Create 2 views in card2.xml
2) One for left and another for right
3) Put condition in your onBindViewHolder, if message is from senders make left view visible and hide right view and same thing for right view also.
You can put condition inside onCreateViewHolder, this will let you to swap the layout (xml file)
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int layout = -1;
switch (viewType) {
case Message.TYPE_MESSAGE:
layout = R.layout.item_message;
break;
case Message.TYPE_LOG:
layout = R.layout.item_log;
break;
case Message.TYPE_ACTION:
layout = R.layout.item_action;
break;
}
View v = LayoutInflater
.from(parent.getContext())
.inflate(layout, parent, false);
return new ViewHolder(v);
}
You can change viewtype from this method
#Override
public int getItemViewType(int position) {
// Just as an example, return 0 or 2 depending on position
// Note that unlike in ListView adapters, types don't have to be contiguous
return position % 2 * 2;
}

how to add Horizontal Recyclerview as a header for Vertical Recyclerview?

I need an output like this. That is HorizontalRecyclerView as a header for VerticalRecyclerView.
Currently I'm using NestedScrollView to attain this design. But I felt scrolling is not smooth.
Please suggest any idea. Thanks in advance.
Layout:
<android.support.v4.widget.NestedScrollView
android:id="#+id/nsv_my_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="ifContentScrolls"
android:visibility="gone"
android:clipChildren="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="15dp">
<LinearLayout
android:id="#+id/ll_continue_watching"
android:layout_width="match_parent"
android:layout_height="132dp"
android:visibility="gone"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="29dp"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<customview.font.TextView
android:id="#+id/txt_edit_remove_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/btn_profile_edit"
android:gravity="center"
android:paddingTop="2dp"
android:text="EDIT"
android:textSize="10sp" />
<customview.font.TextView
android:id="#+id/txt_continue_watchng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:text="Continue watching"
android:textSize="12sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#color/horizontal_view_color" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_horizontal_view"
android:layout_width="match_parent"
android:layout_height="102dp"
android:paddingBottom="15dp"
android:paddingTop="15dp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#color/horizontal_view_color" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_vertical_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Very simple, your "main" recycler view has 2 kind of cells, the first one, that is another recycler view, and the rest that are "normal" cells.
On the adapter of your "main" recycler view, you have to Override the method getItemViewType, something like this:
#Override
public int getItemViewType(int position) {
if(postition == 0) {
return 1; //Let's say that you define that 1 is the view type for the recycler view cell
}
else {
return 2; //And 2 is going to be the "normal" cells
}
}
Then, on your onCreateViewHolder method, depending on the view type, you must build and return the correct viewHolder, something like this:
#Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
if(viewType == 1) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.your_other_recyclerview_layout, parent, false);
//build the viewHolder
ViewHolder vh = new YourOtherRecyclerViewHolder(v);
return vh;
}
else {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.your_normal_cell_layout, parent, false);
//build the normal viewHolder
YourNormalViewHolder vh = new YourNormalViewHolder (v);
return vh;
}
}
Of course you must implement 2 classes that extends the ViewHolder class, one for YourOtherRecyclerViewHolder, and the other one for YourNormalViewHolder.
I hope it helps!

RecyclerView items don't fill width

I have designed a layout with map fragment and recyclerView.
Each recyclerView item is cardview (I have specified give the xml layout).
The problem is RecyclerView item doesn't fill screen width.
img here
I tried to change layout_width to fill_parent, match_parent ... but it can't help
Here is the layout for each item
<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 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:id="#+id/cardView">
<LinearLayout
android:id="#+id/locationItemView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="120px"
android:layout_height="120px"
android:id="#+id/imgIcon"
android:background="#drawable/image_bg"
android:layout_margin="5dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true" />
<LinearLayout
android:id="#+id/layoutInfo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Location Name"
android:textColor="#d5c645"
android:textStyle="bold"
android:textSize="20dp"
android:padding="3dp" />
<TextView
android:id="#+id/txtAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Location Address"
android:textSize="16dp"
android:padding="3dp" />
<TextView
android:id="#+id/txtDistance"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="Location Distance"
android:textSize="14dp"
android:textStyle="italic"
android:padding="2dp"
android:textAlignment="viewEnd" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
and the main_layout
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
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">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="0px" android:id="#+id/map" tools:context=".Main"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_weight=".6"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/locationList"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight=".4"
android:divider="#FEFFCC"
android:dividerHeight="1dp" />
</LinearLayout>
<ListView
android:id="#+id/navdrawer"
android:layout_width="250px"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="?attr/colorPrimaryDark"
android:choiceMode="singleChoice"
android:divider="#android:color/white"
android:dividerHeight="1dp"
android:drawSelectorOnTop="false"/>
</android.support.v4.widget.DrawerLayout>
Hope anyone can help me. I am stuck with it for 3 days.
Thank you.
====================================
Edit on 19/11/15
I don't thing the problem is my itemView layout because when I change it to GridLayout it still doesn't fill the width.
Here is my CustomAdapter for RecyclerView
public class LocationDetailsAdapter extends RecyclerView.Adapter<LocationDetailsViewHolder> {
Context _context;
ArrayList<LocationDetails> _data;
public LocationDetailsAdapter(Context _context, ArrayList<LocationDetails> _object) {
this._context = _context;
_data = _object;
}
#Override
public LocationDetailsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location,null );
LocationDetailsViewHolder _viewHolder = new LocationDetailsViewHolder(_v);
return _viewHolder;
}
#Override
public void onBindViewHolder(LocationDetailsViewHolder locationDetailsViewHolder, int i) {
LocationDetails _location = _data.get(i);
locationDetailsViewHolder._imgType.setImageResource(R.drawable.repair_img);
locationDetailsViewHolder._locationName.setText(_location.get_locationName());
locationDetailsViewHolder._locationAddress.setText(_location.get_locationAddress() + ", " + _location.get_district() + ", " + _location.get_province());
locationDetailsViewHolder._distance.setText(String.valueOf(_location.get_distance()) + " km");
locationDetailsViewHolder._locationName.setOnClickListener(clickListener);
locationDetailsViewHolder._imgType.setOnClickListener(clickListener);
locationDetailsViewHolder._locationAddress.setOnClickListener(clickListener);
locationDetailsViewHolder._locationName.setTag(locationDetailsViewHolder);
locationDetailsViewHolder._imgType.setTag(locationDetailsViewHolder);
locationDetailsViewHolder._locationAddress.setTag(locationDetailsViewHolder);
}
#Override
public int getItemCount() {
return (null != _data ? _data.size() : 0);
}
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
LocationDetailsViewHolder _holder = (LocationDetailsViewHolder)v.getTag();
int _pos = _holder.getPosition();
int _id = _data.get(_pos).get_id();
Intent _intent = new Intent(CommonFields._context, ItemView.class);
_intent.putExtra("LocationID",_id);
_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
CommonFields._context.startActivity(_intent);
}
};
}
and here I get RecylerView in main
_locationView = (RecyclerView)findViewById(R.id.locationList);
_locationView.setLayoutManager(new LinearLayoutManager(this));
_adapter = new LocationDetailsAdapter(Main.this, CommonFields._locationData);
_locationView.setAdapter(_adapter);
When inflating a View from a LayoutInflater, you need to pass a parent parameter in order for layout_* attributes to be used. That's because these attributes need to create the correct LayoutParams class. That means that you can't use inflate(R.layout.*, null), but must instead pass a ViewGroup for the second parameter. In most cases, you also want to use the three-parameter version of the method and pass false as the third parameter. If this is omitted or true then the View is immediately added to the parent, which causes problems in places like onCreateViewHolder() because the framework is designed to perform this operation later instead. For more details, see this answer.
In your case, you have the line
View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location,null );
You should change it to
View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location, viewGroup, false );
You should create View like this
#Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
// create a new view
View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.card_view, viewGroup, false);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
Maybe my answer has a different approach, but it works to occupy the entire width of the screen and not break the elements when passing through the recycling view.
Adapter adapterBalanceInquiry = new Adapter(getActivity(), list);
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(adapterBalanceInquiry);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
recyclerView.post(new Runnable() {
#Override
public void run() {
try {
int dx = (recyclerView.getWidth() - recyclerView.getChildAt(0).getWidth());
recyclerView.scrollBy(-dx, 0);
LinearSnapHelper snapHelper = new LinearSnapHelper();
recyclerView.setOnFlingListener(null);
snapHelper.attachToRecyclerView(recyclerView);
}catch (Exception e){
e.printStackTrace();
}
}
});

Categories

Resources