效果
初始化頁面,用戶將光標移動到BUTTON3上,並向上移動焦點。就切換屏幕,下圖:
控件類ScrollLinearLayout
繼承LinearLayout,ScrollLinearLayout.java代碼如下:
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.Scroller;
public class ScrollLinearLayout extends LinearLayout {
private Scroller scroller;
public ScrollLinearLayout(Context context) {
this(context, null);
}
public ScrollLinearLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
scroller = new Scroller(context);
}
public void startScroll(int y) {
scroller.startScroll(0, getScrollY(), 0, y);
invalidate();
}
@Override
public void computeScroll() {
//判斷是否滾動完成
if (scroller.computeScrollOffset()) {
scrollTo(scroller.getCurrX(), scroller.getCurrY());
}
}
}
ScrollLinearLayoutActivity代碼
ScrollLinearLayoutActivity.java代碼如下:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.Button;
import com.cc.demo.R;
public class ScrollLinearLayoutActivity extends AppCompatActivity {
private ScrollLinearLayout scrollLinearLayout;
private Button btn2;
private Button btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll_linear_layout);
//按照720P的絕對值佈局進行測試的。
scrollLinearLayout = (ScrollLinearLayout) findViewById(R.id.svg);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn3.requestFocus();
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_UP:
if (btn3.isFocused()) {
scrollLinearLayout.startScroll(-600);
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
if (btn2.isFocused()) {
scrollLinearLayout.startScroll(600);
}
break;
}
}
return super.dispatchKeyEvent(event);
}
}
activity_scroll_linear_layout代碼
activity_scroll_linear_layout.xml 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<com.cc.demo.scroll.ScrollLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/svg"
android:layout_width="match_parent"
android:layout_height="1320px"
android:layout_marginTop="-600px"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="100px"
android:text="button1"
android:textColor="@color/white"
android:textSize="20sp" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="100px"
android:text="button2"
android:textColor="@color/white"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="400px"
android:background="@color/colorPrimaryDark" />
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="100px"
android:layout_marginBottom="10dp"
android:text="button3"
android:textColor="@color/white"
android:textSize="20sp" />
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="100px"
android:layout_marginBottom="10dp"
android:text="button4"
android:textColor="@color/white"
android:textSize="20sp" />
<Button
android:id="@+id/btn5"
android:layout_width="match_parent"
android:layout_height="200px"
android:layout_marginBottom="10dp"
android:text="button5"
android:textColor="@color/white"
android:textSize="20sp" />
<Button
android:id="@+id/btn6"
android:layout_width="match_parent"
android:layout_height="200px"
android:layout_marginBottom="10dp"
android:text="button6"
android:textColor="@color/white"
android:textSize="20sp" />
</com.cc.demo.scroll.ScrollLinearLayout>
其它
- 根據具體的需求,TV上翻屏也可以用ScrollView來實現。
- Android TV上的ScrollView和HorizontalScrollView是可以直接使用。和觸摸屏上一樣。
- Android TV上也可以正常使用RecyclerView,焦點可能會存在小問題,上拉加載更多和下拉刷新需要自己實現。建議使用開源庫TvRecyclerView。