Scroller 界面滑动

251012025799394.gif

Scroller 滑动之后滑动器子标签里面的控件,其自身是不动的.这里点在下面的进阶会体现出来.
 1. 继承LinearLayout, 相对布局也可以.
public class MyLinearLayout extends LinearLayout {

   private Scroller scroller;

    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        scroller = new Scroller(context);
    }

    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        if (!scroller.isFinished()) scroller.abortAnimation();
        scroller.startScroll(startX, startY, dx, dy, duration);
        invalidate();
    }

    @Override
    public void computeScroll() {
        Log.d("cece", this.toString() + " computeScroll-----------");
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(), scroller.getCurrY());
            postInvalidate();
        }
    }
}


2. 主界面
public class MyActivity extends Activity implements View.OnClickListener {

    private static final String TAG = "MyActivity";
    private Button button;
    private MyLinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        button = (Button) findViewById(R.id.button);
       // imageView = (ImageView) findViewById(R.id.iv);
        linearLayout = (MyLinearLayout) findViewById(R.id.rl);


        button.setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }


    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                int x = linearLayout.getScrollX();
                int y = linearLayout.getScrollY();
                linearLayout.startScroll(x, y, 0, 100, 1000);
                break;
            case R.id.button2:
                 x = linearLayout.getScrollX();
                 y = linearLayout.getScrollY();
                Log.d(TAG, "x:"+x+" y:"+y);
                linearLayout.startScroll(x, y, 0, -100, 1000);
                break;
        }
    }
}



3. 资源文件
<com.bytereal.luv.myapplication.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button1" />
    <!--   android:layout_alignParentBottom="true"
              android:layout_alignParentEnd="true"-->

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button2" />
    <!-- android:layout_alignTop="@+id/button"
      android:layout_toStartOf="@+id/button"-->
</com.bytereal.luv.myapplication.MyLinearLayout>









转载于:https://www.cnblogs.com/lv-2012/p/0e8682575833d981b374505126158ea2.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值