Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the next UI:

Scheme

When user uses D-pad in recyclerview I want to focus "Part 1" by default and if button down pressed. But if user pressed button right when current focus is in "Part 1" focus must be changed to "Part 2" (and to "Part 1" if left arrow button pressed and current focus is in Part 2).

My current logic is default - when clicking button down all item / adapter raw is focused.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/LL"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:weightSum="1"
    android:orientation="vertical"
    android:background="@drawable/item_touch_selector"
    android:baselineAligned="false"
    >

    <LinearLayout
        android:id="@+id/LLAll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:layout_marginStart="@dimen/dimen_margin_channels"
        android:layout_marginEnd="@dimen/dimen_margin_channels"
        tools:ignore="UselessParent"
        >

        <LinearLayout
            android:id="@+id/LLPart1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:layout_weight="0.9"
            android:minHeight="?android:attr/listPreferredItemHeight"
            android:orientation="horizontal"
            >

            <androidx.cardview.widget.CardView
                android:layout_width="@dimen/list_icon"
                android:layout_height="@dimen/list_icon"
                app:cardElevation="0dp"
                app:cardCornerRadius="10dp"
                app:cardBackgroundColor="@color/colorF5F5F5"
                >

                <ImageView
                    android:id="@+id/list_item_icon"
                    tools:src="@mipmap/ic_launcher"
                    android:layout_gravity="center"
                    android:layout_height="@dimen/list_icon"
                    android:layout_width="@dimen/list_icon"
                    android:contentDescription="@string/image_content_description"
                    android:adjustViewBounds="true"
                    android:layout_marginTop="4dp"
                    android:scaleType="fitCenter"
                    android:background="@color/colorF5F5F5"
                    android:padding="5dp"
                    android:layout_marginBottom="4dp" />
            </androidx.cardview.widget.CardView>

            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="7"
                android:paddingStart="13dp"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/list_item_content_name"
                    tools:text="ICTV"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    style="@style/font_gilroy_medium" />

                <TextView
                    android:id="@+id/TVC"
                    android:text="@string/item_categories"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    style="@style/font_gilroy_regular"
                    android:layout_marginTop="4dp"
                    />

            </LinearLayout>


        </LinearLayout>

        <LinearLayout
            android:id="@+id/LLPart2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_weight="0.1"
            >

            <ImageView
                android:id="@+id/list_item_icon_fav"
                android:layout_width="28dp"
                android:layout_height="28dp"
                android:layout_gravity="center"
                android:contentDescription="@string/image_content_description"
                app:srcCompat="@drawable/ic_likenormal" />

        </LinearLayout>

    </LinearLayout>

    <View
        android:id="@+id/VSeparator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/black20Per"
        android:layout_marginStart="64dp"
        />

</LinearLayout>

What should I change to achieve expected result?

I've tried some combinations of

android:focusableInTouchMode=""
android:focusable=""

but with no result.

UPD from 14.01.2021:

So I've tried the next:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/LL"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:weightSum="1"
    android:orientation="vertical"
    android:background="@drawable/item_touch_selector"
    android:baselineAligned="false"
    >

    <LinearLayout
        android:id="@+id/LLAll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:layout_marginStart="@dimen/dimen_margin_channels"
        android:layout_marginEnd="@dimen/dimen_margin_channels"
        tools:ignore="UselessParent"
        >

        <LinearLayout
            android:id="@+id/LLPart1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:layout_weight="0.9"
            android:minHeight="?android:attr/listPreferredItemHeight"
            android:orientation="horizontal"
            android:nextFocusRight="@id/LLPart2"
            android:nextFocusDown="@id/LLPart1"
            >
            
            <requestFocus />

            <androidx.cardview.widget.CardView
                android:layout_width="@dimen/list_icon"
                android:layout_height="@dimen/list_icon"
                app:cardElevation="0dp"
                app:cardCornerRadius="10dp"
                app:cardBackgroundColor="@color/colorF5F5F5"
                >

                <ImageView
                    android:id="@+id/list_item_icon"
                    tools:src="@mipmap/ic_launcher"
                    android:layout_gravity="center"
                    android:layout_height="@dimen/list_icon"
                    android:layout_width="@dimen/list_icon"
                    android:contentDescription="@string/image_content_description"
                    android:adjustViewBounds="true"
                    android:layout_marginTop="4dp"
                    android:scaleType="fitCenter"
                    android:background="@color/colorF5F5F5"
                    android:padding="5dp"
                    android:layout_marginBottom="4dp" />
            </androidx.cardview.widget.CardView>

            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="7"
                android:paddingStart="13dp"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/list_item_content_name"
                    tools:text="ICTV"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    style="@style/font_gilroy_medium" />

                <TextView
                    android:id="@+id/TVC"
                    android:text="@string/item_categories"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    style="@style/font_gilroy_regular"
                    android:layout_marginTop="4dp"
                    />

            </LinearLayout>


        </LinearLayout>

        <LinearLayout
            android:id="@+id/LLPart2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_weight="0.1"
            android:nextFocusLeft="@id/LLPart1"
            >

            <ImageView
                android:id="@+id/list_item_icon_fav"
                android:layout_width="28dp"
                android:layout_height="28dp"
                android:layout_gravity="center"
                android:contentDescription="@string/image_content_description"
                app:srcCompat="@drawable/ic_likenormal" />

        </LinearLayout>

    </LinearLayout>

    <View
        android:id="@+id/VSeparator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/black20Per"
        android:layout_marginStart="64dp"
        />

</LinearLayout>

But still all LinearLayout "LL"(all raw) is selected (instead of only "LLPart1"). Right and left arrows do nothing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

Use requestFocus for default focused view and use nextFocusUp/Down/Left/Right for each view


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...