I have a Fragment with a SwipeRefreshLayout and then RecyclerView as child.
When the Fragment is committed, it starts talking to a server in order to retrieve some data and populate a CustomAdapter which will serve the RecyclerView.
The user can refresh the content by swiping down or pressing a button on the ActionBar. In the latter case, I manually call swipeRefreshLayout.setRefreshing(true). So no troubles until now.
The problem arises when I manually call the refresh state during the first loading of the RecyclerView (onStart()).
The progress indicator is not showed up and I assume because the RecyclerView is still empty since it requires some seconds to retrieve all the data...
I leave you some code.
Fragment code:
public class MyFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.fragment_stop, container, false);
mRecyclerView = (RecyclerView) mView.findViewById(R.id.recyclerView1);
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),
DividerItemDecoration.VERTICAL_LIST));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//SwipeRefreshLayout retrieved and configured
swipeLayout = (SwipeRefreshLayout) mView.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorSchemeResources(
R.color.primaryColor,
R.color.cyan_500,
R.color.light_green_500,
R.color.amber_500);
return mView;
}
...
#Override
public void onStart() {
super.onStart();
executeSchedule(); //First execution and data loading.
}
...
#Override
public void onRefresh() {
executeSchedule();
}
...
public void executeSchedule() {
new Schedule().execute();
}
private class Schedule extends AsyncTask<Void, Void, Void> {
...
#Override
protected void onPreExecute() {
super.onPreExecute();
if(!swipeLayout.isRefreshing()) {
swipeLayout.setRefreshing(true); //set refresh state
}
}
#Override
protected Void doInBackground(Void... params) {
...
}
#Override
protected void onPostExecute(Void result) {
adap = new CustomAdapter(getActivity(), ...);
mRecyclerView.setAdapter(adap);
swipeLayout.setRefreshing(false);
}
}
XML code:
<FrameLayout 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingBottom="2dp"
android:layout_marginTop="5dp"
android:divider="#e0e0e0"
tools:context=".MyFragment" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
</FrameLayout>
What would be the best practice to address this situation?
Adding a second dummy SwipeRefreshLayout? Immediately force an empty adapter?
I had a problem with SwipeRefreshLayout not working with an empty recycler, so I created a very simple empty adapter.
public class EmptyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return 0;
}
}
I just wrap RecyclerView with RelativeLayout inside SwipeRefreshLayout and the problem is solved.
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
You need to call measure method before call setRefreshing(true).
Take a look at: SwipeRefreshLayout no animation on fragment creation. Maybe it can be helpful.
Related
I have an app that would benefit from a recyclerView nested in a recyclerView.
The issue I have though is that when I remove a textView outside on the recyclerView, the inner recyclerView doesn't redraw itself.
My app allows the user to read pages (RecyclerView with multiple paragraphs (nested RecyclerView)
The user can click the chapter title to get some added definitions/context displayed and if he clicks the definition it goes away.
The issue is when the definition goes away the nested RecyclerView doesn't redraw/re-size itself without a page swipe back and forth, could this be resolved?
I'm not android expert so I'm probably doing something wrong :S
My activity_main.xml
<?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:id="#+id/pageContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_above="#+id/definitionContainer">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/definitionContainer"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="120dp"
android:visibility="gone"
android:onClick="hideDefinition">
<View android:id="#+id/devider" android:layout_marginTop="1dp" android:layout_marginBottom="1dp" android:background="#color/colorAccent" android:layout_width="match_parent" android:layout_height="2px" android:layout_alignParentTop="true"/>
<ScrollView
android:id="#+id/definitionScrollView"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_below="#+id/devider"
android:background="#color/colorPrimaryDark"
android:onClick="hideDefinition">
<TextView
android:id="#+id/definitionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="Lorem ipsum dolor sit amet, ..."
android:onClick="hideDefinition" />
</ScrollView>
</RelativeLayout>
</RelativeLayout>
My MainActivity.java
public class MainActivity extends AppCompatActivity {
#BindView(R.id.page) RecyclerView page;
#BindView(R.id.definitionContainer) RelativeLayout definitionContainer;
private ChapterContainerAdapter chapterAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//if (page == null) page = findViewById(R.id.page);
page.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
page.setItemAnimator(new DefaultItemAnimator());
chapterAdapter = new ChapterContainerAdapter(this);
page.setAdapter(chapterAdapter);
SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(page);
}
public void showDefinition (View v) {
definitionContainer.setVisibility(View.VISIBLE);
}
public void hideDefinition (View v) {
definitionContainer.setVisibility(View.GONE);
}
}
My recyclerView Adapter is very basic:
public class ChapterContainerAdapter extends RecyclerView.Adapter<BaseViewHolder> {
private Context ctx;
static final String pageData[] = {"Page 1", "Page 2", "Page 3", "Page 4", "Page 5"};
public ChapterContainerAdapter(Context ctxIn) { ctx = ctxIn; }
#Override public void onBindViewHolder(BaseViewHolder holder, int position) { holder.onBind(position); }
#Override public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ChapterContainerAdapter.ChapterAdapterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_page, parent, false));
}
#Override public int getItemCount() { return pageData.length; }
public class ChapterAdapterViewHolder extends BaseViewHolder {
#BindView(R.id.chapterTitle)
TextView chapterTitle;
#BindView(R.id.chapterContentRecyclerView)
RecyclerView chapterContentRecyclerView;
ParagraphAdapter paragraphAdapter;
public ChapterAdapterViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
protected void clear() { chapterTitle.setText(""); }
public void onBind(int position) {
super.onBind(position);
initiateVerseRecyclerView(position);
}
private void initiateVerseRecyclerView (int position) {
chapterTitle.setText(pageData[position]);
chapterContentRecyclerView.setLayoutManager(new LinearLayoutManager(ctx, RecyclerView.VERTICAL, false));
chapterContentRecyclerView.setItemAnimator(new DefaultItemAnimator());
paragraphAdapter = new ParagraphAdapter(ctx, position);
chapterContentRecyclerView.setAdapter(paragraphAdapter);
}
}
}
Did you to call notifyDataSetChange manually on adapter after removing view?
I have the following code:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
tools:context=".bakers">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
...
in an activity. I have a few images on the activity and know I won't be able to fit them all on one screen, so I added a ScrollView with a height that is much larger than the screen. However, all this does is simply scale the existing images I have so that they are larger and take up more of the screen. I've tried fixing the fillViewport and clipToPadding settings, but this doesn't help anything.
Essentially what I'm asking is: Is there a way to add an image "below" the preview on screen using a ScrollView, so you can fit more on the screen than you normally would be able to? If I make the phone screen larger or the ScrollView larger, the images simply scale up.
Thanks
First you need use an recyclerview RecyclerView documentation
your activity view will have this code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/buttonAction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button action" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:scrollingCache="true" />
</LinearLayout>
you need create another layout in this case the layout that will display your image, example item_image.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_marginTop="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/image_1"/>
</LinearLayout>
well now you have your views.
the next is create a new class thats implement an adapter: CardAdapter.java
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
Context context;
public void setContext(Context context) {
this.context = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imgCover;
public ViewHolder(final View itemView, int type) {
super(itemView);
imgCover = itemView.findViewById(R.id.image);
}
}
#Override
public int getItemViewType(int position) {
return 1;
}
#NonNull
#Override
public CardAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = null;
RecyclerView.LayoutParams lp;
switch (viewType) {
case 1:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card_payment, null, false);
lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
break;
}
return new CardAdapter.ViewHolder(view, viewType);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return 10;
}
public Context getContext() {
return context;
}
}
and in your activity you will implement the recycler and the adapter
public class CardsActivity extends AppCompatActivity {
RecyclerView recyclerView;
CardAdapter cardAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cards);
initToolbar();
recyclerView = findViewById(R.id.recyclerView);
cardAdapter = new CardAdapter();
cardAdapter.setContext(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(cardAdapter);
}
}
and its all.
I'm learning about fragments and i made this app to display a list of recipes, Each row of this list have an image of the recipe and the name of the recipe and i put the recyclerView inside a fragment but the problem is when i open the application the content of the recyclerView gets messed up and returns to normal after scrolling down as shown in the two screenshots below.
before scrolling
after scrolling down
MainActivity class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListFragment listFragment = new ListFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.placeHolder, listFragment);
fragmentTransaction.commit();
}}
ListAdapter class
public class ListAdapter extends RecyclerView.Adapter{
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,
false);
return new ListViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((ListViewHolder) holder).bindView(position);
}
#Override
public int getItemCount() {
return Recipes.names.length;
}
private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView mRecipeText;
private ImageView mRecipeImage;
public ListViewHolder(View itemView) {
super(itemView);
mRecipeImage = itemView.findViewById(R.id.itemImage);
mRecipeText = itemView.findViewById(R.id.itemText);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
}
public void bindView(int position) {
mRecipeText.setText(Recipes.names[position]);
mRecipeImage.setImageResource(Recipes.resourceIds[position]);
}
}}
listFragment class
public class ListFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container , false);
RecyclerView recyclerView = view.findViewById(R.id.listRecyclerView);
ListAdapter listAdapter = new ListAdapter();
recyclerView.setAdapter(listAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
return view;
}}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="#+id/placeHolder"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.elswefi.smellslikebakin.MainActivity">
</FrameLayout>
fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/listRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.constraint.ConstraintLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
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="wrap_content">
<ImageView
android:id="#+id/itemImage"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:layout_margin="8dp"
android:scaleType="fitCenter"
app:srcCompat="#drawable/bagels"
/>
<TextView
android:id="#+id/itemText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_margin="12dp"
android:text="TextView"
android:textSize="24sp"
/>
</LinearLayout>
I hope anyone could help me because i'm still learning and i don't have any idea where this bug came from.
Problem solved
I solved the problem by adding this line of code
android:adjustViewBounds="true"
to the imageView in the list_item.xml file
thank you guys for your comments i really appreciate it.
I am using fragment and recyclerview together. I also have database and I want to query results coming from the database and display the results inside the activity.
However every time I try to run the application and switch to the part to get and view the results, I don't seem to get anything. No results at all just blank. I don't know why it's not showing up.
This is my full code
Sample class
public class Sample extends Fragment {
private View rootView;
public Sample() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
rootView = inflater.inflate(sample, container, false);
RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView_sample);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setHasFixedSize(true);
DatabaseHandler db= new DatabaseHandler(getActivity());
List<SampleModel> list = db.getResults();
SampleAdapter sampleAdapter = new SampleAdapter(list);
mRecyclerView.setAdapter(sampleAdapter);
return rootView;
}
}
Adapter Class
public class SampleAdapter extends RecyclerView.Adapter <SampleAdapter.ViewHolder> {
private List<SampleModel> list;
private Context mContext;
public SampleAdapter (List<SampleModel> list) {
list = list;
}
#Override
public SampleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.sample_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(SampleAdapter.ViewHolder holder, int position) {
SampleModel sample = list.get(holder.getAdapterPosition());
holder.title.setText(sample.getTitle())
}
#Override
public int getItemCount() {
return (list != null? list.size():0);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
}
}
}
XML
sample.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true">
<include layout="#layout/sample_recyclerview" />
</android.support.design.widget.CoordinatorLayout>
sample_recyclerview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"/>
</RelativeLayout>
sample_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/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:focusable="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="#808080">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff" />
</LinearLayout>
</android.support.v7.widget.CardView>
In your SampleAdapter change
list = list;
to
this.list = list;
Your Adapter constructor is wrong. You are not passing the dataSource to your adapter. Change it to
public SampleAdapter (List<SampleModel> list) {
this.list = list;
}
Furthermore you have to notify the adapter that you changed your dataSource. You do that by calling the method notifyDataSetChanged
SampleAdapter sampleAdapter = new SampleAdapter(list);
mRecyclerView.setAdapter(sampleAdapter);
sampleAdapter.notifyDataSetChanged();
RecyclerViews will return nothing if the size is not rightly returned(e.g using : return 0;)
change your code from :
public int getItemCount() {
return (list != null? list.size():0);
To:
public int getItemCount() {
return list.size();
I followed the recyclerview guidelines and built one for the app I am making, but it does not scroll to the bottom for some reason. I compared it with google code snippets, as well as other code snippets online and can't see the difference. I have posted a picture and the code I am using. I am using tabs, therefore the recyclerview is populated in a fragment.
What the app looks like:
http://imgur.com/H5uOLFR
the adapter class:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<Group> groups;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView groupName;
public TextView groupDate;
public TextView groupLocation;
public TextView className;
public ViewHolder(View v) {
super(v);
groupName = (TextView) v.findViewById(R.id.groupName);
groupDate = (TextView) v.findViewById(R.id.groupDate);
groupLocation = (TextView) v.findViewById(R.id.groupLocation);
className = (TextView) v.findViewById(R.id.className);
}
}
/*
* TODO: finish this method
*/
public void add(int position, String item) {
notifyItemInserted(position);
}
public void remove(String item) {
int position = groups.indexOf(item);
groups.remove(position);
notifyItemRemoved(position);
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(List<Group> groupsList) {
groups = groupsList;
Log.d("TEST", "Number of Groups: " +
Integer.toString(groups.size()));
}
// Create new views (invoked by the layout manager)
#Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.group_view, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final Group group = groups.get(position);
// holder.groupName.setText(group.getName());
holder.groupName.setText(group.getName());
holder.groupDate.setText(group.getFormattedDate());
holder.groupLocation.setText(group.getLocation());
holder.className.setText(group.getParent().getName());
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return groups.size();
}
}
The Fragment class:
public class groupsFragment extends Fragment implements GroupLeaver, GroupRetriever {
private RecyclerView rv;
private List<Group> groups;
private ProgressDialog progressDialog;
#Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
Log.d("TEST", "Entered onCreate");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
AppMain.getController().retrieveGroups(groupsFragment.this);
Log.d("TEST", "Entered onCreateView");
View rootView = inflater.inflate(R.layout.groups_fragment, container, false);
rv = (RecyclerView) rootView.findViewById(R.id.recyclerView);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
Log.d("TEST", "Size of LIST: " + Integer.toString(groups.size()));
MyAdapter adapter = new MyAdapter(groups);
rv.setAdapter(adapter);
return rootView;
}
#Override
public void onMyGroupsFound(List<Group> groups) {
Log.d("TEST", "Entered onMyGroupsFound");
Logg.info(this.getClass(), "Found %d groups for member %s", groups.size(), User.getCurrentUser().getDisplayName());
this.groups = groups;
}
#Override
public void onGroupLeft(Group oldGroup) {
}
#Override
public void onGroupLeftFailed(Group group, ParseException e) {
}
}
The xml layout for the recyclerview:
<?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:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#null"/>
</FrameLayout>
The xml layout for the recyclerview items:
<?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="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="horizontal">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/groupName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Group Name"
/>
<TextView
android:id="#+id/groupDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Group Date"
/>
<TextView
android:id="#+id/groupLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Group Location"
/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/className"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="right"
android:text="Class Name"
/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
You can use these lines to scroll recyclerview to:
list.add(0, group);
adapter.notifyItemInserted(0);
recyclerview.scrollToPosition(0);
Thanks to everyone who responded, but turns out the problem was the version of RecyclerView I was compiling.
Previously I was compiling this
compile 'com.android.support:recyclerview-v7:22.0.0'
But I changed it to this and it worked
compile 'com.android.support:recyclerview-v7:22.2.0'
Credits to #roi divon for the answer: CoordinatorLayout with RecyclerView & CollapsingToolbarLayout
Maybe adding these lines to the recyclerView will do:
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
This is my recyclerView which is working:
<android.support.v7.widget.RecyclerView
android:id="#+id/menuRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"/>
I am not sure but I think problem might be Framelayout You can try with Linearlayout instead of Framelayout in your xml layout for the recyclerview items