簡單自定義實現兩個HorizontalScrollView關聯滑動
阿新 • • 發佈:2019-02-11
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佈局程式碼就是兩個上下的HorizontalScrollView,這裡就不寫了!); scrollView1 = (MyHorizontalScrollView) findViewById(R.id.hsv1); scrollView2 = (MyHorizontalScrollView) findViewById(R.id.hsv2); scrollView1.setOtherHsv(scrollView2); scrollView2.setOtherHsv(scrollView1); } }
圖片以後加吧! }