当textview内容增加时滚动Scrollview

时间:2014-11-14 06:34:55

标签: android scrollview

我采用5相对布局进行展开和折叠动画。当我点击其中一个相对布局时,textview会被展开。对于最后的相对布局textview展开但是scrollview没有设置为底部向上到textview内容。我们必须滚动到看到textview content.i想要当我点击相对布局文本时会自动查看滚动浏览到底部请给我建议。
HelpActivity.java

RelativeLayout mLayout1 = (RelativeLayout) findViewById(R.id.rltRowOne);
    final TextView mLayoutToBeExpanded1 = (TextView) findViewById(R.id.helpContact);
    mLayoutToBeExpanded1.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
                @SuppressLint("NewApi")
                @Override
                public void onGlobalLayout()
                {
                    // gets called after layout has been done but before display
                    // so we can get the height then hide the view
                    h1 = mLayoutToBeExpanded1.getHeight();
                    mLayoutToBeExpanded1.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    mLayoutToBeExpanded1.setVisibility( View.GONE );
            }

        });



    RelativeLayout mLayout2 = (RelativeLayout) findViewById(R.id.rltRowTwo);
    final TextView mLayoutToBeExpanded2 = (TextView) findViewById(R.id.helpServiceContent);
    mLayoutToBeExpanded2.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
                @SuppressLint("NewApi")
                @Override
                public void onGlobalLayout()
                {
                    // gets called after layout has been done but before display
                    // so we can get the height then hide the view

                    h2 = mLayoutToBeExpanded2.getHeight();
                    mLayoutToBeExpanded2.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    mLayoutToBeExpanded2.setVisibility(View.GONE);
            }

        });



    RelativeLayout mLayout3 = (RelativeLayout) findViewById(R.id.rltRowThree);
    final TextView mLayoutToBeExpanded3 = (TextView) findViewById(R.id.helpShareContact);
    mLayoutToBeExpanded3.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
                @SuppressLint("NewApi")
                @Override
                public void onGlobalLayout()
                {
                    h3 = mLayoutToBeExpanded3.getHeight();
                    mLayoutToBeExpanded3.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    mLayoutToBeExpanded3.setVisibility( View.GONE );
                }

        });


    RelativeLayout mLayout4 = (RelativeLayout) findViewById(R.id.rltRowFour);
    final TextView mLayoutToBeExpanded4 = (TextView) findViewById(R.id.helpPrint);
    mLayoutToBeExpanded4.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
                @SuppressLint("NewApi")
                @Override
                public void onGlobalLayout()
                {
                    h4 = mLayoutToBeExpanded4.getHeight();
                    mLayoutToBeExpanded4.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    mLayoutToBeExpanded4.setVisibility( View.GONE );
                }

        });



    RelativeLayout mLayout5 = (RelativeLayout) findViewById(R.id.rltRowFive);
    final TextView mLayoutToBeExpanded5 = (TextView) findViewById(R.id.helpEmergency);
    mLayoutToBeExpanded5.getViewTreeObserver().addOnGlobalLayoutListener( 
            new OnGlobalLayoutListener(){

                @SuppressLint("NewApi")
                @Override
                public void onGlobalLayout()
                {
                    h5 = mLayoutToBeExpanded5.getHeight();
                    mLayoutToBeExpanded5.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    mLayoutToBeExpanded5.setVisibility( View.GONE );
                }
        });



    addAction(mLayout1, mLayoutToBeExpanded1, mLayoutToBeExpanded2,mLayoutToBeExpanded3,mLayoutToBeExpanded4,mLayoutToBeExpanded5);
    addAction(mLayout2, mLayoutToBeExpanded1, mLayoutToBeExpanded2,mLayoutToBeExpanded3,mLayoutToBeExpanded4,mLayoutToBeExpanded5);
    addAction(mLayout3, mLayoutToBeExpanded1, mLayoutToBeExpanded2,mLayoutToBeExpanded3,mLayoutToBeExpanded4,mLayoutToBeExpanded5);
    addAction(mLayout4, mLayoutToBeExpanded1, mLayoutToBeExpanded2,mLayoutToBeExpanded3,mLayoutToBeExpanded4,mLayoutToBeExpanded5);
    addAction(mLayout5, mLayoutToBeExpanded1, mLayoutToBeExpanded2,mLayoutToBeExpanded3,mLayoutToBeExpanded4,mLayoutToBeExpanded5);

}



public void addAction(final View layout, final View summary1,final View summary2,final View summary3,final View summary4,final View summary5)
{
    layout.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            int lay_clicked=layout.getId();
            if(lay_clicked==R.id.rltRowOne)
            {

                if (summary1.getVisibility() == View.GONE)
                {
                    expand(summary1,h1);
                }
                else
                collapse(summary1);

                collapse(summary2);
                collapse(summary3); 
                collapse(summary4);
                collapse(summary5);
            }

            else if(lay_clicked==R.id.rltRowTwo)
            {
                if (summary2.getVisibility() == View.GONE)
                {
                    expand(summary2,h2);
                }
                else
                collapse(summary2);
                collapse(summary1);
                collapse(summary3);
                collapse(summary4);
                collapse(summary5);
            }

            else if(lay_clicked==R.id.rltRowThree)
            {
                if (summary3.getVisibility() == View.GONE)
                {
                    expand(summary3,h3);
                }
                else
                collapse(summary3);
                collapse(summary1);
                collapse(summary2);
                collapse(summary4);
                collapse(summary5);
            }
            else if(lay_clicked==R.id.rltRowFour)
            {
                if (summary4.getVisibility() == View.GONE)
                {
                    expand(summary4,h4);
                }
                else
                collapse(summary4);
                collapse(summary1);
                collapse(summary3);
                collapse(summary2);
                collapse(summary5);
            }
            else if(lay_clicked==R.id.rltRowFive)
            {
                if (summary5.getVisibility() == View.GONE)
                {
                    expand(summary5,h5);
                    final ScrollView Scrbar=(ScrollView)findViewById(R.id.ScrollContainer);
                    Scrbar.post(new Runnable() {
                        @Override
                        public void run(){
                            Scrbar.fullScroll(ScrollView.FOCUS_DOWN);
                        }
                    });

                }
                else
                collapse(summary5);
                collapse(summary1);
                collapse(summary3);
                collapse(summary4);
                collapse(summary2);
            }
        }
    });


}

private void expand(View summary,int height) {
    // set Visible
    summary.setVisibility(View.VISIBLE);

    final int widthSpec = View.MeasureSpec.makeMeasureSpec(1,
            View.MeasureSpec.UNSPECIFIED);
    summary.measure(widthSpec, 300);
    ValueAnimator mAnimator;
    mAnimator = slideAnimator(0, height, summary);

    mAnimator.start();
}

private void collapse(final View summary) {
    int finalHeight = summary.getHeight();

    ValueAnimator mAnimator = slideAnimator(finalHeight, 0, summary);

    mAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationEnd(Animator animator) {
            // Height=0, but it set visibility to GONE
            summary.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationStart(Animator animator) {
        }

        @Override
        public void onAnimationCancel(Animator animator) {
        }

        @Override
        public void onAnimationRepeat(Animator animator) {
        }
    });
    mAnimator.start();
}

private ValueAnimator slideAnimator(int start, int end, final View summary) {

    ValueAnimator animator = ValueAnimator.ofInt(start, end);

    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            // Update Height
            int value = (Integer) valueAnimator.getAnimatedValue();

            ViewGroup.LayoutParams layoutParams = summary.getLayoutParams();
            layoutParams.height = value;
            summary.setLayoutParams(layoutParams);
        }
    });
    return animator;
}

helpactivity.xml

<ScrollView
    android:id="@+id/ScrollContainer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lnrAction"
    android:scrollbarThumbVertical="@drawable/colorscroll" >


    <RelativeLayout
        android:id="@+id/titleContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lnrAction"
        android:paddingLeft="20dp"
        android:paddingRight="20dp" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="About Contact Diary"
            android:textColor="#0260C5"
            android:textSize="23sp" />

        <View
            android:id="@+id/underlineOne"
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:layout_below="@+id/title"
            android:layout_marginTop="5dp"
            android:background="@color/gray_1" />

        <TextView
            android:id="@+id/helpContent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/underlineOne"
            android:layout_marginTop="10dp"
            android:text="@string/abtcon"
            android:textSize="16dp" />


        <RelativeLayout
            android:id="@+id/rltRowOne"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/helpContent"
            android:layout_marginTop="20dp" >

            <View
                android:id="@+id/underlineTwo"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_marginTop="5dp"
                android:background="@color/gray_1" />

            <ImageView
                android:id="@+id/contactImg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/underlineTwo"
                android:layout_marginTop="10dp"
                android:src="@drawable/contact_home_screen" />

            <TextView
                android:id="@+id/contactTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="22dp"
                android:layout_toRightOf="@+id/contactImg"
                android:text="Contact"
                android:textColor="#0260C5"
                android:textSize="22dp" />

            <View
                android:id="@+id/underlineFour"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_below="@+id/contactImg"
                android:layout_marginTop="5dp"
                android:background="@color/gray_1"
                android:visibility="gone" />

            <TextView
                android:id="@+id/helpContact"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/underlineFour"
                android:layout_marginTop="10dp"
                android:text="@string/contact"
                android:textSize="16dp"
                android:visibility="visible" />
        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/rltRowTwo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rltRowOne" >

            <View
                android:id="@+id/underline1"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_marginTop="5dp"
                android:background="@color/gray_1" />

            <ImageView
                android:id="@+id/contactServiceImg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:src="@drawable/service_contact_home_screen" />

            <TextView
                android:id="@+id/serviceContactTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:layout_toRightOf="@+id/contactServiceImg"
                android:text="Service Contacts"
                android:textColor="#0260C5"
                android:textSize="22dp" />

            <View
                android:id="@+id/underlineThree"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_below="@+id/contactServiceImg"
                android:layout_marginTop="10dp"
                android:background="@color/gray_1"
                android:visibility="gone" />

            <TextView
                android:id="@+id/helpServiceContent"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/underlineThree"
                android:layout_marginTop="10dp"
                android:text="@string/service"
                android:textSize="16dp"
                android:visibility="visible" />
        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/rltRowThree"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rltRowTwo" >

            <View
                android:id="@+id/underline2"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_marginTop="5dp"
                android:background="@color/gray_1" />

            <ImageView
                android:id="@+id/contactServiceImgw"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:src="@drawable/share_home_screen" />

            <TextView
                android:id="@+id/shareContactTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:layout_toRightOf="@+id/contactServiceImgw"
                android:text="Share"
                android:textColor="#0260C5"
                android:textSize="22dp" />

            <View
                android:id="@+id/underlineFive"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_below="@+id/contactServiceImgw"
                android:layout_marginTop="10dp"
                android:background="@color/gray_1"
                android:visibility="gone" />

            <TextView
                android:id="@+id/helpShareContact"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/underlineFive"
                android:layout_marginTop="10dp"
                android:text="@string/share"
                android:textSize="16dp"
                android:visibility="visible" />
        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/rltRowFour"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rltRowThree" >

            <View
                android:id="@+id/underline3"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_marginTop="5dp"
                android:background="@color/gray_1" />

            <ImageView
                android:id="@+id/contactPrint"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:src="@drawable/print_home_screen" />

            <TextView
                android:id="@+id/PrintTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:layout_toRightOf="@+id/contactPrint"
                android:text="Print"
                android:textColor="#0260C5"
                android:textSize="22dp" />

            <View
                android:id="@+id/underlineSix"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_below="@+id/contactPrint"
                android:layout_marginTop="10dp"
                android:background="@color/gray_1"
                android:visibility="gone" />

            <TextView
                android:id="@+id/helpPrint"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/underlineSix"
                android:layout_marginTop="10dp"
                android:text="@string/print"
                android:textSize="16dp"
                android:visibility="visible" />
        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/rltRowFive"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rltRowFour" >

            <View
                android:id="@+id/underline4"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_marginTop="5dp"
                android:background="@color/gray_1" />

            <ImageView
                android:id="@+id/contactEmergency"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:src="@drawable/emergency_home_screen_red" />

            <TextView
                android:id="@+id/PrintEmergency"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:layout_toRightOf="@+id/contactEmergency"
                android:text="Emergency"
                android:textColor="#0260C5"
                android:textSize="22dp" />

            <View
                android:id="@+id/underlineSeven"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_below="@+id/contactEmergency"
                android:layout_marginTop="10dp"
                android:background="@color/gray_1"
                android:visibility="gone" />

            <TextView
                android:id="@+id/helpEmergency"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/underlineSeven"
                android:layout_marginTop="10dp"
                android:text="@string/emergency"
                android:textSize="16dp"
                android:visibility="visible" />
        </RelativeLayout>
    </RelativeLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

在你的textView(或他的相对布局) onClickListner()(或在 onAnimationEnd()中),你可以调用此方法将scrollView滚动到底部textView:

private final void scrollMyView(){
      yourScrollView.scrollTo(0, yourTextView.getBottom());
}