Android 슬라이더 구성 요소 부상 이 상단 에 고정 되 어 있 음

본 논문 의 사례 는 안 드 로 이 드 슬라이딩 구성 요소 가 상단 에 고정 되 어 있 는 효과 에 대한 구체 적 인 코드 를 공유 하 였 으 며,구체 적 인 내용 은 다음 과 같다.
실현 하고 자 하 는 효 과 는 다음 과 같다.

장면:어떤 때 는 내용 중간 에 있 는 구성 요소 가 맨 위로 미 끄 러 질 때 맨 위 에 고정 되 어 있 습 니 다.
실현 의 사고:
1.대상 구성 요소(button)는 두 세트 로 상단 과 내용 사이 에 놓 습 니 다.
2.내용 중간 에 있 는 구성 요소 가 상단 표시 줄 위치 로 미 끄 러 질 때 상단 과 중간 에 있 는 구성 요 소 를 표시/숨 깁 니 다(구성 요소 가 화면 에 있 는 위치 지식 을 가 져 오 는 것 과 관련).
activity 코드:

public class MainActivity extends AppCompatActivity implements ObservableScrollView.ScrollViewListener { 
 private ObservableScrollView scrollView; 
 private Button topBtn1, topBtn2, middleBtn1, middleBtn2; 
 private View topPanel, middlePanel; 
 private int topHeight; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  initViews(); 
  initListeners(); 
 
 } 
 
 @Override 
 public void onWindowFocusChanged(boolean hasFocus) { 
  super.onWindowFocusChanged(hasFocus); 
 
  Rect frame = new Rect(); 
  getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); 
  int statusBarHeight = frame.top;//      
 
  int titleBarHeight = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();//      
  topHeight = titleBarHeight + statusBarHeight; 
 } 
 
 
 private void initViews() { 
  scrollView = (ObservableScrollView) findViewById(R.id.scrollView); 
  topPanel = findViewById(R.id.topPanel); 
  topBtn1 = (Button) topPanel.findViewById(R.id.button1); 
  topBtn2 = (Button) topPanel.findViewById(R.id.button2); 
 
  middlePanel = findViewById(R.id.middlePanel); 
  middleBtn1 = (Button) middlePanel.findViewById(R.id.button1); 
  middleBtn2 = (Button) middlePanel.findViewById(R.id.button2); 
 
 } 
 
 private void initListeners() { 
  topBtn1.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View view) { 
    middleBtn1.setBackgroundColor(Color.WHITE); 
    topBtn1.setBackgroundColor(Color.WHITE); 
   } 
  }); 
 
  middleBtn1.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View view) { 
    middleBtn1.setBackgroundColor(Color.BLUE); 
    topBtn1.setBackgroundColor(Color.BLUE); 
   } 
  }); 
 
  scrollView.setScrollViewListener(this); 
 
 
 } 
 
 
 @Override 
 public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) { 
  int[] location = new int[2]; 
  middleBtn1.getLocationOnScreen(location); 
  int locationY = location[1]; 
  Log.e("locationY", locationY + " " + "topHeight   :" + topHeight); 
 
  if (locationY <= topHeight && (topPanel.getVisibility() == View.GONE || topPanel.getVisibility() == View.INVISIBLE)) { 
   topPanel.setVisibility(View.VISIBLE); 
  } 
 
  if (locationY > topHeight && topPanel.getVisibility() == View.VISIBLE) { 
   topPanel.setVisibility(View.GONE); 
  } 
 
 } 
} 
요점 분석:
1.onWindow FocusChanged()방법 에서 화면 상태 표시 줄 과 제목 표시 줄 의 높이 를 가 져 옵 니 다(onCreate()방법 에서 가 져 오 는 것 은 0 입 니 다).
2.레이아웃 에 있 는 ScrollView 의 onScrollChangeListener()방법 이 낮은 버 전 API 가 지원 되 지 않 기 때문에 activity 는 ScrollView 의 onScrollChanged()인터페이스 방법 을 사용자 정의 합 니 다―>이 방법 에서 구성 요소 의 표시/숨 김 을 실현 합 니 다.
사용자 정의 ScrollView 코드:

public class ObservableScrollView extends ScrollView { 
 
 private ScrollViewListener scrollViewListener = null; 
 
 public ObservableScrollView(Context context) { 
  super(context); 
 } 
 
 public ObservableScrollView(Context context, AttributeSet attrs, 
        int defStyle) { 
  super(context, attrs, defStyle); 
 } 
 
 public ObservableScrollView(Context context, AttributeSet attrs) { 
  super(context, attrs); 
 } 
 
 public void setScrollViewListener(ScrollViewListener scrollViewListener) { 
  this.scrollViewListener = scrollViewListener; 
 } 
 
 @Override 
 protected void onScrollChanged(int x, int y, int oldx, int oldy) { 
  super.onScrollChanged(x, y, oldx, oldy); 
  if (scrollViewListener != null) { 
   scrollViewListener.onScrollChanged(this, x, y, oldx, oldy); 
  } 
 } 
 
 public interface ScrollViewListener { 
 
  void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy); 
 
 } 
} 
다음 레이아웃 파일:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:id="@+id/activity_main" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 tools:context="com.example.administrator.slideholdapp.MainActivity"> 
 
 <com.example.administrator.slideholdapp.ObservableScrollView 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:id="@+id/scrollView"> 
 
  <LinearLayout 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" 
   android:orientation="vertical"> 
 
   <TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="30dp" 
    android:text="@string/content" /> 
 
   <include android:id="@+id/middlePanel" layout="@layout/middle_item_layout"></include> 
 
   <TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="30dp" 
    android:text="@string/content" /> 
 
 
  </LinearLayout> 
 
 </com.example.administrator.slideholdapp.ObservableScrollView> 
 
 <include android:id="@+id/topPanel" layout="@layout/middle_item_layout" android:visibility="gone"/> 
</FrameLayout> 
미끄럼 기능 에 관 한 글 은 주 제 를 클릭 하 십시오《안 드 로 이 드 슬라이딩 기능》
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기