为什么有2个Horizo​​ntalGridView时焦点不会改变

时间:2019-08-07 08:24:47

标签: android gridview focus leanback

我正在使用可在Android 4.4.2上运行的Leanback开发Android TV应用

我在垂直布局的LinearLayout中放置了2个Horizo​​ntalGridView。

当第一个Horizo​​ntalGridView具有焦点,并且在DPAD中按下DOWN键时,焦点成功更改为第二个Horizo​​ntalGridView。

但是,当我在第二个Horizo​​ntalGridView的DPAD中按UP时,焦点不会改变。我预计焦点将更改为1st Horizo​​ntalGridView。

我不知道为什么会这样。有人可以帮忙吗?

我发现这里有focusOutFront,focusOutEnd,focusOutSideStart,focusOutSideEnd属性,我都将其设置为true,但是没有运气。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/browse_dummy"
    android:orientation="vertical"
    android:weightSum="20"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_title"
        android:layout_weight="5"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:textSize="24sp"
        android:text="ATTRIBUTE NAME"
        android:textColor="@drawable/white"/>


    <android.support.v17.leanback.widget.HorizontalGridView
        android:id="@+id/boxset_vgv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:focusable="true"
        android:focusableInTouchMode="true"
        app:focusOutEnd="true"
        app:focusOutFront="true"
        >

    </android.support.v17.leanback.widget.HorizontalGridView>

    <!-- Attribute List-->
    <android.support.v17.leanback.widget.HorizontalGridView
        android:id="@+id/attribute_grid_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:focusable="true"
        android:focusableInTouchMode="true"
        app:focusOutEnd="true"
        app:focusOutFront="true">

    </android.support.v17.leanback.widget.HorizontalGridView>
</LinearLayout>

这是Horizo​​ntalGridView的演示者

public class AttributePresenter extends Presenter {

private static final String TAG = "AttributePresenter";

private int mBrowserDockId;

public interface AttributeOnSelectedListener {
    public void onAttributeSelected(Category attribute, int attributeItemViewId);
}

public AttributeOnSelectedListener mListener;

public AttributePresenter() {

}

public AttributePresenter(AttributeOnSelectedListener listener, int browserDockId) {
    mListener = listener;
    mBrowserDockId = browserDockId;
}

public AttributePresenter(AttributeOnSelectedListener listener) {
    mListener = listener;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup) {

    int attributeItemViewId = View.generateViewId();
    AttributeItemView v = new AttributeItemView(viewGroup.getContext()) {
        @Override
        public void setSelected(boolean selected) {

            if(mListener != null && selected) {

                mListener.onAttributeSelected(mAttribute, attributeItemViewId);
            }

            Log.d("AttributePresenter", "XJIXX");
            super.setSelected(selected);
        }
    };
    v.setId(attributeItemViewId);

    Log.d(TAG, String.valueOf(mBrowserDockId));
    Log.d(TAG, String.valueOf(v.getNextFocusDownId()));
    Log.d(TAG, String.valueOf(v.getNextFocusUpId()));

    return new Presenter.ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, Object item) {
    if(item instanceof Category) {
        Category attribute = (Category)item;

        TextView tv = (TextView)viewHolder.view.findViewById(R.id.tv_attribute_title);
        tv.setText(attribute.getName());
        ((AttributeItemView)viewHolder.view).mAttribute = attribute;

    }
}

@Override
public void onUnbindViewHolder(ViewHolder viewHolder) {

}
}

这是项目视图

public class AttributeItemView extends FrameLayout {

    public Category mAttribute;

    public AttributeItemView(Context context) {
        super(context);

        LayoutInflater.from(getContext()).inflate(R.layout.attribute_item, this);
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

}

这是项目布局xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/attribute_item_layout"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_attribute_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="@color/attribute_item_text_selector"
        android:text=""/>
</RelativeLayout>

我希望UP和DOWN可以相应地改变焦点。

1 个答案:

答案 0 :(得分:0)

尝试从Horizo​​ntalGridView小部件中删除以下属性:

android:focusable="true"
android:focusableInTouchMode="true"
app:focusOutEnd="true"
app:focusOutFront="true"

如果焦点仍然无法正确更改,请发布演示者。