将“选定”视图存储为全局变量并在其焦点发生变化时将其删除怎么样?focusable
通过与,focusableInTouchMode
和onClick
listeners一起玩,您可以获得正确的结果。我不确定这是最好的解决方案,但它确实有效。
你需要什么:
- 全局 View 变量:
GridLayout
的子项长时间单击,如选中。
- (可选) 作为 any的自定义父容器
ViewGroup
:它将在其所有子级[*]上设置可聚焦的侦听器。在我的测试中,我使用了 aLinearLayout
和 a RelativeLayout
。
[*] 如果不使用可选的父自定义类,则必须在父 ViewGroup 的所有子级上设置android:focusable="true"
和。android:focusableInTouchMode="true"
并且您必须设置OnClickListener
才能在removeViewSelected()
单击父 ViewGroup 时调用。
- 为子级添加Click 侦听
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
孩子上,添加Click
和LongClick
侦听器以更新或删除选定的视图。我的是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>
希望这会有用。