Home > Mobile >  About RecyclerView focus and key event in TV development problems.
About RecyclerView focus and key event in TV development problems.

Time:10-07

Layout code below
The android: id="@ + id/play_view"
Android: focusable="false"
Android: layout_width="match_parent"
Android: layout_height="match_parent" & gt;



The android: id="@ + id/polyv_player_media_controller"
Android: layout_width="match_parent"
Android: layout_height="match_parent"
Android: visibility="gone"/& gt;

The 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="0 dp"
/>
The Activity code
MetroViewBorderImpl mMetroViewBorderImpl=new MetroViewBorderImpl (this);
MMetroViewBorderImpl. AttachTo (ry_content);
LinearLayoutManager ms=new LinearLayoutManager (this);
Ms. SetOrientation (LinearLayoutManager. VERTICAL);//set recyclerview layout for horizontal layout
Ry_content. SetFocusable (false);
Ry_content. SetLayoutManager (ms);//add RecyClerView set good layout style
//to recyclerview add data content
Adapter=new videoAdapter (this, R.l ayout. Item_video, null);

Ry_content. SetAdapter (adapter);
Ry_content. ScrollToPosition (0);
Ry_content. SetVisibility (the VISIBLE);
Ry_content. SetSelected (true);
The rest of the
Ry_content. GetChildAt (0). RequestFocusFromTouch ();
Ry_content. GetChildAt (0). RequestFocus ();
I want to know why the Activity when loading focus could not be gathered RecyclerView item,?????? Not trigger buttons up and down or so, but touch screen click event is normal use, I try to use requestFocus requestFocusFromTouch while the result is focus on the RecyclerView, but the button still can't use, you must first click at any RecyclerView can normal use, but when I use ListView instead of RecyclerView can normal use

CodePudding user response:

You need to android: descendantFocusability="afterDescendants" and
Child controls defined in the XML android: focusable="true"

CodePudding user response:

In RecyclerView is configurator onBindViewHolder add item gains focus matter (change the item background, looking at intuitive)
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 (" # 908 a8a8a "));

} else {

Holder. ItemView. SetBackgroundColor (Color. ParseColor (" # FFFFFF "));
}
}
});


Then add a button in the activity of listening in
@ Override
Public Boolean onKeyDown (int keyCode, KeyEvent event) {

If (event. GetKeyCode ()==KeyEvent. KEYCODE_DPAD_CENTER) {
BtnSubmit. PerformClick ();
return true;
}
//delete key
If (event. GetKeyCode ()==KeyEvent. KEYCODE_DEL) {
Presenter. Remove ();
Return true;
}
//return key
If (event. GetKeyCode ()==KeyEvent. KEYCODE_BACK) {
UnregisterReceiver (mBrReceiver);
Finish ();
Return true;
}
Return super. OnKeyDown (keyCode, event);
}

CodePudding user response:


You need this: mRecyclerView setItemsCanFocus (true);

CodePudding user response:

After completion of loading the rv to first get focus
 
. Data loading is completed
Adapter. NotifyDataSetChanged ();
The rv. Post (new Runnable () {
@ Override
Public void the run () {
The rv. GetChildAt (0). RequestFocus ();
}
});

Is roughly such written, you measure yourself
  • Related