2017-09-28 19 views
2

이 RelativeLayout은 확장 버튼을 클릭하면 축소됩니다. 버튼 하나로 잘 작동합니다. 동일한 레이아웃에서 더 많은 두 개의 RelativeLayout 에 동일한 메소드를 재사용하고 다른 두 개의 버튼을 사용하여 확장하고 싶습니다.확장 및 축소 단추로 클릭하여 relativelayout을 클릭하십시오.

이 코드는 정상적으로 실행되고 있습니다. 동일한 작업을 수행하기 위해 더 많은 레이아웃을 원할뿐입니다.

레이아웃 : 이것은 내 코드입니다 enter image description here

:

소스 코드
<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scrollbars="none"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <RelativeLayout 

      android:layout_width="fill_parent" 
      android:layout_height="64dp" 
      android:background="#FFF" 
      android:orientation="vertical"> 

      <TextView 

       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Title" 
       android:textSize="20sp" /> 

      <Button 
       android:id="@+id/viewmore" 
       android:layout_width="80dp" 
       android:layout_height="match_parent" 
       android:layout_marginLeft="280dp" 
       android:background="@null" 
       android:text="viewmore" /> 


     </RelativeLayout> 


     <RelativeLayout 

      android:visibility="gone" 
      android:id="@+id/expandable" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:animateLayoutChanges="true" 
      android:background="@color/colorAccent" 
      android:orientation="vertical"> 


      <TextView 

       android:layout_width="match_parent" 
       android:layout_height="133dp" 
       android:text="Text messaging, or texting, is the act of composing and sending electronic messages, typically consisting of alphabetic and numeric characters" 
       android:textSize="20sp" /> 


     </RelativeLayout> 


     <RelativeLayout 


      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <TextView 
       android:id="@+id/textView4" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Title 2" 
       android:textSize="20sp" /> 

      <Button 
       android:id="@+id/viewmore1" 
       android:layout_width="80dp" 
       android:layout_height="match_parent" 
       android:layout_marginLeft="280dp" 
       android:background="@null" 
       android:text="viewmore" /> 


     </RelativeLayout> 

     <RelativeLayout 
      android:visibility="gone" 
      android:animateLayoutChanges="true" 
      android:id="@+id/expandable1" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:layout_marginTop="30dp" 
      android:background="@color/colorPrimary"> 

      <TextView 

       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Text messaging, or texting, is the act of composing and sending electronic messages, typically consisting of alphabetic and numeric characters" 
       android:textSize="20sp" /> 


     </RelativeLayout> 

     <RelativeLayout 

      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <TextView 

       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Title 3" 
       android:textSize="20sp" /> 

      <Button 
       android:id="@+id/viewmore2" 
       android:layout_width="80dp" 
       android:layout_height="match_parent" 
       android:layout_marginLeft="280dp" 
       android:background="@null" 
       android:text="viewmore" /> 


     </RelativeLayout> 

     <RelativeLayout 
      android:visibility="gone" 
      android:animateLayoutChanges="true" 
      android:id="@+id/expandable2" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:layout_marginTop="30dp" 
      android:background="@color/colorPrimary"> 

      <TextView 

       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Text messaging, or texting, is the act of composing and sending electronic messages, typically consisting of alphabetic and numeric characters" 
       android:textSize="20sp" /> 


     </RelativeLayout> 


    </LinearLayout> 
</ScrollView> 

: 당신의 접근 방식을 계속하려면

RelativeLayout relativeLayout, relativeLayout1, relativeLayout2; 
    Button viewmore, viewmore1, viewmore2; 
    ValueAnimator mAnimator; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.viewmore); 


     relativeLayout = (RelativeLayout) findViewById(R.id.expandable); 
     relativeLayout1 = (RelativeLayout) findViewById(R.id.expandable1); 
     relativeLayout2 = (RelativeLayout) findViewById(R.id.expandable2); 


     viewmore = (Button) findViewById(R.id.viewmore); 
     viewmore1 = (Button) findViewById(R.id.viewmore1); 
     viewmore2 = (Button) findViewById(R.id.viewmore2); 

     viewmore.setOnClickListener(this); 
     viewmore1.setOnClickListener(this); 
     viewmore2.setOnClickListener(this); 


     relativeLayout.getViewTreeObserver().addOnPreDrawListener(
       new ViewTreeObserver.OnPreDrawListener() { 

        @Override 
        public boolean onPreDraw() { 
         relativeLayout.getViewTreeObserver().removeOnPreDrawListener(this); 
         relativeLayout.setVisibility(View.GONE); 

         final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 
         final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 
         relativeLayout.measure(widthSpec, heightSpec); 

         mAnimator = slideAnimator(0, relativeLayout.getMeasuredHeight()); 
         return true; 
        } 
       }); 


    } 


    private void expand() { 

     relativeLayout.setVisibility(View.VISIBLE); 
     mAnimator.start(); 
    } 

    private void collapse() { 
     int finalHeight = relativeLayout.getHeight(); 

     ValueAnimator mAnimator = slideAnimator(finalHeight, 0); 

     mAnimator.addListener(new Animator.AnimatorListener() { 
      @Override 
      public void onAnimationEnd(Animator animator) { 
       //Height=0, but it set visibility to GONE 
       relativeLayout.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) { 

     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 = relativeLayout.getLayoutParams(); 
       layoutParams.height = value; 
       relativeLayout.setLayoutParams(layoutParams); 
      } 
     }); 
     return animator; 
    } 


    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.viewmore: 

       if (relativeLayout.getVisibility() == View.GONE) { 
        expand(); 
       } else { 
        collapse(); 
       } 

       break; 


      case R.id.viewmore1: 


       break; 

      case R.id.viewmore2: 


       break; 


     } 
    } 

답변

2

, 당신은 코드를해야 할 것이다 모두에 적용 너가 계획 한 3 개의 단면도. 이렇게하려면 RelativeLayout을 인수로 사용하도록 여러 가지 방법을 변경해야합니다.

먼저 수신자 onClick 수신기에서 사례 블록을 작성하여 각 블록이 RelativeLayout 및 최대 높이로 타겟팅 된 expand()을 호출하도록합니다. RelativeLayout으로 collapse()으로 전화하십시오.

다음 코드에서 애니메이터가 생성 된 방법과 위치가 변경되었음을 알 수 있습니다. 애니메이터는 각 RelativeLayout으로 작업해야합니다.

따라서 onClick()은 을 호출하며 slideAnimator()을 호출합니다. 각 호출에 대해 영향을받은 RelativeLayout이 인수로 전달됩니다. 이 방법으로 하나 이상의 RelativeLayout에서 작동하도록 코드를 일반화 할 수 있습니다.

미리 그리기 수신기는 각각 확장 가능한 RelativeLayout을 측정해야합니다. 여기

은 모두 함께 넣어이다 : 당신은 또한 안드로이드 상대적 레이아웃을 확장하는 확장 사용자 정의를 만들 수 있습니다

MainActivity.xml

public class MainActivity extends AppCompatActivity 
    implements View.OnClickListener { 

    RelativeLayout relativeLayout, relativeLayout1, relativeLayout2; 
    Button viewmore, viewmore1, viewmore2; 
    int height, height1, height2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.viewmore); 

     relativeLayout = (RelativeLayout) findViewById(R.id.expandable); 
     relativeLayout1 = (RelativeLayout) findViewById(R.id.expandable1); 
     relativeLayout2 = (RelativeLayout) findViewById(R.id.expandable2); 

     viewmore = (Button) findViewById(R.id.viewmore); 
     viewmore1 = (Button) findViewById(R.id.viewmore1); 
     viewmore2 = (Button) findViewById(R.id.viewmore2); 

     viewmore.setOnClickListener(this); 
     viewmore1.setOnClickListener(this); 
     viewmore2.setOnClickListener(this); 


     relativeLayout.getViewTreeObserver().addOnPreDrawListener(
      new ViewTreeObserver.OnPreDrawListener() { 

       @Override 
       public boolean onPreDraw() { 
        relativeLayout.getViewTreeObserver().removeOnPreDrawListener(this); 
        relativeLayout.setVisibility(View.GONE); 
        relativeLayout1.setVisibility(View.GONE); 
        relativeLayout2.setVisibility(View.GONE); 

        final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 
        final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 
        relativeLayout.measure(widthSpec, heightSpec); 
        height = relativeLayout.getMeasuredHeight(); 
        relativeLayout1.measure(widthSpec, heightSpec); 
        height1 = relativeLayout.getMeasuredHeight(); 
        relativeLayout2.measure(widthSpec, heightSpec); 
        height2 = relativeLayout.getMeasuredHeight(); 
        return true; 
       } 
      }); 
    } 


    private void expand(RelativeLayout layout, int layoutHeight) { 
     layout.setVisibility(View.VISIBLE); 
     ValueAnimator animator = slideAnimator(layout, 0, layoutHeight); 
     animator.start(); 
    } 

    private void collapse(final RelativeLayout layout) { 
     int finalHeight = layout.getHeight(); 
     ValueAnimator mAnimator = slideAnimator(layout, finalHeight, 0); 

     mAnimator.addListener(new Animator.AnimatorListener() { 
      @Override 
      public void onAnimationEnd(Animator animator) { 
       //Height=0, but it set visibility to GONE 
       layout.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(final RelativeLayout layout, int start, int end) { 
     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 = layout.getLayoutParams(); 
       layoutParams.height = value; 
       layout.setLayoutParams(layoutParams); 
      } 
     }); 
     return animator; 
    } 

    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.viewmore: 
       if (relativeLayout.getVisibility() == View.GONE) { 
        expand(relativeLayout, height); 
       } else { 
        collapse(relativeLayout); 
       } 
       break; 

      case R.id.viewmore1: 
       if (relativeLayout1.getVisibility() == View.GONE) { 
        expand(relativeLayout1, height1); 
       } else { 
        collapse(relativeLayout1); 
       } 
       break; 

      case R.id.viewmore2: 
       if (relativeLayout2.getVisibility() == View.GONE) { 
        expand(relativeLayout2, height2); 
       } else { 
        collapse(relativeLayout2); 
       } 
       break; 
     } 
    } 
} 
+0

안녕하세요, 고마워요. 하지만 처음에는 숨길 수 있도록 VISibility (View.GONE)를 설정하는 방법. – feltonjk

+0

@feltonjk 최근 코멘트 (삭제)가 잘못되었습니다. XML 설정에서 android : visibility = "gone" – Cheticamp

+0

하지만 set android : visibility = "gone"을 설정하면 작동하지 않고 아무것도 표시되지 않습니다. 나 또한 relativeLayout.setVisibility (View.GONE) 너무 노력하고 그 시도했다.다른 soluton 말해 줄 수 있습니까? – feltonjk

0

. 해당 사용자 정의보기에서 확장 또는 접힌 상태를 저장할 수 있습니다. 확장 또는 축소와 같은보기 기본 상태를 정의하기위한 사용자 정의 속성을 만들 수 있습니다. 따라서보기 상태를 비교하여 토글 기능을 호출하여 축소 또는 축소로 전환 할 수도 있습니다.

접힌보기를 기본보기로 표시하려면 onMeasure 전에보기 가시성을 변경하면 안됩니다 기능을 사용하고 측정 된 높이를 저장하십시오. 뷰 생성자에서 가시성을 변경하면 onMeasure 함수는 해당 뷰의 계산을 건너 뜁니다. onPreDraw 기능에 대한 가시성을 전환해야합니다.