Android 在寻呼机适配器内为自定义类充气

Android 在寻呼机适配器内为自定义类充气,android,android-pageradapter,Android,Android Pageradapter,我正在做一个项目,我需要实现一个无限滚动寻呼机适配器。虽然我找到了一些有用的链接来实现类似于CalendarView的双向无限滚动视图寻呼机(功能类似),但我遇到了一个小问题。请看一下这里的代码 有没有办法在我的适配器(扩展PagerAdapter)内更换此设备 @Override public Object instantiateItem (ViewGroup container, int position){ LayoutInflater in

我正在做一个项目,我需要实现一个无限滚动寻呼机适配器。虽然我找到了一些有用的链接来实现类似于CalendarView的双向无限滚动视图寻呼机(功能类似),但我遇到了一个小问题。请看一下这里的代码

有没有办法在我的适配器(扩展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 ;
            }
用这个

    @Override
    public Object instantiateItem (ViewGroup container, int position){

       PageLayout page= new PageLayout(context, Integer i, String str);
        container.addView(page);
        return page;

    }
其中

    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

       }
    }
因此,我在这里需要的是一种在view类中的方法中扩展LinearLayout ViewGroup的方法来扩展视图。我需要这样做,因为我想通过一个构造函数用几个成员变量初始化自定义视图,并使用同一个构造函数用成员变量膨胀布局。当我尝试此代码并调试代码时,页面的视图成员为null。有没有办法这样做?我错过什么了吗?谢谢你的帮助

编辑

自定义视图页面布局有一个构造函数,它接受一些参数。PageLayout需要这些变量来执行异步任务

试试这个:

@Override
public Object instantiateItem (ViewGroup container, int position){

    PageLayout page= new PageLayout(context);
    LayoutInflater inflater = (LayoutInflater)container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View mPage = inflater.inflate(page.getId(),null);
    return mPage;

}
这个想法是膨胀你的客户的LinearLayout Id。我还没有尝试过这个代码,请告诉我结果

编辑

我认为发生错误是因为ID尚未设置,我真的不知道如何修复它

我知道(因为我已经这样做了)将在XML中使用自定义布局(与通常一样),如下所示:

<com.your.package.PageLayout xmlns:android="http://schemas.android.com/apk/res/android" bla bla />

然后像往常一样使用
R.layout
对XML布局文件进行充气。

尝试以下操作:

@Override
public Object instantiateItem (ViewGroup container, int position){

    PageLayout page= new PageLayout(context);
    LayoutInflater inflater = (LayoutInflater)container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View mPage = inflater.inflate(page.getId(),null);
    return mPage;

}
这个想法是膨胀你的客户的LinearLayout Id。我还没有尝试过这个代码,请告诉我结果

编辑

我认为发生错误是因为ID尚未设置,我真的不知道如何修复它

我知道(因为我已经这样做了)将在XML中使用自定义布局(与通常一样),如下所示:

<com.your.package.PageLayout xmlns:android="http://schemas.android.com/apk/res/android" bla bla />

然后像往常一样使用
R.layout
对XML布局文件进行充气。

尝试以下操作:

@Override
public Object instantiateItem (ViewGroup container, int position){

    PageLayout page= new PageLayout(context);
    LayoutInflater inflater = (LayoutInflater)container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View mPage = inflater.inflate(page.getId(),null);
    return mPage;

}
这个想法是膨胀你的客户的LinearLayout Id。我还没有尝试过这个代码,请告诉我结果

编辑

我认为发生错误是因为ID尚未设置,我真的不知道如何修复它

我知道(因为我已经这样做了)将在XML中使用自定义布局(与通常一样),如下所示:

<com.your.package.PageLayout xmlns:android="http://schemas.android.com/apk/res/android" bla bla />

然后像往常一样使用
R.layout
对XML布局文件进行充气。

尝试以下操作:

@Override
public Object instantiateItem (ViewGroup container, int position){

    PageLayout page= new PageLayout(context);
    LayoutInflater inflater = (LayoutInflater)container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View mPage = inflater.inflate(page.getId(),null);
    return mPage;

}
这个想法是膨胀你的客户的LinearLayout Id。我还没有尝试过这个代码,请告诉我结果

编辑

我认为发生错误是因为ID尚未设置,我真的不知道如何修复它

我知道(因为我已经这样做了)将在XML中使用自定义布局(与通常一样),如下所示:

<com.your.package.PageLayout xmlns:android="http://schemas.android.com/apk/res/android" bla bla />


并像往常一样使用
R.layout
对XML布局文件进行充气。

基本思想是创建一个使用自定义视图的XML布局

row.xml可能如下所示:

<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>
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
    }
}
错误消息
类型布局的预期资源
并不意味着:
,因为尚未设置ID
。问题是布局的
getId()
为您提供了
R.id.
类型的id,而不是
R.layout.*
。检查生成的R类以查看
layout
id
是不同的类,并且
inflate()
需要
R.layout.
id

更新:

您的页面布局可以如下所示:

<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>
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
    }
}

这样,您就可以访问您在
行.xml中的
页面布局中添加的每个视图。有关更多详细信息,我建议您阅读有关

的文档。基本思想是创建一个使用自定义视图的xml布局

row.xml可能如下所示:

<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>
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
    }
}
错误消息
类型布局的预期资源
并不意味着:
,因为尚未设置ID
。问题是布局的
getId()
为您提供了
R.id.
类型的id,而不是
R.layout.*
。检查生成的R类以查看
layout
id
是不同的类,并且
inflate()
需要
R.layout.
id

更新:

您的页面布局可以如下所示:

<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>
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
    }
}

这样,您就可以访问您在
行.xml中的
页面布局中添加的每个视图。有关更多详细信息,我建议您阅读有关

的文档。基本思想是创建一个使用自定义视图的xml布局

row.xml可能如下所示:

<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>
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
    }
}
错误消息
类型布局的预期资源
并不意味着:
,因为尚未设置ID
。问题是布局的
getId()
为您提供了
R.id.
类型的id,而不是
R.layout.*
。检查生成的R类以查看
layout
id
是不同的类,并且
inflate()
需要
R.layout.
id

更新:

您的页面布局可以如下所示:

<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>
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
    }
}

这样,您就可以访问您在
行.xml中的
页面布局中添加的每个视图。有关更多详细信息,我建议您阅读有关

的文档。基本思想是创建一个使用自定义视图的xml布局

row.xml可能如下所示:

<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>
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
    }
}
错误消息
类型布局的预期资源
并不意味着:
,因为尚未设置ID
。问题是布局的
getId()
为您提供了
R.id.
类型的id,而不是
R.layout.*
。检查生成的R类以查看
layout
id
是不同的类,并且
inflate()
需要
R.layout.
id

更新:

您的页面布局可以如下所示:<