1

我想实现以下能力:

  • 通过长按每次只选择一个子View项。GridLayout
  • 如果已经选择了一个,则单击GridLayout视觉层次结构中的父级或任何祖先父级将取消选择选定的子级View

问题是当View.OnLongClickListener向 child 注册回调时View,无论是 parentGridLayout还是任何祖先注册的回调(或View.OnClickListenerView.onTouchEvent)都没有在单击它们时被调用。

我怎样才能得到一个GridLayout类似于AdapterView.OnItemSelectedListeneror的选定孩子AdapterView.OnItemLongClickListener并解决上述问题?

4

2 回答 2

4

将“选定”视图存储为全局变量并在其焦点发生变化时将其删除怎么样?focusable通过与,focusableInTouchModeonClicklisteners一起玩,您可以获得正确的结果。我不确定这是最好的解决方案,但它确实有效。

你需要什么:

  • 全局 View 变量GridLayout的子项长时间单击,如选中。
  • (可选) 作为 any的自定义父容器ViewGroup:它将在其所有子级[*]上设置可聚焦的侦听器。在我的测试中,我使用了 aLinearLayout和 a RelativeLayout

[*] 如果不使用可选的父自定义类,则必须在父 ViewGroup 的所有子级上设置android:focusable="true"和。android:focusableInTouchMode="true"并且您必须设置OnClickListener才能在removeViewSelected()单击父 ViewGroup 时调用。

  • 为子级添加Click 侦听GridLayout器:更新所选视图。
  • 实现一个焦点监听器:如果它失去焦点,它会删除选定的视图。

它将处理父子层次结构上的所有焦点更改状态,请参阅输出:

GridLayout 选择视图并禁用单击侦听器上的视图

我使用了以下模式:

CoordinatorLayout         --- simple root group
    ParentLayout          --- aka "parentlayout"
        Button            --- simple Button example
        GridLayout        --- aka "gridlayout"
    FloattingActionButton --- simple Button example

让我们准备 selectedView及其更新方法Activity

private View selectedView;

...
private void setViewSelected(View view) {
    removeViewSelected();

    selectedView = view;
    if (selectedView != null) {
        // change to a selected background for example
        selectedView.setBackgroundColor(
                ContextCompat.getColor(this, R.color.colorAccent));
    }
}

private View getViewSelected() {
    if (selectedView != null) {
        return selectedView;
    }
    return null;
}

private void removeViewSelected() {
    if (selectedView != null) {
        // reset the original background for example
        selectedView.setBackgroundResource(R.drawable.white_with_borders);
        selectedView = null;
    }
    // clear and reset the focus on the parent
    parentlayout.clearFocus();
    parentlayout.requestFocus();
}

在每个GridLayout孩子上,添加ClickLongClick侦听器以更新或删除选定的视图。我的是TextView动态添加的,但您可以轻松创建一个 for 循环来检索子项:

TextView tv = new TextView(this);
...
gridlayout.addView(tv);

tv.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        removeViewSelected();
    }
});

tv.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        setViewSelected(view);
        return true;
    }
});

在父容器上设置FocusChange监听器:

parentlayout.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        View viewSelected = getViewSelected();
        // if the selected view exists and it lost focus
        if (viewSelected != null && !viewSelected.hasFocus()) {
            // remove it
            removeViewSelected();
        }
    }
});

然后,可选的 custom ViewGroup:它是可选的,因为您可以focusable通过 XML 和clickable侦听器动态设置状态,但对我来说似乎更容易。我使用以下自定义Class作为父容器:

public class ParentLayout extends RelativeLayout implements View.OnClickListener {

    public ParentLayout(Context context) {
        super(context);
        init();
    }

    public ParentLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ParentLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    // handle focus and click states
    public void init() {
        setFocusable(true);
        setFocusableInTouchMode(true);
        setOnClickListener(this);
    }

    // when positioning all children within this 
    // layout, add their focusable state
    @Override
    protected void onLayout(boolean c, int l, int t, int r, int b) {
        super.onLayout(c, l, t, r, b);

        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            child.setFocusable(true);
            child.setFocusableInTouchMode(true);
        }
        // now, even the Button has a focusable state
    }

    // handle the click events
    @Override
    public void onClick(View view) {
        // clear and set the focus on this viewgroup
        this.clearFocus();
        this.requestFocus();
        // now, the focus listener in Activity will handle
        // the focus change state when this layout is clicked
    }
}

例如,这是我使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout ...>

    <com.app.ParentLayout
        android:id="@+id/parent_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">

        <Button
            android:id="@+id/sample_button"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:text="A Simple Button"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"/>

        <android.support.v7.widget.GridLayout
            android:id="@+id/grid_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_above="@id/sample_button" .../>
    </com.app.ParentLayout>

    <android.support.design.widget.FloatingActionButton .../>
</android.support.design.widget.CoordinatorLayout>

希望这会有用。

于 2016-12-28T16:26:09.900 回答
0

使用以下代码:

int last_pos = -1;
GridLayout gridLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gridLayout = (GridLayout) findViewById(R.id.gridLayout);
    int child_count = gridLayout.getChildCount();
    for(int i =0;i<child_count;i++){
        gridLayout.getChildAt(i).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                //Deselect previous
                if(last_pos!=-1) gridLayout.getChildAt(last_pos).setSelected(false);
                //Select the one you clicked
                view.setSelected(true);
                last_pos = gridLayout.indexOfChild(view);
                return false;
            }
        });
    }
    //Remove focus if the parent is clicked
    gridLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            gridLayout.getChildAt(last_pos).setSelected(false);
        }
    });
于 2016-12-28T16:20:13.170 回答