许多App界面都有侧拉的显示,下面就是一些简单的侧拉,点击当中的list条目,则更改Fragment中的textview
下面是布局文件 DrawerLayout需放到最大的布局 只要布局写出来就有侧拉效果
<android.support.v4.widget.DrawerLayout android:id="@+id/drawer" 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" tools:context="com.example.drawerlayout.MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment" ></FrameLayout> <ListView android:layout_width="200dp" android:layout_height="match_parent" android:choiceMode="singleChoice" android:layout_gravity="left" android:id="@+id/listview" android:background="#ff3600" ></ListView> </android.support.v4.widget.DrawerLayout>
Activity代码文件
import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.FrameLayout; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private DrawerLayout drawer; private ListView listview; private FrameLayout frame; private ArrayList<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 initView(); //初始化数据 initData(); //监听事件 setListener(); } private void setListener() { listview.setAdapter(new MyAdapter()); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { MyFragment myFragment = new MyFragment(); Bundle bundle = new Bundle(); bundle.putString("name",list.get(position)); myFragment.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.fragment,myFragment).commit(); //关闭drawer drawer.closeDrawer(listview); } }); } private void initData() { list = new ArrayList<>(); for (int i=0;i<5;i++){ list.add("杨栓"+i); } } private void initView() { drawer = (DrawerLayout) findViewById(R.id.drawer); listview = (ListView) findViewById(R.id.listview); frame = (FrameLayout) findViewById(R.id.fragment); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView textView = new TextView(MainActivity.this); textView.setTextSize(25); textView.setText(list.get(position)); return textView; } } }
Fragment文件
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Created by xsj on 2017/9/7. */ public class MyFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); Bundle arguments = getArguments(); String name = arguments.getString("name"); textView.setText(name); return textView; } }