android load RecyclerView when a RecyclerView reachs ends - android

I have an Endless RecyclerView with library PullLoadView
this RecyclerView is fill the layout, I want to when this RecyclerView load all its items and reach the end of end, I load another RecyclerView at bottom of it, which has its own adapter and row layout and continue .
something like this application I need :
first RecyclerView:
first RecyclerView reaches end and new RecyclerView show up
new RecyclerView continue the first RecyclerView in full layour screen
xml Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvOne"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

Replace your xml source with the below source
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="none">
<LinearLayout
android:id="#+id/activity_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvOne"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rvTwo"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

Put them in a NestedScrollView with a vertically oriented LinearLayout and Disable their scrolling by setting their linearlayout manager like this.
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this) {
#Override
public boolean canScrollVertically() {
return false;
}
};

Use ListView as your bottom View and in your ListView header put RecycerView
list_sample.xml (This is your main layout for activity set content)
<?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:id="#+id/content_main"
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">
<ListView
android:id="#+id/list_sample"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
header_recycler.xml(List view header layout)
<?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">
<android.support.v7.widget.RecyclerView
android:id="#+id/list_header"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
item_header.xml(items for header's list i.e. recyclerview one)
<?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/item_header_regular"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF2255" />
</LinearLayout>
list_item.xml (List view's item)
<?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/text"
android:layout_width="match_parent"
android:layout_height="50dp" />
</LinearLayout>
HeaderRecyclerView.java (RecyclerView adapter)
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class HeaderRecyclerView extends RecyclerView.Adapter<HeaderRecyclerView.HeaderHolder> {
private final Context context;
private List<String> items;
public HeaderRecyclerView(List<String> items, Context context) {
this.items = items;
this.context = context;
}
#Override
public HeaderHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_header, parent, false);
return new HeaderHolder(v);
}
#Override
public void onBindViewHolder(HeaderHolder holder, int position) {
String item = items.get(position);
holder.textView.setText(item);
}
#Override
public int getItemCount() {
if (items == null) {
return 0;
}
return items.size();
}
class HeaderHolder extends RecyclerView.ViewHolder {
TextView textView;
HeaderHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.item_header_regular);
}
}
}
BottomListAdapter.java (ListView adapter)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class BottomListAdapter extends BaseAdapter {
private List<String> items = new ArrayList<>();
private Context context;
public BottomListAdapter(List<String> items, Context context) {
this.items = items;
this.context = context;
}
#Override
public int getCount() {
return items.size();
}
#Override
public String getItem(int i) {
return items.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
TextView txtView;
if (convertView == null) {
convertView = LayoutInflater.from(context)
.inflate(R.layout.list_item, viewGroup, false);
txtView = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(new ViewHolder(txtView));
} else {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
txtView = viewHolder.txtView;
}
String string = getItem(i);
txtView.setText(string);
return convertView;
}
private static class ViewHolder {
public final TextView txtView;
public ViewHolder(TextView txtView) {
this.txtView = txtView;
}
}
}
In Your onCreate use the below to initialize as follwing
ListView listView = (ListView) findViewById(R.id.list_sample);
View view = getLayoutInflater().inflate(R.layout.header_recycler, null);
RecyclerView headerList = (RecyclerView) view.findViewById(R.id.list_header);
List<String> strings = new ArrayList<>();
for (int i = 0; i < 50; i++) {
strings.add(i + "");
}
headerList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
headerList.setAdapter(new HeaderRecyclerView(strings, this));
listView.addHeaderView(view);
listView.setAdapter(new BottomListAdapter(strings, this));

Related

How to create a 3/2 grid using grid view with base adapter android in java

6 grid should be shown without scrolling, that is it should be like setting layout_weight="1" & layout_height="1" for every grid cards.
6 cards should be shown respective of screen size.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<GridView
android:id="#+id/gvImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintVertical_weight="1"
app:layout_constraintHorizontal_weight="1"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
grid_layout.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="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
tools:srcCompat="#tools:sample/avatars" />
</LinearLayout>
MyAdapter.java
package com.aijishnu.baseadaptersample;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.aijishnu.baseadaptersample.databinding.GridLayoutBinding;
public class MyAdapter extends BaseAdapter {
Context c;
int items[];
MyAdapter(Context c, int arr[]) {
this.c = c;
items = arr;
}
#Override
public int getCount() {
return items.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
GridLayoutBinding itemBinding = GridLayoutBinding.inflate(LayoutInflater.from(parent.getContext()), parent,false);
holder = new ViewHolder(itemBinding);
holder.view = itemBinding.getRoot();
holder.view.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.binding.imageView.setImageResource(items[position]);
return holder.view;
}
private static class ViewHolder {
private View view;
private GridLayoutBinding binding;
ViewHolder(GridLayoutBinding binding) {
this.view = binding.getRoot();
this.binding = binding;
}
}
}
MainActivity.java
package com.aijishnu.baseadaptersample;
import android.os.Bundle;
import com.aijishnu.baseadaptersample.databinding.ActivityMainBinding;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
int[] itemsarray = new int[] {
R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
};
MyAdapter adapter = new MyAdapter(this, itemsarray);
binding.gvImages.setAdapter(adapter);
}
}
Above code results as like this, (This is not fit to screen height, also have scrolling option, I want 6 cards should fit to screen size)
I want to have like this(all cards equals according to screen size)
NB: These cards should be randomly generated using BaseAdapter or RecyclerAdapter
You can set the number of rows and columns of your GridLayout inside your xml file

Single Row Shows Insight RecyclerView

I have recyclerview and it shows just single records instead of two records. I have searched many post on google and found to modify height of listview items from "match_parent " To "Wrap_Content" hence I did the same but still it shows single records.
Activity Mumbai_Male
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MumbaiMale">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/myImg"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/age"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/education"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:scrollbars="vertical"
android:id="#+id/recyclerView" />
</LinearLayout>
CustomeAdapter is:
package com.maheshwaghela.mahesh.rukhivivah;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends
RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private MumbaiMale.IconData[] data;
public CustomAdapter (MumbaiMale.IconData[] data) {
this.data = data;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.activity_mumbai_male, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.name.setText(data[position].getPname());
holder.age.setText(data[position].getPage());
holder.education.setText(data[position].getPedu());
holder.imageView.setImageResource(data[position].getImgId());
}
#Override
public int getItemCount() {
return data.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView name;
public TextView age; public TextView education;
public ViewHolder(View itemView) {
super(itemView);
this.imageView = (ImageView) itemView.findViewById(R.id.myImg);
this.name = (TextView) itemView.findViewById(R.id.name);
this.age=(TextView) itemView.findViewById(R.id.age);
this.education=(TextView) itemView.findViewById(R.id.education);
}
}
}
Mumbai_Male.Java
package com.maheshwaghela.mahesh.rukhivivah;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class MumbaiMale extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mumbai_male);
IconData[] data = new IconData[] {
new IconData("Name:- Mahesh K. Waghela","Age:-45","Education:-B.com", R.drawable.dilip333),
new IconData ("Name:- Sunil K. Waghela","Age:-33","Education:-S.S.C.",R.drawable.hiteshhh)
};
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
CustomAdapter adapter=new CustomAdapter(data);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
public class IconData {
private String pname;
private String page;
private String pedu;
private int imgId;
public IconData(String name, String age, String edu, int imgId) {
this.pname = name;
this.page= age;
this.pedu= edu;
this.imgId = imgId;
}
public String getPname()
{
return pname;
}
public void setPname(String name)
{
this.pname = name;
}
public String getPage()
{
return page;
}
public void setPage(String age)
{
this.page=age;
}
public String getPedu()
{
return pedu;
}
public void setMyPedu(String edu)
{
this.pedu=edu;
}
public int getImgId()
{
return imgId;
}
public void setImgId(int imgId)
{
this.imgId = imgId;
}
}
}
Where I am wrong don't know but it shows single records instead of two records.
As per your current code, your RecyclerView will display both record for both wrap_content and match_parent. You can see second record if you will scroll your list.
Also You are getting this type of wired output because your are using the same layout as a Activity Layout and Adapter's item layout. So adapter will bind image, name etc.. but it will also display a blank RecyclerView for each row.
So the solution is just keep your RecyclerView inside activity_mumbai_male.xml and create new xml file for your RecyclerView item and inflate that layout xml file inside your adapter. Your issue will be fixed.
Please check below corrected code.
activity_mumbai_male.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MumbaiMale">
<LinearLayout
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="match_parent"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:scrollbars="vertical" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
item_list.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="horizontal">
<ImageView
android:id="#+id/myImg"
android:layout_width="90dp"
android:layout_height="90dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/education"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Replace your code with my below code in CustomAdapter.xml
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem = layoutInflater.inflate(R.layout.item_list, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
You are using RecyclerView, but I could not found your xml regarding Row, you have set text to the Simple Single TextView Which you have provide in your activity_mumbai.xml, To you recyclerview you need to create another xml, which is counted as a row, means single item of your RecyclerView, and Print your xml data in that view.
row_item_view.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/myImg"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/age"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/education"/>
</LinearLayout>
</LinearLayout>
and Change SetContentView in your custom adapter with this new xml.
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.row_item_view, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
This will fix your problem.

I am not able to see the grid view in my Bookmark which extends Fragment

I am facing an issue the problem is I can see the Fragment but not the
GridView which I have there created, in the LogCat no error nothing but somehow the code it is not working.
But if I remove GridView from bookmark.xml and only leave a TextView than I can show the TextView
I am trying to reach a GridView into a Fragment.
This is my code.
Fragment
public class FragmentBookmark extends Fragment {
View paramView;
public FragmentBookmark() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
paramView = inflater.inflate(R.layout.bookmark, container, false);
return paramView;
}
}
This is the Main Activity
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.AddFragment(new FragmentExplore(), "");
adapter.AddFragment(new FragmentBookmark(), "");
adapter.AddFragment(new FragmentStore(), "");
mViewPager.setAdapter(adapter);
mTabLayout.setupWithViewPager(mViewPager);
This is the xml for Bookmark Fragment
<?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"
android:gravity="center">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
Try like this using framelayout
<FrameLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:id = "#+id/gvList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="spacingWidth"
android:numColumns="2"/>
</FrameLayout>
You need to set adapter to your GridView as only GridView will never show anything as it is a simple ViewGroup.. Also rather than using GridView try to use RecyclerView in grid mode. Below is a simple tutorial you can follow-
RecyclerView as GridView with GridLayoutManager
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="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/m_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp" />
</LinearLayout>
grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="wrap_content"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageview1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:contentDescription="#null" />
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:singleLine="true"
tools:text="Category" />
</LinearLayout>
</android.support.v7.widget.CardView>
Adapter:
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context mContext;
private List<String> myNameList;
public MyAdapter(Context context, List<String> categoryInfo) {
mContext = context;
myNameList = categoryInfo;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
String myName = myNameList.get(position);
holder.tvName.setText(myName);
}
#Override
public int getItemCount() {
return myNameList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tvName;
MyViewHolder(View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
}
}
}
In your Activity or Fragment:
Context mContext = this;
RecyclerView mRecyclerView;
mRecyclerView = findViewById(R.id.m_recyclerView);
mRecyclerView.setLayoutManager(new android.support.v7.widget.GridLayoutManager(mContext, 3));
ArrayList<String> nameArray = new ArrayList<>();
nameArray.add("name 1");
nameArray.add("name 2");
nameArray.add("name 3");
nameArray.add("name 4");
mRecyclerView.setAdapter(new MyAdapter(mContext, nameArray));

I tries to implement recycler view with multiple view but I am not getting the desired result

Check out my code:
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.ls.LSException;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by Ankit on 3/25/2016.
*/
public class CustomAdapterRecyclerView extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
private ArrayList<String> questionsList;
LayoutInflater layoutInflater;
public CustomAdapterRecyclerView(Context context, ArrayList<String> questionsList) {
this.context = context;
this.questionsList = questionsList;
layoutInflater = LayoutInflater.from(context);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 1:
final View view = layoutInflater.inflate(R.layout.recycler_view_list, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view, context);
return myViewHolder;
default:
final View view1 = layoutInflater.inflate(R.layout.single_item_in_list, parent, false);
MyExpandedViewHolder myExpandedViewHolder= new MyExpandedViewHolder(view1, context,new ArrayList<String>(Arrays.asList("A","Aa")));
return myExpandedViewHolder;
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType())
{
case 1:
MyViewHolder vh = (MyViewHolder) holder;
vh.questionTitle.setText(questionsList.get(position));
break;
default:
MyExpandedViewHolder viewHolder= (MyExpandedViewHolder) holder;
viewHolder.getListView();
}
}
/*
#Override
public void onBindViewHolder(RecyclerView.V holder, int position) {
holder.questionTitle.setText(questionsList.get(position));
}*/
#Override
public int getItemCount() {
return questionsList.size();
}
#Override
public int getItemViewType(int position) {
if(position ==0|| position==1|| position==2)
{
return 0;
}
else
return 1;
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView questionTitle;
private Context context;
public MyViewHolder(final View itemView, Context context) {
super(itemView);
this.context = context;
itemView.setOnClickListener(this);
questionTitle = (TextView) itemView.findViewById(R.id.question);
questionTitle.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(context, "" + getAdapterPosition() + questionTitle.getText().toString(), Toast.LENGTH_LONG).show();
Log.d("TAG", "" + getAdapterPosition());
Intent intent = new Intent(context, ProgramViewer.class);
intent.putExtra("titlePos",getAdapterPosition());
intent.putExtra("title",questionTitle.getText().toString());
context.startActivity(intent);
}
}
public class MyExpandedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private Context context;
ListView listView;
public MyExpandedViewHolder(View itemView, Context context,ArrayList<String> mobileArray) {
super(itemView);
this.context = context;
ArrayAdapter adapter = new ArrayAdapter<String>(context, R.layout.expanded_list_inside_recycler_view, mobileArray);
listView= (ListView) itemView.findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
public ListView getListView()
{
return this.listView;
}
#Override
public void onClick(View v) {
Toast.makeText(context,getAdapterPosition()+"",Toast.LENGTH_LONG).show();
}
}
}
What I tried to do is to expand the RecyclerView item on click but first I have to implement recycler view and list view together. I am getting NullPointerException. Please tell me the problem in my code. The answer would be greatly appreciated.
Log file:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at kole.technicalbird.CustomAdapterRecyclerView$MyExpandedViewHolder.<init>(CustomAdapterRecyclerView.java:113)
at kole.technicalbird.CustomAdapterRecyclerView.onCreateViewHolder(CustomAdapterRecyclerView.java:43)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5476)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4701)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(
Layout File: single_item_in_list.xml
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:text="a"/>
</LinearLayout>
**recycler_view_list**
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:elevation="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:fontFamily="sans-serif"
android:text="ques"
android:padding="8dp"
android:layout_marginRight="32dp"
android:textSize="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:fontFamily="sans-serif"
android:layout_centerVertical="true"
android:padding="8dp"
android:text=">" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<ImageView
android:src="#android:drawable/divider_horizontal_dark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="0.2dp"
android:paddingTop="0.2dp" />
</LinearLayout>
<?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">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_view">
</ListView>
</LinearLayout>
expanded_list_inside_recycler_view
<?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">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_view">
</ListView>
</LinearLayout>
Where is your listview in your single_item_in_list.xml.
Your single_item_in_list.xml don't have listview,so when you use getListView() ,the listview is null.I guess this is the reason!

RecycleView displaying only the first item

Here is the problem: I create the simplest RecyclerView in the world, but it displays only the first item. I cannot understand why. Thanks for any help.
item_layout.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tv_detail"/>
</RelativeLayout>
activity_main.xml
<?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: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.bcit.moonlady.testrecycler.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/tv_hello"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/rv_details"
android:layout_below="#+id/tv_hello"/>
</RelativeLayout>
MainActivity.java
package com.bcit.moonlady.testrecycler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String[] data = {"test1", "test2", "test3"};
RecyclerView mRecView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecView = (RecyclerView)findViewById(R.id.rv_details);
mRecView.setHasFixedSize(true);
mRecView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mRecView.setAdapter(new DetailAdapter());
}
private class DetailView extends RecyclerView.ViewHolder {
TextView mTextView;
public DetailView(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.tv_detail);
}
public void bindView(String string) {
mTextView.setText(string);
}
}
private class DetailAdapter extends RecyclerView.Adapter<DetailView> {
#Override
public DetailView onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View v = layoutInflater.inflate(R.layout.item_layout, parent, false);
return new DetailView(v);
}
#Override
public void onBindViewHolder(DetailView holder, int position) {
String string = data[position];
holder.bindView(string);
}
#Override
public int getItemCount() {
return data.length;
}
}
}
You need to change your item_layout height as wrap_content
if your are using Android Support Library v 23.2.0 and above
<?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="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tv_detail"/>
</RelativeLayout>
I too made this common mistake. Just change your parent LinearLayout - height should be "wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height = "wrap_content"
..... >
..
..
</LinearLayout>
I had same problem, when RecyclerView was wrapped in ScrollView. You should use NestedScrollView instead.
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- other content -->
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Set the height to wrap_content of the RelativeLayout in the item_layout.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="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tv_detail"/>
</RelativeLayout>
it may help
customAdapter = new CustomRecycleradapter(arrItems);
recyclerView.setLayoutManager(new LinearLayoutManager(mParentActivity));
recyclerView.setAdapter(customAdapter);
ArrayList<ListofItems> mSource;
// adapter class
public CustomRecycleradapter(ArrayList<ListofItems> source) {
this.mSource = source;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
return new CustomHolder(view);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof CustomHolder) {
((CustomHolder) holder).customerName.setText(mSource.get(position).getCustomerNumber());
}
}
#Override
public int getItemCount() {
return mSource.size();
}
public void addItem(ArrayList<ListofItems> itemLists) {
mSource.addAll(itemLists);
notifyItemInserted(mSource.size() - 1);
}
public class CustomHolder extends RecyclerView.ViewHolder {
#Bind(R.id.customer_name)
TextView customerName;
#Bind(R.id.time_stamp)
TextView timeStamp;
#Bind(R.id.amount)
TextView amount;
public CustomHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
Activity:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecycler;
String[] data = {"test1", "test2", "test3"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecycler = (RecyclerView) findViewById(R.id.rv_details);
DetailAdapter adapter = new DetailAdapter();
LinearLayoutManager manager = new LinearLayoutManager(this);
mRecycler.setHasFixedSize(true);
mRecycler.setLayoutManager(manager);
mRecycler.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DetailAdapter extends RecyclerView.Adapter<DetailAdapter.DetailView> {
#Override
public void onBindViewHolder(DetailView holder, int position) {
String string = data[position];
holder.bindView(string);
}
#Override
public DetailView onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View v = layoutInflater.inflate(R.layout.item_layout, parent, false);
return new DetailView(v);
}
#Override
public int getItemCount() {
return data.length;
}
class DetailView extends RecyclerView.ViewHolder {
TextView mTextView;
public DetailView(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.tv_detail);
}
public void bindView(String string) {
mTextView.setText(string);
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rv_details"
/>
</RelativeLayout>
item_layout.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tv_detail"/>
</RelativeLayout>
First up, u should put your Recyclerview.veiwholder class inside the adapter to ensure youre getting the same instance.
//replaces contents of a view, invoked by the layout manager
#Override public void onBindViewHolder(ViewHolder holder, int position) {
// get the message to display from the array at the specified position
// replace contents of the view with the new element
holder.mytextview.setText(recieveHistory.get(position));
}

Categories

Resources