在JazzyViewPager中调用其它layout布局xml并使用

开源地址:https://github.com/jfeinstein10/JazzyViewPager

发现网上的例子使用的是直接创建的一个TextView来做的。但是实际上使用,不可能只有这一个控件。

下面是我的实现方式,如果大家有更好的方法,麻烦告之。不胜感激~~~

在自定义的

MainAdapter extends PagerAdapter
  + public Object instantiateItem(ViewGroup container, final int position) { 方法中的部分代码实现如下:

 View currView = null;
 LayoutInflater factory = LayoutInflater.from(context);
currView = factory.inflate(getLayoutId(), null);

//获取控件内容并设定click侦听
final RelativeLayout btnDebug = (RelativeLayout) currView.findViewById(R.id.btnDebug);
        Log.d("MainAdapter", "Button is null ?" + (btnDebug == null));
        if (btnDebug != null) btnDebug.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("DEBUG", "CLICK ME.");                
            }
        });

container.addView(currView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
jazzyPager.setObjectForPosition(currView, position);
return currView;

即,要通过代码的方式来实现载入layout中的布局XML,不知有没有其它的办法。

原文地址:https://www.cnblogs.com/atwind/p/4521932.html