RecycleView does not scroll to the last items - android

I have a List View with Vertical Scroll, its item contains RecyclerView which will scroll Horizontally.
But when i scroll horizontal, RecyclerView does not allow me to scroll to the last item in the list (the picture below). What's happened with it? Even, i forced it scroll to the last item by setting linearLayoutManager.setStackFromEnd(true); it still does not work, only scroll to half of the 8th item, not the last item in list of 10
Edit: add codes:
list.xml
<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="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.phongponix.trackingbodybuilding.MainActivity$PlaceholderFragment">
<ListView android:id="#+id/lvExerciseList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="50dp"
></ListView>
</RelativeLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="20dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imgExcercisePhoto"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView android:text="aaa"
android:id="#+id/tvExerciseName"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvExcerciseRecords"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:layout_margin="10dp"
android:paddingRight="50dp"
android:scrollbars="horizontal">
</android.support.v7.widget.RecyclerView>
</TableRow>
</TableLayout>
</LinearLayout>
list_item_horizontal.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="100dp"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingRight="10dp">
<TextView
android:id="#+id/tvTemp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Code for List Adapter:
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.list_item, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) view.findViewById(R.id.tvExerciseName);
view.setTag(viewHolder);
recyclerView = (RecyclerView)view.findViewById(R.id.rvExcerciseRecords);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(trackingPlanHorizontalAdapter);
recyclerView.setNestedScrollingEnabled(false);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (data.size() > 0) {
ExerciseModel exercise = (ExerciseModel) data.get(i);
viewHolder.title.setText(exercise.getTitle());
} else {
viewHolder.title.setText("No data");
}
return view;
}
Code for Recycle Adapter:
public TrackingPlanHorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_horizontal, parent, false);
return new TrackingPlanHorizontalViewHolder(itemView);
}
#Override
public void onBindViewHolder(TrackingPlanHorizontalViewHolder holder, int position) {
holder.textView.setText(horizontalList.get(position));
}

Yes i found your problem,there is problem regarding your list_item.xml change your list item with below code,
<?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"
android:padding="20dp">
<LinearLayout
android:id="#+id/first_panel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:src="#mipmap/ic_launcher"
android:id="#+id/imgExcercisePhoto"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="#+id/tvExerciseName"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="aaa" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_toRightOf="#+id/first_panel"
android:id="#+id/rvExcerciseRecords"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>

Related

setVisibility(View.Gone) work but the view still own the space

I make a CustomAdapter extends BaseAdapter.In it's getView() method , I use ViewHolder. And I set a clickListener with a TextView to set a view (call it A)gone and another view (call it B)visible , but when I click the TextView , the A GONE but it leave a space ,so the B cannot match the parent.
My code like
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null){
convertView = mLayoutInflater.inflate(R.layout.customlayout,parent,false);
viewHolder = new ViewHolder();
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView = (TextView) convertView.findViewById(R.id.textview);
viewHoler.a = (LinearLayout) convertView.findViewById(R.id.a);
viewHoler.b = (LinearLayout) convertView.findViewById(R.id.b);
viewHolder.textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (viewHolder.a.getVisibility() == View.GONE){
viewHolder.b.setVisibility(View.GONE);
viewHolder.a.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}else {
viewHolder.a.setVisibility(View.GONE);
viewHolder.b.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
}
});
convertView.setTag(viewHolder);
return convertView;
}
the customlayout code is
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/bg"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button a"/>
</LinearLayout>
<LinearLayout
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button b"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
It should be show like this way when I click the textView
but it always like another way below , just like the A view still take the space ,that like call setVisibility(View.INVISIBLE) or not setVisibility(View.GONE)
the A view is not appear because that although B view has disappeared but it still take the space
Why will it behave like that? How should I do to solve it ?
Thank you for your help.
Try with wrap_content on your LinearLayouts.
Like this -
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/bg"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button a"/>
</LinearLayout>
<LinearLayout
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button b"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

How to Inflate new layout inside cardview dynamically

I am trying to inflate the second XML inside cardview dynamically, but it throws null pointer exception.When I am trying to do hard code(by including directly inside card view layout) it works well.Is there any other way to inflate new XML dynamically?
CardView Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/room_detail_cards"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/room_detail_Linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/module_type_5"/>
<!--<include layout="#layout/module_type_3"/>-->
<!--<It Works well when i hard code the layout here/>-->
</LinearLayout>
</android.support.v7.widget.CardView>
RecyclerAdapter.java(onCreateViewHolder)
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
layout= (LinearLayout) parent.findViewById(R.id.room_detail_Linear_layout);
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.module_type_3, layout, false);
layout.addView(itemView); //NULL Pointer Exception occurs here
View itemView1 = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.room_detail_card_view, null, false);
return new ViewHolder(itemView1);
}
First layout XML(module_type_5)
<?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">
<TextView
android:hint="module"
android:id="#+id/module_5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Second XML Layout(module_type_3)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:hint="module"
android:id="#+id/module_5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
You are trying to get view id before inflating it at here:
layout= (LinearLayout) parent.findViewById(R.id.room_detail_Linear_layout);
That's why layout is null.
Use onCreateViewHolder to return an instance of ViewHolder and perform all assignments in the view holder.
Something like this would work:
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.room_detail_card_view, parent, false));
}
And then in your ViewHolder class:
public class ViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView)itemView.findViewById(R.id.textView);
}
}
And finally data binding in onBindViewHolder:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText("some data")
}

Make Square images in Volley.NetworkingImageView

I have the following xml implementation, but i would like to make the images bigger and square. Sorry if my question is easy, please consider that I started a couple of weeks on Android development.
Grid.xml
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"/>
GridItem.xml
<?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">
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:layout_gravity="bottom"
android:textColor="#android:color/white"
android:background="#55000000"/>
</FrameLayout>
Here is my adapter class code if needed:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
}
NetworkImageView imageView = (NetworkImageView) convertView.findViewById(R.id.picture);
TextView textView = (TextView) convertView.findViewById(R.id.text);
ImageRecord imageRecord = getItem(position);
imageView.setImageUrl(imageRecord.getUrl(),mImageLoader);
textView.setText(imageRecord.getTitle());
return convertView;
}
Start by setting a fixed size for your NetworkImageView:
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/picture"
android:layout_width="120dp"
android:layout_height="120dp"
android:scaleType="centerCrop"/>

Row item in recycler view does not respond to click event cardview

I have a RecyclerView with CardView the problem is that the click doesn't repsond(unless i press the bottom of the view).I have set the clickable="true"
but it doesn't help
this is my row_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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
android:clickable="true"
card_view:cardUseCompatPadding="true" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:foreground="?selectableItemBackground"
android:clickable="true">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tv_name"
android:gravity="center"
android:padding="10dp"
android:textColor="#color/material_deep_teal_200"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cb_check_box"
android:gravity="right"
android:layout_centerVertical="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="number"
android:clickable="true"
android:id="#+id/tv_number"
android:layout_below="#+id/tv_name"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is how i set the onClick:
// Create new views (invoked by the layout manager)
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(getActivity())
.inflate(R.layout.contacts_list_item, parent, false);
// set the view's size, margins, paddings and layout parameters
v.setOnClickListener(this);
CustomViewHolder vh = new CustomViewHolder(v);
return vh;
}

Android add badge to list

I have a list,and I want to add badge to each item.I am using this library:https://github.com/jgilfelt/android-viewbadger
And this is my code:
static class ViewHolder {
TextView nameView;
ImageView imageView;
BadgeView badge;
}
public void placeRandomUsers(final String search) {
randomAdapter = new ArrayAdapter<JsonObject>(this, 0) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.random_bars, null);
holder = new ViewHolder();
holder.nameView=(TextView)convertView.findViewById(R.id.tweet);
holder.badge = new BadgeView(getContext(), holder.nameView);
holder.badge.setTextColor(Color.BLACK);
holder.badge.setBadgeBackgroundColor(Color.parseColor("#A4C639"));
holder.imageView=(ImageView)convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position >= getCount() - 3 && search.equals("") == true) {
//loadRandomUsers("");
}
JsonObject user=getItem(position);
String name=user.get("name").getAsString();
String image_url="http://domain/photos/profile/thumb/"+user.get("photo").getAsString();
holder.nameView.setText(name);
Ion.with(holder.imageView)
.placeholder(R.drawable.twitter)
.load(image_url);
holder.badge.setText("1");
holder.badge.show();
return convertView;
}
};
ListView listView = (ListView)findViewById(R.id.list);
listView.setAdapter(randomAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Log.w("Pos","asd"+randomAdapter.getItem(position).get("name").getAsString());
startChat(randomAdapter.getItem(position));
}
});
loadRandomUsers(search);
}
And this is my random_bars.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="4dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/twitter"/>
<LinearLayout
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
style="#android:style/TextAppearance.Medium"
android:id="#+id/handle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="#+id/tweet"
style="#android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
When I add badge to each item,nameView is not showing.Here the output:
If I remove the badge codes.Output is:
Here you can see.Image is showing but name is not showing when I use the badge class.What can be cause this ?
I changed my random_bars.xml code to this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="4dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/twitter"/>
<LinearLayout
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
style="#android:style/TextAppearance.Medium"
android:id="#+id/handle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="#+id/tweet"
style="#android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Problem fixed.

Categories

Resources