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);
}
}
}
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>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有