I am working on a project where I need to implement an infinite scrolling pager adapter. While I have found useful links to implement a two way infinite scrolling view pager similar to the CalendarView( similar in terms of functionality), I am stuck with a small problem. Please have a look the code here.
Is there a way where I can replace this inside my adapter (extending PagerAdapter)
@Override
public Object instantiateItem (ViewGroup container, int position){
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mPage = inflater.inflate(R.layout.page_layout,null);
container.addView(mPage );
return mPage ;
}
With this
@Override
public Object instantiateItem (ViewGroup container, int position){
PageLayout page= new PageLayout(context, Integer i, String str);
container.addView(page);
return page;
}
Where
public class PageLayout extends LinearLayout {
Integer i;
String str;
public PageLayout (Context context, Integer i, String str) {
super(context);
View v = inflate(context,R.layout.page,null);
this.i =i;
this.str =str;
//Find TextViews etc and set them.
//Perform an asynctask and some other cool stuff
}
}
So what I need here, is a way to inflate the view extending a LinearLayout ViewGroup in a method within the view class. I need to do this because I want to initialize the Custom View with a few member variables through a constructor and use the same constructor to inflate the layout with the member variables. When I tried this code and debugged the code, the view member of the page is null. Is there a way to do it this way? Am I missing something? Thanks for your help.
Edit
The custom view PageLayout has a constructor which takes in a few parameters. PageLayout needs those variables to do perform an asynctask.
The basic idea is to create a xml layout where you use your custom view.
row.xml could look like that:
<my.awesome.package.name.PageLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- just put more useful views here -->
</my.awesome.package.name.PageLayout>
You can then easily use it in your PageAdapter like this:
@Override
public Object instantiateItem (ViewGroup container, int position){
LayoutInflater inflater = LayoutInflater.from(container.getContext());
PageLayout mPage = (PageLayout) inflater.inflate(R.layout.row, container);
mPage.init("param1", 123); // pass some data to set up the layout
// to some stuff with other views
// Also read about ViewHolder pattern if you do not know what it is
return mPage;
}
The error message Expected resource of type layout
does not mean: because the ID is not set yet
. The problem is that getId()
of a layout gives you the R.id.*
kind of ID and not the kind of R.layout.*
. Check the generated R class to see that layout
and id
are different classes and inflate()
requires R.layout.*
ids.
Update:
Your PageLayout can look like that:
public class PageLayout extends RelativeLayout {
public PageLayout(Context context) {
super(context);
}
public PageLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PageLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void init(String param1, int param2) {
// here you can call findViewById(R.id.some_internal_view_ids)
// and to what ever you want with the parameter
}
}
With that you can access every view you have added inside your PageLayout
in your row.xml
. For more details I suggest you read the doc about Creating Custom Views