DrawerLayout侧滑菜单的布局,注意事项及代码

整体的布局里,先写主背景的布局,再写侧滑页面的布局




    
    

        

        

            

            

            

            
        
    
    
    

        

            

            
        

        
    
侧滑页面listview的布局



    
    

ThirdActivity里面 给侧滑页面添加内容 给主页面添加内容,fragment

public class ThirdActivity extends AppCompatActivity {

    private DrawerLayout drawer_gen;
    private LinearLayout rela_zhu;
    private LinearLayout linear_cemian;
    private LinearLayout linear_weidenglu;
    private ListView listView;
    private FrameLayout frameLayout;
    private RadioGroup group;

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

        //进入页面先显示新闻的fragment布局
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new Fragment_News()).commit();
        //根布局
        drawer_gen = (DrawerLayout) findViewById(R.id.drawer_gen);
        //主背景
        rela_zhu = (LinearLayout) findViewById(R.id.linear_zhu);
        //主背景中的framelayout
        frameLayout = (FrameLayout) findViewById(R.id.frame_layout);
        //侧滑页面
        linear_cemian = (LinearLayout) findViewById(R.id.linear_cemian);
        //侧滑页面的未登录布局
        linear_weidenglu = (LinearLayout) findViewById(R.id.linear_weidenglu);
        //侧滑页面下面的listview
        listView = (ListView) findViewById(R.id.ce_list_view);
        //group
        group = (RadioGroup) findViewById(R.id.group);

        final List listce = new ArrayList<>();
        listce.add("微信登录");
        listce.add("QQ登录");
        listce.add("微博登录");

        CeHuaAdapter ceHuaAdapter = new CeHuaAdapter(listce, ThirdActivity.this);
        listView.setAdapter(ceHuaAdapter);

        //侧滑listview点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                //点击条目吐司展示
                Toast.makeText(ThirdActivity.this,listce.get(i),Toast.LENGTH_SHORT).show();
                //关闭抽屉
                drawer_gen.closeDrawer(linear_cemian);
            }
        });

        //点击上面的未登录布局,跳转页面打开相册
        linear_weidenglu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawer_gen.closeDrawer(linear_cemian);
                Intent intent = new Intent(ThirdActivity.this, XiangjiActivity.class);
                startActivity(intent);
            }
        });

        //替换布局显示
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                switch (checkedId){
                    case R.id.radio_xinwen:
                        transaction.replace(R.id.frame_layout,new Fragment_News());
                        break;
                    case R.id.radio_gridView:
                        transaction.replace(R.id.frame_layout,new Fragment_GridView());
                        break;
                    case R.id.radio_faxian:
                        transaction.replace(R.id.frame_layout,new Fragment_News());
                        break;
                    case R.id.radio_wode:
                        transaction.replace(R.id.frame_layout,new Fragment_News());
                        break;
                }

                transaction.commit();
            }
        });
    }
}
侧滑页面listview的适配器

public class CeHuaAdapter extends BaseAdapter{
    List listce;
    Context context;
    public CeHuaAdapter(List listce, Context context) {
        this.listce = listce;
        this.context = context;
    }

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

    @Override
    public Object getItem(int i) {
        return listce.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view==null){
            view = View.inflate(context, R.layout.ce_list_item,null);

            holder = new ViewHolder();

            holder.textView = (TextView) view.findViewById(R.id.ce_list_text);

            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }
        holder.textView.setText(listce.get(i));
        return view;
    }
    class ViewHolder{
        TextView textView;
    }
}

你可能感兴趣的:(DrawerLayout侧滑菜单的布局,注意事项及代码)