词Ci 介绍页 StackView的使用

词Ci的介绍页是这样的,可以上下滑动翻页。


这样效果自己要实现还是很麻烦的,后来发现已经有了Stack'View就是这样效果了,要做的就是使用啦。

public class IntroductionView extends RelativeLayout
{
    private Context mContext;
    final int[] mColors = {R.drawable.intro00, R.drawable.intro01, R.drawable.intro02, R.drawable.intro03,
            R.drawable.intro04, R.drawable.intro05};
    public IntroductionView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }
    public IntroductionView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
    public IntroductionView(Context context)
    {
        super(context);
        mContext = context;
        initView();
    }
    private void layoutItemContainer(View itemContainer)
    {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) itemContainer.getLayoutParams();
        params.width = LayoutParams.MATCH_PARENT;
        params.height = ResizeUtil.resize(mContext, 864);
        itemContainer.setLayoutParams(params);
    }

    private void initView()
    {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View root = inflater.inflate(R.layout.layout_intro, null);
        TextView title = (TextView) root.findViewById(R.id.title);
        title.setTypeface(MyApplication.getTypeface());
        final StackView stackView = (StackView) root.findViewById(R.id.stack_view);
        layoutItemContainer(stackView);
        IntroAdapter colorAdapter = new IntroAdapter(mContext, mColors);
        stackView.setAdapter(colorAdapter);
        stackView.getLayoutParams().height = ResizeUtil.resize(mContext, 600);
        // stackView.setLayoutParams(new LayoutParams(-1, ));
        addView(root, new LayoutParams(-1, -1));
    }

}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="词Ci介绍"
        android:textColor="@color/white"
        android:layout_marginTop="100dp"
        android:layout_centerHorizontal="true"
        android:textSize="28sp" />

    <StackView
        android:id="@+id/stack_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="60dp"
        android:loopViews="true" />

</RelativeLayout>

StackView 也是继承了AdapterView<Adapter>的,adpater的写法也没什么区别。

这里有个要注意的地方是自定义View的LayoutParams设置问题,

<pre name="code" class="java">// stackView.setLayoutParams(new LayoutParams(-1, ));
        addView(root, new LayoutParams(-1, -1));

 

上面一句是没必要的,addView时注意要加上LayoutParams,-1就是MatchParent,这样写着快点,但应该还是要写名称好。

 public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        if (convertView == null)
        {
            holder = new ViewHolder();
            LinearLayout.LayoutParams colorLayoutParams = new LinearLayout.LayoutParams(
                    ResizeUtil.resize(mContext, 450), ResizeUtil.resize(mContext, 540));

            convertView = new LinearLayout(mContext);
            holder.imageview = new ImageView(mContext);

            holder.imageview.setScaleType(ScaleType.FIT_XY);
            holder.imageview.setLayoutParams(colorLayoutParams);
            ((LinearLayout) convertView).addView(holder.imageview);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.imageview.setImageResource(mColors[position]);

        return convertView;
    }
Adapter就贴个getView了,其它也一样。这里设置每一项的宽度为450,高度为540,整个StackView宽度是MatchParent,高度是600,宽度MatchParent是viewPage一个Page的宽度,就是540,这样每一项宽高和整体的宽高就有差值了,然后整体显示就有种叠加的书页的效果了。(这里的宽高数值都是把界面宽度当做720的比值,上一篇讲到过的)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值