简而言之,使用ViewPager
作为BottomSheetFragment
的根元素,并使用片段的getChildSupportFragmentManager();
初始化寻呼机适配器
试试这个
MapsActivity
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private final Integer[] IMAGES = {R.drawable.nilesh, R.drawable.nilesh, R.drawable.nilesh, R.drawable.nilesh,
R.drawable.nilesh, R.drawable.nilesh, R.drawable.nilesh, R.drawable.nilesh};
private ArrayList<Integer> arrayList = new ArrayList<Integer>();
RecyclerView myRecyclerView;
private BottomSheetBehavior bottomSheetBehavior;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Collections.addAll(arrayList, IMAGES);
bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottomSheetLayout));
myRecyclerView=findViewById(R.id.myRecyclerView);
myRecyclerView.setLayoutManager(new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL,false));
myRecyclerView.setHasFixedSize(true);
myRecyclerView.setAdapter(new DataAdapter(this,arrayList));
SnapHelper helper = new PagerSnapHelper();
helper.attachToRecyclerView(myRecyclerView);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
// bottomSheetHeading.setText(getString(R.string.text_collapse_me));
} else {
// bottomSheetHeading.setText(getString(R.string.text_expand_me));
}
switch (newState) {
case BottomSheetBehavior.STATE_COLLAPSED:
Log.e("Bottom Sheet Behaviour", "STATE_COLLAPSED");
break;
case BottomSheetBehavior.STATE_DRAGGING:
Log.e("Bottom Sheet Behaviour", "STATE_DRAGGING");
break;
case BottomSheetBehavior.STATE_EXPANDED:
Log.e("Bottom Sheet Behaviour", "STATE_EXPANDED");
break;
case BottomSheetBehavior.STATE_HIDDEN:
Log.e("Bottom Sheet Behaviour", "STATE_HIDDEN");
break;
case BottomSheetBehavior.STATE_SETTLING:
Log.e("Bottom Sheet Behaviour", "STATE_SETTLING");
break;
}
}
@Override
public void onSlide(View bottomSheet, float slideOffset) {
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
layout.activity_maps
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomSheetLayout"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:padding="16dp"
app:behavior_peekHeight="60dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
DataAdapter的
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private Context context;
private ArrayList<Integer> arrayList = new ArrayList<Integer>();
public DataAdapter(Context context, ArrayList<Integer> imagesArray) {
this.context = context;
arrayList = imagesArray;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View imageLayout = LayoutInflater.from(context).inflate(R.layout.aa, parent, false);
return new ViewHolder(imageLayout);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.imageView.setImageResource(arrayList.get(position));
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView
.findViewById(R.id.profile_image);
}
}
}
layout.aa
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="120dp"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="#919191"
app:civ_border_color="#FF000000"
app:civ_border_width="2dp" />
</LinearLayout>
产出
底部的STATE_COLLAPSED
底部的STATE_EXPANDED
有没有办法将多个底页连接到一个可以滑动的视图
无需添加多个Bottomsheets只需添加一个BottomSheetFragment
并在此片段中添加ViewPager
>在ViewPager
适配器广告数据作为您的要求
听到官方的android网站url更多关于bottomsheetDialogfragment可以帮助你的https://developer.android.com/reference/android/support/design/widget/BottomSheetDialogFragment
这是简单的例子https://medium.com/@mujtahidah/bottom-sheet-dialog-fragment-android-6ac7dc36e6ac
我希望它有帮助