1

我在我的应用程序中以稍微不同的方式实现了导航抽屉。但抽屉开合不顺畅。它介于两者之间。在 Google 应用程序中,导航抽屉非常流畅。我如何实现它?这个问题已经在这里问过了,但它不符合我的需求。请帮忙!

XML

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Swipe from the left to open the drawer"
        android:id="@+id/textView"
        android:layout_gravity="center" />
</FrameLayout>

<LinearLayout
    android:id="@+id/left_drawer_layout"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_marginLeft="-65dp"
    android:background="#111"
    android:orientation="vertical"
    android:layout_gravity="start" >

    <EditText
        android:id="@+id/searchbar"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:textColor="#bfc2d1"
        android:singleLine="true"
        android:padding="10dp"
        android:background="@drawable/search_bar"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:imeOptions="flagNoExtractUi"
        android:hint="Search" >
    </EditText>

<RelativeLayout
    android:id="@+id/empty"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_marginLeft="-65dp"
    android:background="#111"
    android:orientation="vertical"
    android:layout_gravity="start" >
    <TextView
        android:id="@+id/empty_text"
        android:text="@string/notfound"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:textColor="#ffababab"
        android:textSize="15dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:gravity="center">
    </TextView>
    </RelativeLayout>

    <ListView android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
 </LinearLayout>

</android.support.v4.widget.DrawerLayout>

爪哇

public class MainActivity extends FragmentActivity {
final String[] data ={"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Flourine","Noen","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium",
        "Iodine","Xenon","Caesium","Barium","Lanthanum","Cerium","Praseodymium","Neodymium","Promethium","Samarium","Europium","gadoliium","Terbium","Dysprosium","Holmium","Erbium","Thulium","Ytterbium","Lutetium","Hafnium","Tantalum","Tungsten","Rhenium","Osmium","Iridium","Platinum","Gold","Mercury","Thallium","Lead","Bismuth","Polonium","Astatine","Radon","Francium","Radium","Actinium","Thorium","Protactinium","Uranium","Neptunium","Plutonium","Americium","Curium","Berkelium","Californium","Einstenium","Fermium","Mendelevium","Nobelium","Lawrencium","Rutherfordium","Dubnium","Seaborgium","Bohrium","Hassium",
        "Meitnerium","Darmstadtium","Roentgenium","Copernicium","Ununtrium","Ununquadium","Ununpentium","Ununhexium","Ununseptium","Ununoctium"};

ArrayAdapter<String> adapter;

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

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
    final EditText searchBar = (EditText) findViewById(R.id.searchbar);
    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    final ListView navList = (ListView) findViewById(R.id.left_drawer);
    final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
    navList.setAdapter(adapter);


    navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
            drawer.closeDrawer(linearLayout);
        }
    });

    //filter list view after search instantly
    searchBar.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            String searchedquery = cs.toString().replaceAll(" ", "");
            MainActivity.this.adapter.getFilter().filter(searchedquery);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {

        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

    //hide the keyboard after search on touch list view
    navList.setOnScrollListener(new AbsListView.OnScrollListener() {
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            //hide keyboard
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
        }

        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        }
    });

    navList.setEmptyView(findViewById(R.id.empty));

 }

}
4

1 回答 1

0

这是使滑动平滑的方法

  1. 单独创建导航抽屉的布局
  2. 从片段中调用抽屉

这是更好理解的代码

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

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/custom_drawer_layout">

    <fragment 
//this contains the layout of your fragment
        android:id="@+id/drawer_fragement"
        android:layout_width="260dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:name="adress to navigationdrawer class"
        tools:layout="@layout/drawer_frag
//this line tells which layout to inflate ontop of the fragment
        />
</android.support.v4.widget.DrawerLayout>

</LinearLayout>

现在创建一个名称为drawer_frag的布局,其中包含您要在抽屉中显示的所有代码

希望能帮助到你

于 2015-04-11T12:46:00.850 回答