1. 程式人生 > >簡單自定義實現兩個HorizontalScrollView關聯滑動

簡單自定義實現兩個HorizontalScrollView關聯滑動

1.MyHorizontalScrollView的程式碼
public class MyHorizontalScrollView extends HorizontalScrollView {
    //另一個HorizontalScrollView
MyHorizontalScrollView otherHsv;

    public MyHorizontalScrollView(Context context) {
        this(context, null);
    }

    public MyHorizontalScrollView(Context context, AttributeSet attrs) {
        super
(context, attrs); } //滑動時呼叫 @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { //不為空,例項不是自己是呼叫 if (otherHsv != null && otherHsv != this) { otherHsv.scrollTo(l, t); } } public void setOtherHsv(MyHorizontalScrollView otherHsv) { this
.otherHsv = otherHsv; } 2.MyActivity的程式碼
public class MyActivity extends AppCompatActivity {
    MyHorizontalScrollView scrollView1, scrollView2;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second
); scrollView1 = (MyHorizontalScrollView) findViewById(R.id.hsv1); scrollView2 = (MyHorizontalScrollView) findViewById(R.id.hsv2); scrollView1.setOtherHsv(scrollView2); scrollView2.setOtherHsv(scrollView1); } }
佈局程式碼就是兩個上下的HorizontalScrollView,這裡就不寫了!
圖片以後加吧!
}