Computer >> Hướng Dẫn Máy Tính >  >> Hệ Thống >> Android

Thành thạo Android Visibility Listeners: Cách và lý do theo dõi hiển thị View

Giao diện người dùng Android được xây dựng từ các View, và trong một ứng dụng thông thường thường có nhiều View. Để biết View nào người dùng đang nhìn, bạn cần cài đặt Visibility Listeners (Trình nghe hiển thị).

Đọc tiếp để tìm hiểu các tùy chọn khác nhau để xác định trạng thái hiển thị của một View.

Cách trở nên hiển thị

Để các listener hoạt động, trước tiên phải đảm bảo View của bạn nằm trong hệ thống phân cấp layout. Có hai cách này xảy ra:

  1. View của bạn đã là một phần của layout được định nghĩa trong file XML
  2. Bạn tạo View động và cần thêm nó bằng phương thức addView
public void addView(View child, ViewGroup.LayoutParams params)

child là View bạn muốn thêm, LayoutParams là các tham số layout (ví dụ: chiều rộng, chiều cao)

Trạng thái hiển thị của View là kiểu Integer và có một trong ba giá trị:

  1. VISIBLE (0) - View hiển thị cho người dùng
  2. INVISIBLE (4) - View không hiển thị nhưng vẫn chiếm không gian trong layout
  3. GONE (8) - View không hiển thị và không chiếm không gian trong layout

Khi đã nằm trong hệ thống phân cấp layout, có một số tùy chọn gốc để biết khi nào hiển thị của View thay đổi.

onVisibilityChanged

protected void onVisibilityChanged(View changedView, int visibility)

Phương thức này được kích hoạt khi hiển thị của view hoặc của một tổ tiên của view đã thay đổi. Trạng thái hiển thị nằm trong tham số visibility.

onWindowVisibilityChanged

protected void onWindowVisibilityChanged(int visibility)

Phương thức này được kích hoạt khi cửa sổ chứa View của chúng ta thay đổi hiển thị. Điều này không đảm bảo cửa sổ chứa View của bạn hiển thị cho người dùng, vì nó có thể bị che khuất bởi cửa sổ khác.

Visibility Listeners trong thực tế

Để xem hai listener này hoạt động, hãy tạo một dự án đơn giản. Chúng ta sẽ có một LinearLayout với TextView và Button. Sự kiện onClick của Button sẽ thêm custom view vào layout.

Thành thạo Android Visibility Listeners: Cách và lý do theo dõi hiển thị View

Custom View của chúng ta:

package com.tomerpacific.viewvisibility; import android.content.Context; import android.graphics.Color; import android.util.Log; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import static android.view.Gravity.CENTER; public class MyCustomView extends LinearLayout { private final String TAG = MyCustomView.class.getSimpleName(); public MyCustomView(Context context) { super(context); this.setBackgroundColor(Color.GREEN); this.setGravity(CENTER); TextView myTextView = new TextView(context); myTextView.setText("My Custom View"); addView(myTextView); } @Override public void onVisibilityChanged(View changedView, int visibility) { super.onVisibilityChanged(changedView, visibility); Log.d(TAG, "View " + changedView + " changed visibility to " + visibility); } @Override public void onWindowVisibilityChanged(int visibility) { super.onWindowVisibilityChanged(visibility); Log.d(TAG, "Window visibility changed to " + visibility); } }

Và code trong MainActivity:

package com.tomerpacific.viewvisibility; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity { private Button addCustomViewBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addCustomViewBtn = (Button) findViewById(R.id.addCustomViewBtn); addCustomViewBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout); MyCustomView myCustomView = new MyCustomView(getApplicationContext()); myCustomView.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); mainLayout.addView(myCustomView); } }); } }

Chúng ta gắn OnClick Listener cho button

Khi chạy ứng dụng và nhấn button, chúng ta nhận được:

Demo GIF

Bạn có thể tải dự án mẫu tại đây.

ViewTreeObserver

Đây là đối tượng gốc có nhiều listener được thông báo về các thay đổi hiển thị của cây view. Một số listener nổi bật:

  • OnGlobalLayoutListener
  • OnWindowAttachListener
  • OnWindowFocusChangeListener

Để gắn ViewTreeObserver, bạn làm như sau:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.YOUR_VIEW_ID); ViewTreeObserver viewTreeObserver = linearLayout.getViewTreeObserver(); viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); // TODO Thêm logic } });

removeOnGlobalLayoutListener yêu cầu API > 15

Dòng linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this) đảm bảo listener chỉ được gọi một lần. Nếu muốn tiếp tục lắng nghe thay đổi, hãy xóa dòng này.

Nếu bạn có ý kiến hoặc đóng góp, hãy cho tôi biết.