2023年7月15日发(作者:)
⾃定义控件(ScrollView的重写)版权声明:欢迎转载,转载请注明出处/qian_xiao_lj实现两个ScrollView的同步显⽰。因为控件中没有此种功能,因此需要重写ScrollView。思路介绍 我们⾸先想到使⽤ ScrollView的类似与setOnScrollChangedListener的⽅法来实现,当⼀个ScrollView滚动时,触发该⽅法进⽽使另外⼀个ScrollView滚动。不过很遗憾,⾕歌没有提供该⽅法。通过查询相应的源代码,我们发现该⽅法的原型protected void onScrollChanged(int x, int y, int oldx, int oldy)
该⽅法是protected类型,不能直接调⽤,于是需要重新实现ScrollView。具体实现 ⾸先,定⼀个⼀个接⼝():public interface ScrollViewListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
我们需要重写ScrollView才能实现该接⼝,因此有下⾯的代码():import t;
import uteSet;
import View;
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) {
ViewListener = scrollViewListener;
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
llChanged(x, y, oldx, oldy);
if(scrollViewListener != null) {
llChanged(this, x, y, oldx, oldy);
}
}
}
接下来是界⾯的XML,这⾥是⼀个简单的Demo,如下():
android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="horizontal" > android:id="@+id/scrollview1" android:layout_width="400dp" android:layout_height="wrap_content" > android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="monday" android:textColor="#000000" /> android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="tuesday" android:textColor="#000000" /> android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="wednesday" android:textColor="#000000" /> android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="thursday" android:textColor="#000000" /> android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="friday" android:textColor="#000000" /> android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="saturday" android:textColor="#000000" /> android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="sunday" android:textColor="#000000" /> android:id="@+id/scrollview2" android:layout_width="400dp" android:layout_height="wrap_content" > android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="monday" android:textColor="#000000" /> android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="tuesday" android:textColor="#000000" /> android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="wednesday" android:textColor="#000000" /> android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="thursday" android:textColor="#000000" /> android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="friday" android:textColor="#000000" /> android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="saturday" android:textColor="#000000" /> android:layout_width="wrap_content" android:layout_height="200dp" android:layout_weight="1" android:text="sunday" android:textColor="#000000" />
import ;
public class TestActivity extends Activity implements ScrollViewListener {
private ObservableScrollView scrollView1 = null;
private ObservableScrollView scrollView2 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
te(savedInstanceState);
setContentView();
scrollView1 = (ObservableScrollView) findViewById(view1);
ollViewListener(this);
scrollView2 = (ObservableScrollView) findViewById(view2);
ollViewListener(this);
}
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
if(scrollView == scrollView1) {
To(x, y);
} else if(scrollView == scrollView2) {
To(x, y);
}
}
}
private MainViewGroup mv;
private int subViewWidth = 0; private int subViewHeight = 0;
private int x; private int y;
private int width;private int height;public LionScrollView() {super(ctivity); = new MainViewGroup();w(mv);}public void addView(View child) {if (child == null)return;w(child);}//This method should return false to intercept the touch eventpublic boolean onInterceptTouchEvent(MotionEvent ev) { return false; }//here you can add protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = ldCount(); for (int i = 0; i < count; i++) {
final View child = ldAt(i); int x = (int) () + suredWidth();
int y = (int) () + suredHeight();
if (x > subViewWidth) {
subViewWidth = x; imumWidth(x); } if (y > subViewHeight) {
subViewHeight = y; imumHeight(y);
* 区别2:菜单的偏移量需要修改 *
* 区别3:菜单的显⽰时有缩放以及透明度变化 缩放:0.7 ~1.0 1.0 - scale * 0.3 透明度 0.6 ~ 1.0
* 0.6+ 0.4 * (1- scale) ; * 0.6+ 0.4 * (1- scale) ; *
*/ float rightScale = 0.7f + 0.3f * scale; float leftScale = 1.0f - scale * 0.3f; float leftAlpha = 0.6f + 0.4f * (1 - scale); // 调⽤属性动画,设置TranslationX nslationX(myMenu, myMenuWidth * scale * 0.8f);
leX(myMenu, leftScale); leY(myMenu, leftScale); ha(myMenu, leftAlpha); // 设置content的缩放的中⼼点 otX(myContent, 0); otY(myContent, ght() / 2); leX(myContent, rightScale); leY(myContent, rightScale); }}
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689425637a246208.html
评论列表(0条)