Viewpager with listview inside - java

i want to create a viewpager with one layout that contains a listview but i'm always getting this error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.myapp/com.example.myapp.Join_activity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a
null object reference
The viewpager works fine without the listview, and the listview works fine without the viewpager. I know the problem, i'm using "setContentView" with a different layout where my listview is, but i don't know how to solve it, please help, here is the code:
public class Join_activity extends AppCompatActivity {
ListView lv_zona;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.join_pager);//here is the problem
//my listview is in join3.xml
//ViewPager
viewPager = (ViewPager) findViewById(R.id.viewpaginador);
viewPager.setAdapter(new Join_Adapter(this));
//ListView
lv_zona = (ListView)findViewById(R.id.lv_zona);
lv_zona.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,new String[]{"A1","B1","C1","D1"}));
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
#Override
public void onPageSelected(int position) {}
#Override
public void onPageScrollStateChanged(int state){}
});
}//end oncreate
}//end class
*
public class Join_Adapter extends PagerAdapter {
private Context mContext;
public Join_Adapter(Context context) {
mContext = context;
}
#Override
public Object instantiateItem(ViewGroup coleccion, int posicion) {
ModelObject modelObject = ModelObject.values()[posicion];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup mlayout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), coleccion, false);
coleccion.addView(mlayout);
return mlayout;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view)
{collection.removeView((View) view);}
#Override
public int getCount()
{return ModelObject.values().length;}
#Override
public boolean isViewFromObject(View view, Object object)
{return view == object;}
#Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
My files:
My_files.jpg
My xmls:
***join3.xml - Here is my ListView***
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_zona"
android:choiceMode="singleChoice" />
</LinearLayout> </LinearLayout>
*
***join_pager.xml - my main layout***
<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"
tools:context=".Paginador">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpaginador"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

You should use fragments for this. You are using a view pager. Now you should display the list inside a fragment that you will load in the activity. This answer shows how to do that. Your fragment xml will contain the listView. And the getItem of the adapter will return the fragment.

The reason of NullPointerException is the list activity is not in the main layout, so, findViewById returns null.
To put a listview in a ViewPager, see: https://stackoverflow.com/a/12535150/189961

Related

Android Recycler returns zero output despite reaching onBindViewHolder() for each row

I've put a RecyclerView in the 'Tutorial' activity in my application, to list tutorial versions, which come from the Room database my app has. It is hosted within a fragment, but that's of little consequence since I had the same problem before putting it in there. The RecyclerView adapter reaches onBindViewHolder() without any issues and I get logs from the Log.w I used for debugging there with correct data. The problem seems to be that Layout does not get inflated at all, and the buttons that each row is supposed to output never appear.
THE ADAPTER
public class VersionListAdapter extends RecyclerView.Adapter<VersionListAdapter.VersionViewHolder> {
private List<Version> mVersionList;
private final LayoutInflater mInflater;
private final OnViewClickListener mListener;
public VersionListAdapter(Context context)
{
this.mInflater = LayoutInflater.from(context);
this.mVersionList = null;
this.mListener = (OnViewClickListener) context;
}
#NonNull
#Override
public VersionViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int viewType) {
View row = mInflater.inflate(R.layout.row_versions_rv, viewGroup, false);
return new VersionViewHolder(row, mListener);
}
#Override
public void onBindViewHolder(#NonNull VersionViewHolder versionHolder, int rowNumber) {
versionHolder.thisVersion = mVersionList.get(rowNumber);
versionHolder.versionButton.setText((mVersionList.get(rowNumber).getText()));
Log.w("THIS IS A TAG", mVersionList.get(rowNumber).getText()); //correct output
}
#Override
public int getItemCount() {
if(mVersionList!=null) return mVersionList.size();
else return 0;
}
public void setElementList(List<Version> versions){
this.mVersionList = versions;
notifyDataSetChanged();
}
public static class VersionViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener
{
OnViewClickListener listenerForThisRow;
Version thisVersion;
Button versionButton;
public VersionViewHolder(
#NonNull View viewForThisRow, OnViewClickListener listenerFromActivity)
{
super(viewForThisRow);
this.listenerForThisRow = listenerFromActivity;
versionButton = viewForThisRow.findViewById(R.id.version_button);
viewForThisRow.setOnClickListener(this);
}
#Override
public void onClick(View v){
listenerForThisRow.onViewClick(thisVersion);
}
}
public interface OnViewClickListener{
void onViewClick(Version version);
}
}
THE FRAGMENT CLASS WHICH HOSTS THE RECYCLER VIEW
public class VersionRecycler extends Fragment {
protected RecyclerView mRecycler;
protected VersionListAdapter mAdapter;
public VersionRecycler(){
super(R.layout.fragment_recycler_versions);
}
#Override
public View onCreateView(
#NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
) {
VersionViewModel mVersionViewModel = new VersionViewModel(requireActivity().getApplication());
long tutorialId = requireArguments().getLong("tutorialId");
View view = inflater.inflate(R.layout.fragment_recycler_versions, container, false);
mAdapter = new VersionListAdapter(requireActivity());
mRecycler = view.findViewById(R.id.versions_rv);
mRecycler.setLayoutManager(new LinearLayoutManager(view.getContext(), LinearLayoutManager.VERTICAL, false));
mVersionViewModel.getByTutorialId(tutorialId).observe(requireActivity(), versions->mAdapter.setElementList(versions));
mRecycler.setAdapter(mAdapter);
return view;
}
}
HOW THE FRAGMENT IS EMBEDDED IN THE ACTIVITY
<LinearLayout
android:id="#+id/layout_versions_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
FRAGMENT LAYOUT
<?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=".versionrecycler.VersionRecycler">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/versions_rv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="#layout/row_versions_rv"/>
</androidx.constraintlayout.widget.ConstraintLayout>
LAYOUT FOR RECYCLERVIEW ROW
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<Button
android:id="#+id/version_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You should also add Start and Bottom constraints:
<?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=".versionrecycler.VersionRecycler">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/versions_rv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:listitem="#layout/row_versions_rv"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You could also change RecyclerView layout_height to wrap_content and add Start constraint only

How to update fragment data using RecyclerView?

I have a ViewPager with 12 fragments. I need to get data from API and update the fragments when scrolling. How do I update the contents of fragment, with only one fragment updating for 12 pages?
My fragment layout fragment_item:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/surface">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/khaire"
android:gravity="center"
android:text="Overal Working Hour"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center"
android:padding="15dp"
android:text="January"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
My main activity code:
public class MainActivity extends AppCompatActivity {
private static final int num_pages = 12;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = findViewById(R.id.pager);
mPagerAdapter = new ScreenAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
private class ScreenAdapter extends FragmentStatePagerAdapter {
public ScreenAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return new SliderFragment();
}
#Override
public int getCount() {
return num_pages;
}
}
My SlideFragment class:
public class SliderFragment extends Fragment {
Toolbar toolbar;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_item,container,false);
return rootView;
}
Now I want to have same fragment while scrolling the page but with diff texts how could I do that... And if possible use RecyclerView also.
You could create a new Class (UpdatingFragment) with public abstract updateView(); that extends Fragment.
Create your SliderFragment by extending UpdatingFragment instead of Fragment
In the SliderFragment in onCreateView() at the end call updateView().
Write an overwrite method in your SliderFragment for what you need to update (anything you want to show on the UI)
Adjust your screenAdapter to return and take UpdatingFragment’s instead of Fragments.
Now whenever your fragments onCreateView gets called all their UI data gets updated, and if you create a list of SliderFragments and pass that into your adapter instead of creating new ones, you can just run a for loop over that list and call updateView() for each fragment.

fragment doesn't show in framelayout in linearlayout

I use fragments to display multiple views in one activity. I have 2 framelayouts in one linearlayout. The onCreate and onCreateView of the fragment gets called but the view of the fragment is not displayed. Is what i'm trying to do not possible? Or is there a way to fix issue?
Layout of the activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".view.activity.StandardFlowActivity">
<FrameLayout
android:id="#+id/standardFlowDownloadContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="#+id/standardFlowBaseContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Layout of the fragment
<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"
tools:context="com.example.sennevervaecke.crossexperience.view.fragment.WedstrijdFragment">
<ListView
android:id="#+id/wedstrijdListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Fragment Class
public class WedstrijdFragment extends Fragment implements AdapterView.OnItemClickListener{
private ArrayList<Wedstrijd> wedstrijden;
private WedstrijdFragmentCom communication;
public WedstrijdFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
Log.e("wedstrijdFragment", "onCreate is called");
super.onCreate(savedInstanceState);
wedstrijden = LocalDB.getWedstrijden();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e("wedstrijdFragment", "onCreateView is called");
View view = inflater.inflate(R.layout.fragment_wedstrijd, container, false);
ListView listView = view.findViewById(R.id.wedstrijdListView);
WedstrijdAdapter adapter = new WedstrijdAdapter(getContext(), wedstrijden);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
communication = (WedstrijdFragmentCom) activity;
} catch (ClassCastException e){
e.printStackTrace();
}
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
communication.onWedstrijdItemClick(wedstrijden.get(i));
}
}
Code in the onCreate of the activity to add the fragment:
wedstrijdFragment = new WedstrijdFragment();
getSupportFragmentManager().beginTransaction().add(R.id.standardFlowBaseContainer, wedstrijdFragment, "wedstrijd").commit();
Thanks in advance!
Your first layout standardFlowDownloadContainer width and height is match_parent , so the standardFlowBaseContainer FrameLayout is out of the screen. you can change your standardFlowDownloadContainer's height to 20dp and run your project , you will see your Fragment content.

Android Fragment Always NULL When Accessing From ViewPagerAdapter

The Problem:
In my onCreate() on my main activity, I am trying to set the text value of a TextView in my fragment which is displayed by a viewpager. When I try to get my fragment instance, It always returns null.
Solutions I Tried:
Almost every solution related to this problem on SO gives me the same result(NPE).
My Code is below as well as some explanation...
Home.java Activity (My Main Activity):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the home layout
setContentView(R.layout.home);
// Setup the toolbar
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Now setup our tab bar
TabLayout tabLayout = (TabLayout)findViewById(R.id.homeTabs);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
// Setup the view pager
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
// Start our service controller
Intent startServiceController = new Intent(this, ServiceController.class);
startService(startServiceController);
Fragment viewFragment = pagerAdapter.getRegisteredFragment(viewPager.getCurrentItem());
if(viewFragment == null){
Log.v("Fragment", "NULL");
}
}
After setting up the pageAdapter and viewpager, I try to access the fragment thats being shown. The code for my PagerAdapter is below...
PagerAdapter:
public class PagerAdapter extends SmartFragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ParkListFragment();
case 1:
return new MapFragment();
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
CharSequence title;
switch (position){
case 0:
title = "Spaces Near You";
break;
case 1:
title = "Map";
break;
default:
title = "N/A";
break;
}
return title;
}
}
SmartFragmentStatePagerAdapter:
public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
// Sparse array to keep track of registered fragments in memory
private SparseArray<Fragment> registeredFragments = new SparseArray<>();
public SmartFragmentStatePagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Register the fragment when the item is instantiated
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
// Unregister when the item is inactive
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
// Returns the fragment for the position (if instantiated)
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
I really don't understand what the issue is. I have tried many things. What I don't want to use is this:
String tag="android:switcher:" + viewId + ":" + index;
ParkListFragment.java:
public class ParkListFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.park_list_fragment, container, false);
}
}
ParkList Fragment Layout:
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/confidenceText"
android:textAlignment="center"
android:textSize="24sp"
android:textColor="#android:color/primary_text_light"
android:padding="10dp"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/activityText"
android:textAlignment="center"
android:textSize="24sp"
android:textColor="#android:color/primary_text_light"
android:padding="10dp"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Stack Trace: Pastebin
So after hours of trial and error, the only thing that worked for me was this SO post:
ViewPager Fragments are not initiated in onCreate

Embedding a RecyclerView within a custom view

I am trying to embed a RecyclerView within the body of a custom view (a RefreshableList).
This is my project's structure: it contains 2 modules, app & rlist.
the rlist module holds the custom view (RefreshableList) in which I want to embed a RecyclerView.
RefreshableList.java (The custom view)
public class RefreshableList extends RelativeLayout {
private RecyclerView mRecyclerView;
private Context mContext;
private MyAdapter mAdapter;
public RefreshableList(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
Log.i("ADAPTER", "RefreshableList is initializing views...");
setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.refreshable_list, null);
findViewsById(view);
setupRecyclerView();
}
private void findViewsById(View view) {
mRecyclerView = (RecyclerView) view.findViewById(R.id.dataRecyclerView);
Log.i("ADAPTER", "RecyclerView has been initialized.");
}
private void setupRecyclerView() {
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
Log.i("ADAPTER", "RecyclerView has been setup.");
}
public void setAdapter(MyAdapter adapter) {
mAdapter = adapter;
mRecyclerView.setAdapter(mAdapter);
Log.i("ADAPTER", "Adapter has been set." + mAdapter.getItemCount());
}
}
MyAdapter.java (The RecyclerView's adapter)
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
protected Context mContext;
protected List<String> mItems;
public MyAdapter(Context context, List<String> items) {
mContext = context;
mItems = new ArrayList<>(items);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_row, parent, false);
Log.i("ADAPTER", "Creating row...");
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.label.setText(mItems.get(position));
}
#Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView label;
public ViewHolder(View itemView) {
super(itemView);
label = (TextView) itemView.findViewById(R.id.label);
}
}
}
item_row.xml: (the XML layout of the RecyclerView's elements)
<?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="60dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/label"
android:gravity="left|center_vertical"
android:padding="8dp"
android:background="#ff0000"/>
</LinearLayout>
refreshable_list.xml: (The XML layout of the RefreshableList custom view)
<?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/dataRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
All this code belongs to the rlist module. In order to test it out, I added a MainActivity to the app module where I embed a RefreshableList:
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Bind(R.id.list)
RefreshableList mRefreshableList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mRefreshableList.setAdapter(new MyAdapter(
this, Arrays.asList("Java", "Android", "Python", "Kivy")));
}
}
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.zouag.refreshablelist.MainActivity"
android:background="#00ff00">
<com.zouag.rlist.RefreshableList
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00"/>
</RelativeLayout>
After running the application, I can't see any of the RecyclerView's elements: (even though the RecyclerView is clearly visible, marked with yellow)
What am I missing here ?
Not your recyclerview is visible, your relativelayout is.
You don't attach the inflated layout to your viewgroup.
The call :
View view = inflater.inflate(R.layout.refreshable_list, null);
just inflates the layout but dosen't attach it to a view.
If u wanna stick with this you need to attach the view after inflating by:
this.addView(view)
Or just call:
View view = inflater.inflate(R.layout.refreshable_list, this,true);
which attaches the inflated layout to the root view.

Categories

Resources