布局代码如下
<com.easefun.polyvsdk.video.PolyvVideoView
android:id="@+id/play_view"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.easefun.polyvsdk.video.PolyvVideoView>
<com.lqh.asuzx.testplay.PolyvPlayerMediaController
android:id="@+id/polyv_player_media_controller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" /> <android.support.v7.widget.RecyclerView
android:id="@+id/ry_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:focusable="false"
android:paddingTop="0dp"
/>
Activity代码
MetroViewBorderImpl mMetroViewBorderImpl = new MetroViewBorderImpl(this);
mMetroViewBorderImpl.attachTo(ry_content);
LinearLayoutManager ms = new LinearLayoutManager(this);
ms.setOrientation(LinearLayoutManager.VERTICAL);// 设置 recyclerview 布局方式为横向布局
ry_content.setFocusable(false);
ry_content.setLayoutManager(ms); //给RecyClerView 添加设置好的布局样式
// 对 recyclerview 添加数据内容
adapter = new videoAdapter(this, R.layout.item_video, null); ry_content.setAdapter(adapter);
ry_content.scrollToPosition(0);
ry_content.setVisibility(View.VISIBLE);
ry_content.setSelected(true);
其余
ry_content.getChildAt(0).requestFocusFromTouch();
ry_content.getChildAt(0).requestFocus();
我想知道为什么Activity加载的时候 焦点无法聚集上RecyclerView的item中。 ?? 按键的上下左右无法触发。但是触屏的点击事件正常使用。 我尝试使用requestFocus requestFocusFromTouch 结果得到的是 焦点虽然到RecyclerView上了。但是按键还是无法使用,必须先任意点击一下RecyclerView才可以正常使用,但是当我使用ListView 来代替RecyclerView 却可以正常使用
<com.easefun.polyvsdk.video.PolyvVideoView
android:id="@+id/play_view"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.easefun.polyvsdk.video.PolyvVideoView>
<com.lqh.asuzx.testplay.PolyvPlayerMediaController
android:id="@+id/polyv_player_media_controller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" /> <android.support.v7.widget.RecyclerView
android:id="@+id/ry_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:focusable="false"
android:paddingTop="0dp"
/>
Activity代码
MetroViewBorderImpl mMetroViewBorderImpl = new MetroViewBorderImpl(this);
mMetroViewBorderImpl.attachTo(ry_content);
LinearLayoutManager ms = new LinearLayoutManager(this);
ms.setOrientation(LinearLayoutManager.VERTICAL);// 设置 recyclerview 布局方式为横向布局
ry_content.setFocusable(false);
ry_content.setLayoutManager(ms); //给RecyClerView 添加设置好的布局样式
// 对 recyclerview 添加数据内容
adapter = new videoAdapter(this, R.layout.item_video, null); ry_content.setAdapter(adapter);
ry_content.scrollToPosition(0);
ry_content.setVisibility(View.VISIBLE);
ry_content.setSelected(true);
其余
ry_content.getChildAt(0).requestFocusFromTouch();
ry_content.getChildAt(0).requestFocus();
我想知道为什么Activity加载的时候 焦点无法聚集上RecyclerView的item中。 ?? 按键的上下左右无法触发。但是触屏的点击事件正常使用。 我尝试使用requestFocus requestFocusFromTouch 结果得到的是 焦点虽然到RecyclerView上了。但是按键还是无法使用,必须先任意点击一下RecyclerView才可以正常使用,但是当我使用ListView 来代替RecyclerView 却可以正常使用
子控件的xml中定义 android:focusable="true"
holder.itemView.setFocusable(true);
holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
holder.itemView.setBackgroundColor(Color.parseColor("#908a8a8a")); } else { holder.itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
}
});
然后在activity中添加按键的监听
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { if(event.getKeyCode()==KeyEvent.KEYCODE_DPAD_CENTER){
btnSubmit.performClick();
return true;
}
//删除键
if(event.getKeyCode()==KeyEvent.KEYCODE_DEL){
presenter.remove();
return true;
}
//返回键
if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
unregisterReceiver(mBrReceiver);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
你需要这个: mRecyclerView.setItemsCanFocus(true);
adapter.notifyDataSetChanged();
rv.post(new Runnable() {
@Override
public void run() {
rv.getChildAt(0).requestFocus();
}
});大致是这样写的,你自己测测