布局代碼如下
<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 卻可以正常使用
uj5u.com熱心網友回復:
你需要 android:descendantFocusability="afterDescendants" 和子控制元件的xml中定義 android:focusable="true"
uj5u.com熱心網友回復:
在 RecyclerView的是配置器中onBindViewHolder 中添加item獲得焦點事(改變item的背景,看著直觀)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);
}
uj5u.com熱心網友回復:
你需要這個: mRecyclerView.setItemsCanFocus(true);
uj5u.com熱心網友回復:
在rv加載完成之后讓第一個獲取焦點
...資料加載完成之后
adapter.notifyDataSetChanged();
rv.post(new Runnable() {
@Override
public void run() {
rv.getChildAt(0).requestFocus();
}
});
大致是這樣寫的,你自己測測
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/117098.html
標籤:Android
上一篇:使用Android 谷歌新推介面android.media.projection 截屏時使同屏器(miracast協議)斷開
