如何在 GridLayout 中选择 child 类似于 GridView
How to get selected child in GridLayout similar to GridView
我想达到以下能力:
- Select 每次长按
GridLayout
里面只有一个 child View
。
- 单击视觉层次结构中的
GridLayout
或任何祖先 parent 将取消选中 child View
如果已经选中。
问题是当向 child View
注册 View.OnLongClickListener
回调时,parent GridLayout
和任何祖先注册的回调(View.OnClickListener
或 View.onTouchEvent
) 在点击它们时调用。
如何在类似于 AdapterView.OnItemSelectedListener
或 AdapterView.OnItemLongClickListener
的 GridLayout
中选择一个 child 并解决上述问题?
使用以下代码:
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);
}
});
如何将 "selected" 视图存储为全局变量,并在其焦点改变时将其删除?通过使用 focusable
、focusableInTouchMode
和 onClick
听众,您可以获得正确的结果。我不确定这是最好的解决方案,但它确实有效。
您将需要:
- A global View variable: the
GridLayout
's child long clicked, as selected.
- (optional) A custom parent container as any
ViewGroup
: it will set the focusable listeners on all its children [*]. In my tests, I used a LinearLayout
and a RelativeLayout
.
[*] 如果您不使用可选的 parent 自定义 Class,则必须对所有设置 android:focusable="true"
和 android:focusableInTouchMode="true"
children 属于 parent ViewGroup。您必须设置 OnClickListener
才能在单击 parent ViewGroup 时调用 removeViewSelected()
。
- Adding Click listeners for
GridLayout
children: which updates the selected view.
- Implementing a Focus listener: which removes the selected view if it's losing focus.
它将处理 parent 和 child 层次结构上的所有焦点更改状态,请参阅输出:
我使用了以下模式:
CoordinatorLayout --- simple root group
ParentLayout --- aka "parentlayout"
Button --- simple Button example
GridLayout --- aka "gridlayout"
FloattingActionButton --- simple Button example
让我们在 Activity
中准备选定的 View
及其更新方法:
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
child 上,添加 Click
和 LongClick
侦听器以更新或删除所选视图。我的 TextView
是动态添加的,但您可以轻松创建 for-loop 来检索 children:
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;
}
});
在 parent 容器上设置 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();
}
}
});
然后,可选的自定义 ViewGroup
:它是可选的,因为您可以通过 XML 和 clickable
侦听器动态设置 focusable
状态,但它似乎更容易我。我使用以下自定义 Class
作为 parent 容器:
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>
希望这会有所帮助。
我想达到以下能力:
- Select 每次长按
GridLayout
里面只有一个 childView
。 - 单击视觉层次结构中的
GridLayout
或任何祖先 parent 将取消选中 childView
如果已经选中。
问题是当向 child View
注册 View.OnLongClickListener
回调时,parent GridLayout
和任何祖先注册的回调(View.OnClickListener
或 View.onTouchEvent
) 在点击它们时调用。
如何在类似于 AdapterView.OnItemSelectedListener
或 AdapterView.OnItemLongClickListener
的 GridLayout
中选择一个 child 并解决上述问题?
使用以下代码:
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);
}
});
如何将 "selected" 视图存储为全局变量,并在其焦点改变时将其删除?通过使用 focusable
、focusableInTouchMode
和 onClick
听众,您可以获得正确的结果。我不确定这是最好的解决方案,但它确实有效。
您将需要:
- A global View variable: the
GridLayout
's child long clicked, as selected.- (optional) A custom parent container as any
ViewGroup
: it will set the focusable listeners on all its children [*]. In my tests, I used aLinearLayout
and aRelativeLayout
.
[*] 如果您不使用可选的 parent 自定义 Class,则必须对所有设置 android:focusable="true"
和 android:focusableInTouchMode="true"
children 属于 parent ViewGroup。您必须设置 OnClickListener
才能在单击 parent ViewGroup 时调用 removeViewSelected()
。
- Adding Click listeners for
GridLayout
children: which updates the selected view.- Implementing a Focus listener: which removes the selected view if it's losing focus.
它将处理 parent 和 child 层次结构上的所有焦点更改状态,请参阅输出:
我使用了以下模式:
CoordinatorLayout --- simple root group
ParentLayout --- aka "parentlayout"
Button --- simple Button example
GridLayout --- aka "gridlayout"
FloattingActionButton --- simple Button example
让我们在 Activity
中准备选定的 View
及其更新方法:
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
child 上,添加 Click
和 LongClick
侦听器以更新或删除所选视图。我的 TextView
是动态添加的,但您可以轻松创建 for-loop 来检索 children:
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;
}
});
在 parent 容器上设置 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();
}
}
});
然后,可选的自定义 ViewGroup
:它是可选的,因为您可以通过 XML 和 clickable
侦听器动态设置 focusable
状态,但它似乎更容易我。我使用以下自定义 Class
作为 parent 容器:
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>
希望这会有所帮助。