下面是我的 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 成功加载微调器和微调器加载,但是当我尝试选择微调器的任何项目时,它只显示第一个或默认项目,因此我无法从微调器中选择任何项目。