【问题标题】:Linearlayout programtically clicked, focused and active state is not workingLinearlayout以编程方式单击,聚焦和活动状态不起作用
【发布时间】:2020-11-18 02:25:26
【问题描述】:

我有一个具有 4x4 网格的 gridview 的应用程序。当光标位于这些网格项目的顶部并且光标由与手机连接的蓝牙操纵杆控制时,我试图突出显示并单击网格项目。

现在我已经创建了一个像这样的背景 -

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/state_active" android:state_pressed="true"/>
    <item android:drawable="@drawable/state_focus" android:state_focused="true"/>
    <item android:drawable="@drawable/state_normal"/>
</selector>

我将其设置为由 Linearlayout 组成的每个网格项的背景,以便我可以在以编程方式执行操作时更改状态。

public void triggerItem(int position) {
        if(gridView != null && gridView.getChildAt(position) != null)
            gridView.performItemClick(gridView.getAdapter().getView(position,null,null),position,position);
}

private void highlightItem(int position, int state) {
    if(gridView != null && gridView.getChildAt(position) != null)
        gridView.getChildAt(position).setActivated((state == 1) ? true : false);
      //  gridView.getChildAt(position).setFocused((state == 1) ? true : false);
}

这些组合都不起作用。谁能告诉我该怎么做,或者是否可以通过任何方式?

【问题讨论】:

    标签: android android-layout android-linearlayout android-gridview


    【解决方案1】:

    所以我已经想通了,因为我的兄弟已经指导我这样做了——

        private void highlightItem(int position, int state) {
            if(gridView != null && gridView.getChildAt(position) != null) {
                gridView.getChildAt(position).setEnabled((state==1)?false:true);
                gridView.getChildAt(position).setPressed((state==1)?true:false);
            }
        }
    

    并更改您的 xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:drawable="@drawable/state_pressed"
            android:state_pressed="true"/>
        <item
            android:state_enabled="true"
            android:drawable="@drawable/state_normal_action"/>
    </selector>
    

    【讨论】:

      猜你喜欢
      • 2018-01-06
      • 2013-10-04
      • 2013-02-17
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      相关资源
      最近更新 更多