Use DialogFragment implement nested ViewPager, according to the highest height of the layout, adapting the layout of the list

product demand:

A dialog There is a button click Show details, and the details if the show inside the pop, the effect of product needs is to have a right to the same page jump animation, this time to complete the thought in the dialog nested viewpager

Problems encountered:

  • Options
    nested dialog in viewpager there will be an error exception, the Internet has a lot to say, the solution is given to using dialogfragment instead of nested dialog viewpager, due to their own project fragment using a lot, so I direct this selection sets of programs.
  • Height matching problem
    due viewpager inside layout is highly wrap_content no action will be highly dissatisfied or hold a blank area of a lot of cases, this time need a custom control replication viewpager calculated subspace height, taking the maximum height setting viewapager height, onMeasure replication method, as follows
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int height = 0;
    for(int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = child.getMeasuredHeight();
        if(h > height) height = h;
    }

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
  • New problems
    thought this can end, but with the addition of a list of details of the interface is that there is a problem highly covered screen, a new problem arises, the list of interface height measured is inactive high, as long as no settings specified height is the height of all would give to the list, so onMeasure measured by the appeal of rewriting the height of the largest child support is the height of the screen. Careful research needs, according to our interface, display area and the details of the list is the current interface display area is the same height, the height of the first pager method of measuring transformation onMeasure as highly viewpager, the code is as follows

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

    var height = 0
    //下面遍历所有child的高度
    for (i in 0 until childCount) {
        if (i == 0) {
            val child = getChildAt(i)
            child.measure(widthMeasureSpec,
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
            val h = child.measuredHeight
            if (h > height)
            //采用最大的view的高度。
                height = h
        }
    }

    var heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)

    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
  • Additional demand
    the removal of the sliding effect viewpager only jump in when clicked, right-click the slide animation to achieve, just add in just a custom viewpager of control:

fun setNoScroll(noScroll: Boolean) {

    this.noScroll = noScroll
}

override fun onTouchEvent(arg0: MotionEvent): Boolean {
    return if (noScroll)
        false
    else
        super.onTouchEvent(arg0)
}

override fun onInterceptTouchEvent(arg0: MotionEvent): Boolean {
    return if (noScroll)
        false
    else
        super.onInterceptTouchEvent(arg0)
}
  • Margins question
    directly last met a very strange question, set margins viewpager in the internal pager with layout viewpager, set margins and may conflict with the layout of the padding inside the pager, only this record.

Reproduced in: https: //www.jianshu.com/p/96e370aa83c2

Guess you like

Origin blog.csdn.net/weixin_34015566/article/details/91256902