充气寻呼机适配器内部的自定义类 [英] Inflate custom class inside Pager Adapter
问题描述
我工作的一个项目,我需要实现无限滚动寻呼机适配器。虽然我已经找到有用的链接来实现类似CalendarView双向无限滚动视图寻呼机(在功能方面相似),我坚持一个小问题。请各位看看code在这里。
有没有一种方法,我可以代替我这个适配器内部(扩大PagerAdapter)
@覆盖
公共对象instantiateItem(ViewGroup中的容器,INT位置){
LayoutInflater吹气=(LayoutInflater)container.getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
查看MPAGE = inflater.inflate(R.layout.page_layout,NULL);
container.addView(MPAGE);
返回MPAGE;
}
有了这个
@覆盖
公共对象instantiateItem(ViewGroup中的容器,INT位置){ 页面布局页=新的页面布局(背景下,整数i,字符串str);
container.addView(页);
返回页面; }
其中,
公共类页面布局扩展的LinearLayout { 整数i;
字符串str; 公共页面布局(上下文的背景下,整数i,字符串str){
超级(上下文);
视图V =膨胀(背景下,R.layout.page,NULL);
this.i = I;
this.str =海峡;
//查找TextViews等,并设置。
//执行的AsyncTask和其他一些很酷的东西 }
}
所以我在这里需要,是吹在视图类中的方法延伸出的LinearLayout ViewGroup中的观点的一种方式。我需要做的,因为我想通过一个构造函数有几个成员变量初始化自定义视图,并使用相同的构造函数的成员变量膨胀布局。当我尝试这样做code和调试的code,页面视图成员为空。有没有办法做到这一点这样?我缺少的东西吗?感谢您的帮助。
修改
在自定义视图页面布局有一个构造函数这需要在几个参数。页面布局需要这些变量做执行的AsyncTask。
其基本思想是建立在您使用自定义视图一个XML布局。
row.xml可能看起来像:
< my.awesome.package.name.PageLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
<! - 只是把更多有用的意见在这里 - >
< /my.awesome.package.name.PageLayout>
您就可以轻松地使用它在你的PageAdapter是这样的:
@覆盖
公共对象instantiateItem(ViewGroup中的容器,INT位置){
LayoutInflater充气= LayoutInflater.from(container.getContext());
页面布局MPAGE =(页面布局)inflater.inflate(R.layout.row,集装箱);
mPage.init(参数1,123); //通过一些数据来设置布局
//一些东西,与其他意见
//还可以阅读有关ViewHolder模式,如果你不知道它是什么
返回MPAGE;
}
错误消息类型的布局预计资源
并不意味着:,因为ID尚未设定
。问题是,的getId()
A布局为您提供了 R.id。*
样的ID,而不是那种 R.layout。*
。检查生成的R舱看到布局
和 ID
是不同的阶级和膨胀()
要求 R.layout。*
的ID。
更新:
您可以页面布局看起来像:
公共类页面布局扩展的RelativeLayout {
公共页面布局(上下文的背景下){
超级(上下文);
} 公共页面布局(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
} 公共页面布局(上下文的背景下,ATTRS的AttributeSet,诠释defStyleAttr){
超(背景下,ATTRS,defStyleAttr);
} 公共无效的init(字符串参数1,INT参数2){
//这里你可以调用findViewById(R.id.some_internal_view_ids)
//并要与参数什么都
}
}
使用您可以访问每查看您已经在里面加了你的页面布局
在 row.xml
。有关详细信息,我建议你阅读创建自定义视图 商务部p>
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
这篇关于充气寻呼机适配器内部的自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!