最详尽的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>
上一篇: 不带这么碰瓷的
推荐阅读
-
Android中AlertDialog四种对话框的最科学编写用法(实例代码)
-
DrawerLayout、Toolbar 的详细用法
-
最详尽的DrawerLayout用法
-
python中yield的用法详解——最简单,最清晰的解释
-
Vue中jsx的最简单用法
-
最完的htaccess文件用法收集整理
-
excel数据分析软件工具有哪些(最实用的5个分析工具及用法)
-
excel数据分析软件工具有哪些(最实用的5个分析工具及用法)
-
史上最简单的Spring Security教程(十):AuthenticationFailureHandler高级用法
-
史上最简单的Spring Security教程(八):用户登出成功LogoutSuccessHandler高级用法