i want to reuse an inflated view just the same way a listView adapter does with convertView. Ive been lurking on the source code of adapters with no luck.
I need to add dinamically views depending on data to a recyclerItemView, now im inflating as much times as i need the view but having in mind its inside a recyclerView, this operation could become absurdly costly depending on the amount of data and scrolling behaviour of the user.
Other solution to my problem would be to create a listview inside the recyclerItem, so the listview wouldnt be scrollable, expands to maximum height depending on data added dynamically(making the listview container to expand to show all its data) and make the recyclerItem expand depending on its height.
ACTIVITY
public class Main1Activity extends AppCompatActivity {
RecyclerView recyclerView;
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
setUpRecycler();
}
private void setUpRecycler(){
Sample sample1 = new Sample("List of Items1");
Sample sample2 = new Sample("List of Items2");
Sample sample3 = new Sample("List of Items3");
List<Sample> sampleList = new ArrayList<>();
sampleList.add(sample1);
sampleList.add(sample2);
sampleList.add(sample3);
adapter = new CustomAdapter(this,sampleList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
MODEL
public class Sample {
String name;
public Sample(){}
public Sample(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
XML ITEMS
**item_list**
<?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="40dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
**item_smaple**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_margin="10dp"
android:text="List of Items"
android:textSize="20sp"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:orientation="vertical"/>
</LinearLayout>
ADAPTER
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private List<Sample> mySamples;
private Context mContext;
public CustomAdapter(Context context, List<Sample> mySamples) {
this.mContext = context;
this.mySamples = mySamples;
}
private Context getContext() {
return mContext;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public LinearLayout linearLayout;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
}
}
#Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.item_sample, parent, false);
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
#Override
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {
Sample sample = mySamples.get(position);
holder.name.setText(sample.getName());
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.item_list, null, false);
for (int i = 0; i < 20; i++){
TextView textView = (TextView) v.findViewById(R.id.textView);
textView.setText("hello"+i);
holder.linearLayout.addView(v);
}
}
#Override
public int getItemCount() {
return mySamples.size();
}
}
Right now i´m inflating inside the loop. How could i modify view parameters to make it reusable to the framework?
My app crashes on the addView()
-->java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
The issue that you are referring to is the one which was addressed by creating RecyclerView which is an upgrade to ListView. What RecyclerView does is that it will create/inflate say 5-10 list item and when the user starts scrolling new listItem view(s) are not inflated infact just the data is updated inside the listItem.
This is a great explainer video of how recycler view actually works with a bit of code as well. https://www.youtube.com/watch?v=Wq2o4EbM74k
I hope this clears your doubt regarding the inflation issue.
My app crashes on the addView() -->java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
try:
#Override
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {
Sample sample = mySamples.get(position);
holder.name.setText(sample.getName());
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context
.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 20; i++) {
View v = inflater.inflate(R.layout.item_list, holder.linearLayout, false);
TextView textView = (TextView) v.findViewById(R.id.textView);
textView.setText("hello" + i);
holder.linearLayout.addView(v);
}
}
itemSample.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_margin="10dp"
android:text="List of Items"
android:textSize="20sp"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="30dp"
android:orientation="vertical"
android:isScrollContainer="true"
/>
</LinearLayout>
itemList.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="40dp"
android:isScrollContainer="true"
android:id="#+id/txt_cont"
>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
You should try answer #
Related
i have a RecyclerView with a GridLayoutManager which load items like this:
as you can see there is a margin to left. I didn't add any sort of ItemDecoration to recyclerview and didn't add any margin to any of the layouts here.
i've already tried:
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
and using LinearSnapHelper but none of them fixed it.
item layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="#dimen/_130sdp"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="#dimen/_130sdp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<androidx.cardview.widget.CardView
app:cardPreventCornerOverlap="true"
app:cardElevation="#dimen/_5sdp"
app:cardCornerRadius="#dimen/_3sdp"
android:layout_width="#dimen/_60sdp"
android:layout_height="#dimen/_90sdp"
android:layout_marginTop="#dimen/_20sdp">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/choosestorycoverimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:riv_corner_radius="#dimen/_3sdp"
android:scaleType="fitXY"
android:src="#drawable/test"/>
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/choosestorycoverimagelayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:riv_corner_radius="#dimen/_3sdp"
android:visibility="gone"
android:scaleType="fitXY"
android:src="#drawable/choosen_story_layer"/>
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/choosestorynametext"
android:layout_marginTop="#dimen/_3sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/_15sdp"
android:text="text"/>
</LinearLayout>
</RelativeLayout>
activity layout:
<?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"
tools:context=".ChooseStoryActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/choosestoryrecyclerview"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_gravity="center_horizontal"
android:layout_height="match_parent" />
</RelativeLayout>
recyclerview adapter:
public class ChooseStoryRecyclerViewAdapter extends RecyclerView.Adapter<ChooseStoryRecyclerViewAdapter.ViewHolder> {
private static final String TAG = "LibraryRecyclerViewAdap";
private ArrayList<String> coverImageUrlList = new ArrayList<>();
private ArrayList<String> nameTextList = new ArrayList<>();
private ArrayList<String> shelfNameList = new ArrayList<>();
private ArrayList<HashSet<String>> shelfStoryIDList = new ArrayList<>();
private HashSet<String> shelfStoryIDSet = new HashSet<>();
private Context context;
public ChooseStoryRecyclerViewAdapter(ArrayList<String> coverImageUrlList, ArrayList<String> nameTextList,Context context) {
this.coverImageUrlList = coverImageUrlList;
this.nameTextList = nameTextList;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.choose_story_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
//***********the main method***********
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Glide.with(context)
.asBitmap()
.load(coverImageUrlList.get(position))
.into(holder.coverImage);
holder.coverImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(holder.coverImageLayer.getVisibility() == View.VISIBLE){
holder.coverImageLayer.setVisibility(View.GONE);
}
else{
holder.coverImageLayer.setVisibility(View.VISIBLE);
}
}
});
holder.nameText.setText(nameTextList.get(position));
}
#Override
public int getItemCount() {
return coverImageUrlList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
RoundedImageView coverImage;
TextView nameText;
RoundedImageView coverImageLayer;
public ViewHolder(#NonNull View itemView) {
super(itemView);
coverImage = itemView.findViewById(R.id.choosestorycoverimage);
nameText =itemView.findViewById(R.id.choosestorynametext);
coverImageLayer = itemView.findViewById(R.id.choosestorycoverimagelayer);
}
}
}
Remove these unnecessary attributes from your recyclerView tag:
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
You should change the width of the parent RelativeLayout of your item layout to use match_parent instead of a fixed width and then play with the spanCount attribute of your GridLayoutManager if you want smaller item layout
You should also add the GridLayoutManager directly in your activity_main.xml if you don't really need any Custom GridLayoutManager.
For that, remove the GridLayoutManager from MainActivity.java if you have one and add these two new lines in your recyclerView tag
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="3"
I have a ListView
<ListView
android:layout_width="match_parent"
android:divider="#null"
android:dividerHeight="0dp"
android:layout_height="match_parent"
android:id="#+id/listView1"/>
Here i'm adding some item using my custom adapter.
CustomAdapter adapter = new CustomAdapter(this, myArrayList);
ListView listView = (ListView) findViewById(R.id. listView1);
listView.setAdapter(adapter);
In my custom Adapter, i used also a custom layout, where only one textVeiw:
<?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="match_parent">
<TextView
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/custom_textView"/>
</LinearLayout>
Everything is good. Working properly. The textView displaying texts line by line.
Example:
<TextView>
<TextView>
<TextView>
and so on..
But, i don't want to display text line by line. i want to display text one immediately after another.
Like:
<TextView><TextView><TextView><TextView><TextView><TextView>
but it'll take a new line when it reaches the end of its parent or device screen.
Thanks in advance.
I think you want horizontal list view instead of vertical list view.
Following Step are there :
Implement Recyclerview in your layout (XML) “your_activity.xml” file.
<android.support.v7.widget.RecyclerView
android:id="#+id/horizontalRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite">
</android.support.v7.widget.RecyclerView>
Implement List Adapter Item Layout (XML) “item_list.xml” file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="#+id/lnrItem"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Name"
android:textColor="#color/colorWhite"
android:textSize="14sp" />
</LinearLayout>
Implement Custome List Adapter “CustomListAdapter.java”
public class CustomListAdapter extends RecyclerView.Adapter< CustomListAdapter.ViewHolder> { private Activity context; private ArrayList<String> list = new ArrayList<String>();
public CustomListAdapter(Activity context, ArrayList<String> list) {
this.context = context;
this.list = list;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
try {
holder.getTxtMessage().setText(list[position]);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView txtMessage;
public ViewHolder(View view) {
super(view);
txtMessage = (TextView) view.findViewById(R.id.txtMessage);
}
public TextView getTxtMessage() {
return txtMessage;
}
}
Set Adapter in Activity “YourActivity.java” in OnCreate Method.
#Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_activity);
RecyclerView horizontalRecyclerView = (RecyclerView) findViewById(R.id.horizontalRecyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); horizontalRecyclerView.setLayoutManager(linearLayoutManager);
ArrayList<String> list = new ArrayList<>();
list.add("First TextView ");
list.add("Second TextView ");
list.add("Third TextView ");
list.add("Four TextView ");
list.add("Fifth TextView ");
private CustomListAdapter adapter = new CustomListAdapter(YourActivity.this, list); horizontalRecyclerView.setAdapter(topMemberAdapter);
}
I am having problems displaying all the items in my recyclerview. My code only shows one item (Home. I have 3 items: Home, Browse Photos and My Travels)
fragment_navigation_drawer.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_navigation_drawer"
android:background="#EEE"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.traveldiaries.ztir.traveldiaries.NavigationDrawerFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#16a085"
android:id="#+id/linearLayout">
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<MenuList> data = Collections.emptyList();
public RecyclerAdapter(Context context, List<MenuList> data){
inflater= LayoutInflater.from(context);
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.itemlist,parent,false);
MyViewHolder holder= new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MenuList current=data.get(position);
holder.title.setText(current.title);
holder.image.setImageResource(current.icon);
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.item_list);
image = (ImageView) itemView.findViewById(R.id.icons);
}
}
}
NavigationDrawerFragment.java (posted code only related to recyclerview)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
adapter = new RecyclerAdapter(getActivity(),getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<MenuList> getData(){
List<MenuList> data = new ArrayList<>();
String[] titles={"Home","My Travels","Browse Photos"};
int[] icons ={R.drawable.home,R.drawable.sunglasses,R.drawable.tooltip_image};
for(int i=0;i<3;i++){
MenuList current = new MenuList();
current.title=titles[i];
current.icon=icons[i];
Log.d("Data",titles[i]);
Log.d("Data",""+icons[i]);
data.add(current);
}
return data;
}
itemlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:padding="8dp"
android:id="#+id/icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/item_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dummy Text"
android:layout_gravity="center_vertical"
android:padding="8dp"
/>
</LinearLayout>
What I was able to do is to check all the passed data (all seems to be fine). Also tried to change width to wrap content (fragment_navigation_drawer.xml) in both my relative and linear layout but I just lose my background color instead and still only one item appear.
Change height of LinearLayout to wrap_content in itemlist.xml
android:layout_height="wrap_content"
Just change the height of your itemlist from "match_parent" to "wrap_content".
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
I have a cardview with 2 textviews inside. The first textview displays the name of a contact and the second displays the phone number. I want to use an ArrayList to populate the recyclerview.
I'm still new to Android programming so I don't know how to implement RecyclerView or make an adapter that will alternate between name and number filling in the textview. How can I achieve this?
My CardView (card_row.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="match_parent"
android:layout_margin="5dp"
android:clickable="true"
android:orientation="horizontal"
card_view:cardCornerRadius="0dp"
card_view:cardUseCompatPadding="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="38dp"
android:layout_margin="5dp"
android:gravity="left"
android:textColor="#android:color/black"
android:textSize="24sp" />
<TextView
android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="37dp"
android:layout_margin="5dp"
android:layout_below="#id/text1"
android:textColor="#android:color/darker_gray"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
EDIT:
I made 2 arraylists, one for names and the other for numbers. Now I how do I populate the recyclerview?
OK First make a inner class. This will be your adapter change all the id:
class Adapter extends RecyclerView.Adapter<LibraryAdapter.MyViewHolder>{
LayoutInflater inflater;
public LibraryAdapter(Context context){
inflater = LayoutInflater.from(context);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view =inflater.inflate(R.layout.main_exerciselibrary_fragment_singlerow,parent,false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Exercises exercises = new Exercises(getActivity());
ArrayList<String> arrayList1 = new ArrayList<String>();
ArrayList<String> arrayList2 = new ArrayList<String>();
holder.Name.setText(arrayList1.get(position));
holder.PhoneNumber.setText(arrayList2.get(position));
}
#Override
public int getItemCount() {
return array.lenght;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView Name, PhoneNumber;
CardView card;
public MyViewHolder(View itemView) {
super(itemView);
PhoneNumber= (TextView) itemView.findViewById(R.id.imageViewPicLibrary);//change Here
Name = (TextView) itemView.findViewById(R.id.textViewExerciseLibraryExerciseName);//change Here
card= (CardView) itemView.findViewById(R.id.card_view_ex_lib);//change Here
card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//OnClick
}
});
}
}
}
I think this is all.