DrawLayout侧拉

<1>、xml布局代码
<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/dl">

<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent">

</FrameLayout>

<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="@+id/lv"
android:background="#00ff00"
android:layout_gravity="start"
android:choiceMode="singleChoice"></ListView>

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

<2>、MainActivity代码
public class MainActivity extends AppCompatActivity {

privateList<String>list;
privateDrawerLayoutdl;

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

initDate();

dl= (DrawerLayout) findViewById(R.id.dl);
FrameLayout fl= (FrameLayout) findViewById(R.id.fl);
finalListView lv= (ListView) findViewById(R.id.lv);

lv.setAdapter(newMyAdapter());

lv.setOnItemClickListener(newAdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,intposition,longid) {
//String string= (String) parent.getItemAtPosition(position);
ContentFragment contentFragment =newContentFragment();
Bundle bundle=newBundle();
bundle.putString("name",list.get(position));
contentFragment.setArguments(bundle);

getSupportFragmentManager().beginTransaction().replace(R.id.fl,contentFragment).commit();

dl.closeDrawer(lv);


}
});

}

private void initDate() {

list= new ArrayList<>();
for(inti=0;i<5;i++){

list.add("item"+i);
}
}


class MyAdapter extends BaseAdapter{


@Override
public int getCount() {
returnlist.size();
}

@Override
public Object getItem(intposition) {
return null;
}

@Override
public long getItemId(intposition) {
return0;
}

@Override
public View getView(intposition, View convertView, ViewGroup parent) {

TextView tv =new TextView(MainActivity.this);
tv.setText(list.get(position));
tv.setTextSize(25);
returntv;
}
}
}

<3>、Fragment类代码
public classContentFragmentextendsFragment{

@Nullable
@Override
publicView onCreateView(LayoutInflater inflater,@NullableViewGroup container,@NullableBundle savedInstanceState) {

Bundle bundle = getArguments();
String name = bundle.getString("name");
TextView textView =newTextView(getActivity());
textView.setText(name);
returntextView;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值