欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

最详尽的DrawerLayout用法

程序员文章站 2022-06-30 14:17:59
...

MainActivity:

public class Main7Activity extends Activity {
    private MyAdapter adapter;
    private ListView listView;
    private List<Use> mList = new ArrayList<Use>();
    private Context mContext = this;//上下文不可少
    private DrawerLayout mDrawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main7);
        listView=(ListView)findViewById(R.id.listview1);
        addDATA();
        adapter=new MyAdapter(mContext,mList);
        listView.setAdapter(adapter);
        mDrawer=(DrawerLayout)findViewById(R.id.draw);

        mDrawer.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                if (drawerView.getTag().equals("left")) {
                    View content = mDrawer.getChildAt(0);
                    System.out.println("xxxxxxxxxx");
                    int offset = (int) (drawerView.getWidth() * slideOffset);
                    content.setTranslationX(offset);
                    content.setScaleX(1 - slideOffset * 0.5f);
                    content.setScaleY(1 - slideOffset * 0.5f);
                }
            }
            @Override
            public void onDrawerOpened(View drawerView) {
            }
            @Override
            public void onDrawerClosed(View drawerView) {
            }
            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }
    private void addDATA() {
        Use u1=new Use("发送给对方水电费讽德诵功","上的风格都是告诉对方公司的风格");
        Use u2=new Use("发送给对方水电费讽德诵功","上的风格都是告诉对方公司的风格");
        Use u3=new Use("发送给对方水电费讽德诵功","上的风格都是告诉对方公司的风格");
        Use u4=new Use("发送给对方水电费讽德诵功","上的风格都是告诉对方公司的风格");
        Use u5=new Use("发送给对方水电费讽德诵功","上的风格都是告诉对方公司的风格");
        Use u6=new Use("发送给对方水电费讽德诵功","上的风格都是告诉对方公司的风格");
        Use u7=new Use("发送给对方水电费讽德诵功","上的风格都是告诉对方公司的风格");
        Use u8=new Use("发送给对方水电费讽德诵功","上的风格都是告诉对方公司的风格");
        mList.add(u1);
        mList.add(u2);
        mList.add(u3);
        mList.add(u4);
        mList.add(u5);
        mList.add(u6);
        mList.add(u7);
        mList.add(u8);

    }
}

MainActivity.xml:

<android.support.v4.widget.DrawerLayout 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:id="@+id/draw"
    tools:context="com.fae.mobile.activity.Main7Activity"
    >
    <include layout="@layout/activity_main4"/>
    <LinearLayout
        android:tag="left"   //不可缺失
       android:layout_gravity="left"
        android:layout_width="350dp"
        android:layout_height="match_parent"
        android:id="@+id/fragment"
        android:orientation="vertical"
        android:background="@color/white">
       <TextView
            android:id="@+id/Text"
           android:layout_gravity="center"
           android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="筛选"
            android:textSize="25sp" />
        <ListView
            android:layout_marginTop="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@id/listview1"></ListView>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

MyAdapter:

public class MyAdapter extends BaseAdapter {
    private Context mcontext;
    private List<Use> mList;
    public MyAdapter(Context mcontext,List<Use> list) {
        this.mcontext = mcontext;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }
    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final InfoViewHold mInfoViewHold;
        if (convertView == null) {
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.item_layout, null);
            mInfoViewHold = new InfoViewHold(convertView);
          convertView.setTag(mInfoViewHold);
        } else {
            mInfoViewHold = (InfoViewHold) convertView.getTag();
        }
     mInfoViewHold.persion .setText(mList.get(position).getPersion());
     mInfoViewHold.speak  .setText(mList.get(position).getSpeak());
                   return convertView;
    }
    public class InfoViewHold {
        private TextView persion,speak;
        public InfoViewHold(View convertView) {
            persion = (TextView) convertView.findViewById(R.id.testView);
            speak = (TextView) convertView.findViewById(R.id.testView2);
        }
    }
}

Item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
       <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/testView"
        android:maxLines="1"
        android:textSize="20dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/testView2"
            android:textSize="10dp"
            android:maxLines="1"/>
    </LinearLayout>


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
           android:text="控件1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
            android:id="@+id/mbutton1"
            android:layout_alignParentRight="true"
            android:maxLines="1"/>

    </RelativeLayout>
</LinearLayout>

最详尽的DrawerLayout用法
最详尽的DrawerLayout用法