android spinner无法在Bottom Sheet中工作

时间:2016-04-21 12:01:21

标签: android spinner bottom-sheet

以下是我的MainActivity。

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private BottomSheetBehavior bottom_sheet_checkbox;
    private Button btnCity;
    private Toolbar toolbar;
    private Spinner sprCity;
    private ArrayList<SprModel> alCity;
    private SpinnerAdapter spinnerAdapter;

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

        getId();
        setSupportActionBar(toolbar);
        setListner();
        loadSpinner();
    }
    private void loadSpinner() {
        alCity = new ArrayList<SprModel>();
        alCity.add(new SprModel("Ahmedabad"));
        alCity.add(new SprModel("Vadodara"));
        alCity.add(new SprModel("Surat"));
        alCity.add(new SprModel("Mumbai"));

        spinnerAdapter = new SpinnerAdapter(MainActivity.this,alCity);
        sprCity.setAdapter(spinnerAdapter);
        spinnerAdapter.notifyDataSetChanged();
    }
    private void getId() {
        try {
            try {
                bottom_sheet_checkbox = BottomSheetBehavior.from(findViewById(R.id.bottomSheet_checkbox));
                toolbar = (Toolbar) findViewById(R.id.toolbar);
                btnCity = (Button)findViewById(R.id.btnCity);
                sprCity = (Spinner)findViewById(R.id.sprCity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void setListner() {
        try {
            btnCity.setOnClickListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onClick(View v) {
        try {
            switch (v.getId()) 
            {
            case R.id.btnCity:
                    bottom_sheet_checkbox.setState(BottomSheetBehavior.STATE_EXPANDED);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我的activity_main.xml

     <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/LayOut"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="android.com.bottomsheets.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            />

    </android.support.design.widget.AppBarLayout>

    <!--it is used to add one more layout on same layout.-->
    <include layout="@layout/content_main" />
    <include layout="@layout/bottom_sheet_checkbox" />

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

这里是content_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center_horizontal"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnCity"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Select city"
        android:textAllCaps="false"
        />
</LinearLayout>

这里定义了微调器。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottomSheet_checkbox"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="@android:color/darker_gray"
    android:padding="@dimen/activity_vertical_margin"
    app:behavior_hideable="true"
    app:behavior_peekHeight="60dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

   <Spinner
       android:id="@+id/sprCity"
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:spinnerMode="dropdown"
       ></Spinner>
</RelativeLayout>

我已为Design support library实施了Bottom sheet,我希望在自定义布局上实现微调器。我正在使用arraylist来成功加载微调器和微调器,但是当我尝试选择任何微调器项时,它只显示第一个或默认项,因此我无法从微调器中选择任何项。

1 个答案:

答案 0 :(得分:0)

我注意到这一点,在我的情况下,所选微调器的值将在edittext中发布。经过一些研究后,我决定在编辑文本上改变焦点时调用ongetselected

如图所示

   spinnerDocumentType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    textInputEditTextDocumentType.setText(documentType[i]);

                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
                }
            });

设置我的编辑文本

 textInputEditTextDocumentType.setOnTouchListener(new View.OnTouchListener()
            {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    spinnerDocumentType.performClick();

                    return false;
                }
            });

更甜的部分是

   textInputEditTextDocumentType.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if ( spinnerDocumentType.getCount() > 0 )
                    {
                        spinnerDocumentType.getOnItemSelectedListener()
                                .onItemSelected( spinnerDocumentType, null, spinnerDocumentType.getSelectedItemPosition(), 0 );
                        Log.e(TAG,"on touch spinner document");
                        String a=spinnerDocumentType.getSelectedItem().toString();
                        textInputEditTextDocumentType.setText(a);

                    }

                }
            });

仍然处于活动开发状态,导致在焦点更改时发生微调器更改,而不是在选择微调器时