2016-11-17 43 views
0

我面临着一个奇怪的问题,使用SearchViewBottomSheet使用SearchView后底部页面出现意外边距

BottomSheet在使用SearchView后使用时会获得意外的余量。他们的视频解释了问题。

Video

XML

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


<android.support.v7.widget.RecyclerView 
    android:id="@+id/trip_recyclerView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 


<android.support.v4.widget.NestedScrollView 
    android:id="@+id/bottom_sheet" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/backgroundcolor" 
    android:clipToPadding="false" 
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="32dp" 
     android:orientation="vertical" 
     android:padding="12dp"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentStart="true" 
       android:text="Sort &amp; Filter" 
       android:textColor="@color/black" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 

      <TextView 
       android:id="@+id/sheet_done" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentEnd="true" 
       android:drawablePadding="8dp" 
       android:drawableStart="@drawable/ic_done" 
       android:text="Done" 
       android:textSize="18sp" /> 

     </RelativeLayout> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="12dp" 
      android:text="Sort By" /> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="8dp"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 

       <Spinner 
        android:id="@+id/sheet_spinner" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:entries="@array/sort_by" 
        android:padding="8dp" /> 
      </LinearLayout> 

     </android.support.v7.widget.CardView> 


     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="12dp" 
      android:text="Price Range" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="8dp" 
      android:layout_marginEnd="12dp" 
      android:layout_marginStart="12dp" 
      android:layout_marginTop="8dp" 
      android:orientation="horizontal"> 

      <TextView 
       android:id="@+id/sheet_price_min" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

      <TextView 
       android:id="@+id/sheet_price_max" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="end" 
       android:gravity="end" /> 


     </LinearLayout> 

     <SeekBar 
      android:id="@+id/sheet_bar_price" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:thumb="?android:attr/textSelectHandle" /> 

     <TextView 
      android:id="@+id/sheet_price_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:gravity="center" 
      android:text="Rs 1,115" /> 


     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="8dp" 
      android:text="Ratings" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="8dp" 
      android:layout_marginEnd="12dp" 
      android:layout_marginStart="12dp" 
      android:layout_marginTop="8dp" 
      android:orientation="horizontal"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="1" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginStart="12dp" 
       android:layout_weight="1" 
       android:gravity="start" 
       android:text="2" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:text="3" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginEnd="12dp" 
       android:layout_weight="1" 
       android:gravity="end" 
       android:text="4" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="end" 
       android:text="5" /> 


     </LinearLayout> 

     <SeekBar 
      android:id="@+id/sheet_bar_rating" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:max="4" 
      android:thumb="?android:attr/textSelectHandle" /> 


     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="12dp" 
      android:text="Packages" /> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <CheckBox 
       android:id="@+id/sheet_package_gold" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="12dp" 
       android:layout_marginRight="12dp" 
       android:layout_weight="1" 
       android:text="Gold" /> 

      <CheckBox 
       android:id="@+id/sheet_package_diamond" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="12dp" 
       android:layout_marginRight="12dp" 
       android:layout_weight="1" 
       android:text="Diamond" /> 


     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <CheckBox 
       android:id="@+id/sheet_package_silver" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="12dp" 
       android:layout_marginRight="12dp" 
       android:layout_weight="1" 
       android:text="Silver" /> 

      <CheckBox 
       android:id="@+id/sheet_package_platinum" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="12dp" 
       android:layout_marginRight="12dp" 
       android:layout_weight="1" 
       android:text="Platinum" /> 

     </LinearLayout> 
    </LinearLayout> 
</android.support.v4.widget.NestedScrollView> 
</android.support.design.widget.CoordinatorLayout> 

活动

public class TripsActivity extends AppCompatActivity implements 
    SearchView.OnQueryTextListener, SeekBar.OnSeekBarChangeListener, View.OnClickListener { 

public String MAX = "150000"; 
public String MIN = "0"; 
@BindView(R.id.trip_recyclerView) 
RecyclerView recyclerView; 
@BindView(R.id.bottom_sheet) 
View view; 
@BindView(R.id.sheet_spinner) 
Spinner spinner; 
@BindView(R.id.sheet_price_min) 
TextView minPrice; 
@BindView(R.id.sheet_price_max) 
TextView maxPrice; 
@BindView(R.id.sheet_price_text) 
TextView currentPrice; 
@BindView(R.id.sheet_bar_price) 
SeekBar priceBar; 
@BindView(R.id.sheet_bar_rating) 
SeekBar ratingBar; 
@BindView(R.id.sheet_package_gold) 
CheckBox packageGold; 
@BindView(R.id.sheet_package_silver) 
CheckBox packageSilver; 
@BindView(R.id.sheet_package_platinum) 
CheckBox packagePlatinum; 
@BindView(R.id.sheet_package_diamond) 
CheckBox packageDiamond; 

BottomSheetBehavior.BottomSheetCallback callback = new BottomSheetBehavior.BottomSheetCallback() { 
    @Override 
    public void onStateChanged(@NonNull View bottomSheet, int newState) { 
    } 

    @Override 
    public void onSlide(@NonNull View bottomSheet, float slideOffset) { 

     if (slideOffset < 0.4) 
      getSupportActionBar().show(); 
     else 
      getSupportActionBar().hide(); 

    } 

}; 
private SearchView searchView; 
private TripsAdapter adapter; 
private BottomSheetBehavior mBottomSheetBehavior; 

@OnClick(R.id.sheet_done) 
public void onClick(View view) { 
    switch (view.getId()) { 
     case R.id.sheet_done: 
      mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 
      break; 
    } 
} 


@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_trips_activity); 
    ButterKnife.bind(this); 


    mBottomSheetBehavior = BottomSheetBehavior.from(view); 
    mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); 
    mBottomSheetBehavior.setPeekHeight(0); 
    mBottomSheetBehavior.setBottomSheetCallback(callback); 
} 


@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (item.getItemId() == android.R.id.home) { 
     if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) 
      mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 
     else 
      this.finish(); 

    } else if (item.getItemId() == R.id.filterOptions) 
     mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); 

    return super.onOptionsItemSelected(item); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    getMenuInflater().inflate(R.menu.seach_meanu, menu); 

    final MenuItem searchItem = menu.findItem(R.id.action_search); 
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem); 

    searchView.setOnQueryTextListener(this); 

    return true; 
} 

@Override 
public boolean onQueryTextSubmit(String query) { 
    adapter.filter(query); 
    return true; 
} 

@Override 
public boolean onQueryTextChange(String newText) { 
    adapter.filter(newText); 
    return true; 
} 

@Override 
public void onBackPressed() { 
    if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) 
     mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 
    else 
     super.onBackPressed(); 
} 


} 

有人可以帮我找出问题所在。谢谢

+0

如何搜索查看被打开? –

+0

我只是在听'onQueryTextChange'。我没有实现如何打开searchview。 –

+0

那么,它可以帮助解决问题,如果提供了关于如何打开/关闭SearchView的代码。 –

回答

1

将此添加到您的清单。这个问题由软键盘触发。很难找到问题的真正根源。

<activity> 
    android:name=.. 
    android:windowSoftInputMode="adjustPan"> 

我得到这个来自:https://stackoverflow.com/a/12956520/7202257

+0

我还是不明白BottomSheet与'SoftKeyboard'的关系,但是这个技巧。谢谢 –