在寻呼机适配器内膨胀自定义类

时间:2014-10-29 12:56:44

标签: android android-pageradapter

我正在开发一个需要实现无限滚动寻呼机适配器的项目。虽然我找到了有用的链接来实现类似于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

       }
    }

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

修改

自定义视图PageLayout有一个构造函数,它接受一些参数。 PageLayout需要这些变量来执行asynctask。

2 个答案:

答案 0 :(得分:4)

基本思想是创建一个使用自定义视图的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>

然后您可以在PageAdapter中轻松使用它,如下所示:

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

错误消息Expected resource of type layout并不意味着:because the ID is not set yet。问题是布局的getId()会为您提供R.id.*种ID,而不是R.layout.*种。检查生成的R类,看layoutid是不同的类,inflate()需要R.layout.* ID。

更新

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

通过它,您可以访问PageLayout row.xml中添加的每个视图。有关详细信息,建议您阅读有关Creating Custom Views

的文档

答案 1 :(得分:-1)

试试这个:

@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布局文件。

相关问题