Can't deselect buttons using a custom layout

2019-06-09 17:36发布

I'm implementing a container with 3 buttons, added dynamically. When I select one, I want that the other ones are deselected (change drawable). This works when I add the buttons directly to a LinearLayout, which I declared in XML. But when I create a custom layout, which extends LinearLayout, and put my buttons there, it doesn't work anymore (although the code is almost the same). I select one button and the code to deselect the other ones is executed, but they still show the drawable of selected state.

Edit: I always can select the buttons, in both cases. Selection works. What doesn't work in the custom view case is de -selecting the views, or at least the drawable is not changed when the views are deselected.

I reduced the code to the minimal necessary to see the problem.

The activity:

public class TestActivity extends Activity {

private View[] views;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

/////////////////////////////////////////////////////////////////
//uncomment this block to see the working code (without custom layout)
//      views = new View[3];
//      LinearLayout root = (LinearLayout) findViewById(R.id.container);
//      findViewById(R.id.myCustomLayout).setVisibility(View.GONE);
//      addButton(root, 0);
//      addButton(root, 1);
//      addButton(root, 2);
//////////////////////////////////////////////////////////////////
}

private void addButton(final ViewGroup root, final int position) {
    View v = new View(this);
    LinearLayout.LayoutParams viewLayoutPars = new LinearLayout.LayoutParams(150, 200);
    v.setLayoutParams(viewLayoutPars);

    v.setBackgroundResource(R.drawable.mybg);

    if (position == 0) {
        v.setSelected(true);
    }

    root.addView(v);

    views[position] = v;

    v.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            for (View view : views) {
                view.setSelected(false);
            }
            v.setSelected(true);
            root.invalidate();
        }
    });
}

}

The layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>

    <com.test.MyCustomLayout
        android:id="@+id/myCustomLayout"
        xmlns:dg="http://schemas.android.com/apk/res/com.test"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

My custom layout:

public class MyCustomLayout extends LinearLayout {
private Context context;

private View[] views;

public MyCustomLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;

    views = new View[3];
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);

    addButton(0);
    addButton(1);
    addButton(2);
}

private void addButton(final int position) {
    View v = new View(context);
    LinearLayout.LayoutParams viewLayoutPars = new LinearLayout.LayoutParams(150, 200);
    v.setLayoutParams(viewLayoutPars);

    v.setBackgroundResource(R.drawable.mybg);

    if (position == 0) {
        v.setSelected(true);
    }

    addView(v);

    views[position] = v;

    v.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            for (View view : views) {
                view.setSelected(false);
            }
            v.setSelected(true);
            invalidate();
        }
    });
}
}

The drawable (mybg):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/selected" />
    <item android:drawable="@drawable/not_selected" />
</selector>

The images are 2 harmless squares selected.png and not_selected.png with different color.

Thanks in advance.

0条回答
登录 后发表回答