Android TV listview的侧滑动画

  • 在Android TV 的编程中,会遇到各种各样与手机端不同的问题,其中交互方式(没有触屏)带来的各种问题尤多.其中比如在lisview中,要想实现item侧滑,我们没法出发其中的onTouchEvent事件,所以在github上面的诸如SlideListview等不适用于TV端.那么我们换种思路,如果从listview角度开发起来比较困难的话,那我们从item的自身view来控制自己的滑动就会变得更加简单.
  • item布局代码如下:

 <com.wx.message.widget.ScrollLinearLayout
            android:id="@+id/list_content"
            android:layout_width="match_parent"
            android:layout_height="95dp"
            android:background="@drawable/list_bg"
            android:clickable="true"
            android:focusable="true"
            android:orientation="horizontal"
            android:duplicateParentState="true">

            <ImageView
                android:id="@+id/msg_list_user"
                android:src="@drawable/user"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="39dp"
                android:clickable="false"
                android:focusable="false"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/msg_level"
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="39dp"
                android:focusable="false"
                android:gravity="center_vertical"
                android:textColor="@color/white"
                android:textSize="24dp" />

            <TextView
                android:id="@+id/msg_title"
                android:layout_width="457dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="20dp"
                android:focusable="false"
                android:gravity="center_vertical"
                android:maxLines="1"
                android:textColor="@color/snow"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/msg_create_date"
                android:layout_width="110dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="0dp"
                android:focusable="false"
                android:gravity="center_vertical"
                android:textColor="@color/snow"
                android:textSize="20dp" />

            <com.wx.message.widget.UButton
                android:id="@+id/msg_delete_ly"
                style="@style/button_delete"
                android:layout_width="113dp"
                android:layout_height="96dp"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp"
                android:clickable="false"
                android:focusable="false"
                android:gravity="center"
                android:text="@string/msg_del"
                android:textColor="@color/white"
                android:textSize="24dp"
                android:visibility="visible"/>
        </com.wx.message.widget.ScrollLinearLayout>

对于该item使用自己定义的ScrollLinearLayout,完成对滑动动画的操作,其中将数字等直接放入代码中的行为不可取,这里为了演示更加直观.

public class ScrollLinearLayout extends LinearLayout {

    private Scroller mScroller;

    public ScrollLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context);
    }

    public void startSrcoll() {
        mScroller.startScroll(0, 0, 136, 0, 4000);
        invalidate();
    }

    public void scrollBack(){
        mScroller.startScroll(136, 0, -136, 0, 4000);
        invalidate();
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            // 产生了动画效果,根据当前值 每次滚动一点
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            // 此时同样也需要刷新View ,否则效果可能有误差
            postInvalidate();
        }
    }
    private class LlayoutOnKeyListener implements View.OnKeyListener {
        int pos = -1;
        ScrollLinearLayout scrollContent;
        public LlayoutOnKeyListener(int position, ScrollLinearLayout scrolllayout) {
//          super();
            pos = position;
            scrollContent = scrolllayout;
        }

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            ScrollLinearLayout scroll = (ScrollLinearLayout) v;
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                //按右键弹出删除btn
                if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                    scrollContent.startSrcoll();
                }
            }

            return false;
        }

    }

左滑同上,调用scrollback方法即可.


如果在布局中有子控件存在,需要注意子控件的焦点获取问题,焦点在布局上还是在子控件上可以自己设置,多考虑焦点情况,也是TV开发中需要尤为注意的问题.

焦点在第一个

焦点在删除按钮

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值