侧边栏 与 动态加载 fragment


package com.example.monikaoshi;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.example.monikaoshi.base.MyListBase;
import com.example.monikaoshi.fragment.FragmentSy;
import com.example.monikaoshi.fragment.FragmentSz;
import com.example.monikaoshi.fragment.FragmentWd;
import com.example.monikaoshi.fragment.FragmentZx;
import com.example.monikaoshi.gson.MyGson_List;
import com.google.gson.Gson;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class Activity_DongTai extends AppCompatActivity {

    private LinearLayout linear_zhu;
    private LinearLayout linear_ce;
    private RadioGroup group;
    private ListView listView;
    private LinearLayout linece;
    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__dong_tai);
        //初始化控件
        initView();
        //请求数据
        requestDtatFunction();

--------------------------------------------------------------------------------------
侧边栏
 //添加条目点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //用drawerLayout布局关闭侧边栏布局
                drawerLayout.closeDrawer(linece);//点击关闭侧边栏
                //跳转页面
                Intent intent = new Intent(Activity_DongTai.this,MainViewListView.class);
                startActivity(intent);
            }
        });
    }
    //请求数据
    private void requestDtatFunction() {
        new MyTask().execute("http://api.tianapi.com/tiyu/?key=55acaf346503d8d5f98af8dec90ceb01&num=10");
    }

    private void initView() {
        //获取资源ID
        linear_zhu = findViewById(R.id.linear_zhu);//主页面
        group = findViewById(R.id.group);
        listView = findViewById(R.id.listView);
        linece = findViewById(R.id.linear_ce);//侧边栏
        drawerLayout = findViewById(R.id.drawer);//drawerLayout布局

        linear_ce = findViewById(R.id.linear_ce);//侧边栏
-------------------------------------------------------------------------------------------
动态加载
         getFragmentS(new FragmentSy());//默认加载首页
        //添加点击事件
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.btn_sy:
                        getFragmentS(new FragmentSy());
                        break;
                    case R.id.btn_zx:
                        getFragmentS(new FragmentZx());
                        break;
                    case R.id.btn_sz:
                        getFragmentS(new FragmentSz());
                        break;
                    case R.id.btn_wd:
                        getFragmentS(new FragmentWd());
                        break;
                }
            }
        });

    }
    //动态加载
    private  void getFragmentS(Fragment fragment){
        FragmentManager manager = getSupportFragmentManager();
        //打开一个事物
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.replace(R.id.frame,fragment);
        fragmentTransaction.commit();//提交
    }

    class MyTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setConnectTimeout(5000);
                urlConnection.setConnectTimeout(5000);
                if(urlConnection.getResponseCode() == 200){
                    InputStream inputStream = urlConnection.getInputStream();
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    int len;
                    byte[] b = new byte[1024];
                    while ((len = inputStream.read(b))!=-1){
                        outputStream.write(b,0,len);
                    }
                    return outputStream.toString();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            MyGson_List myGson_list = gson.fromJson(s, MyGson_List.class);
            List<MyGson_List.NewslistBean> newslist = myGson_list.getNewslist();
            //创建适配器
            MyListBase base = new MyListBase(Activity_DongTai.this,newslist);
            listView.setAdapter(base);

        }
    }
}

--------------------------------------------------------------------------------------------------------------------

布局文件

<?xml version="1.0" encoding="utf-8"?>
<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/drawer"
    tools:context="com.example.monikaoshi.Activity_DongTai">
    <!--主页面-->
    <LinearLayout
        android:id="@+id/linear_zhu"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/frame"
            android:layout_weight="9"
            android:layout_width="match_parent"
            android:layout_height="0dp"></FrameLayout>
        <RadioGroup
            android:id="@+id/group"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp">
            <RadioButton
                android:id="@+id/btn_sy"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:gravity="center"
                android:button="@null"
                android:checked="true"
                android:background="@drawable/shouye"
                android:layout_height="match_parent"
                android:text="首页"/>
            <RadioButton
                android:id="@+id/btn_zx"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:gravity="center"
                android:button="@null"
                android:layout_height="match_parent"
                android:background="@drawable/zixun"
                android:text="咨询"/>
            <RadioButton
                android:id="@+id/btn_sz"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:gravity="center"
                android:button="@null"
                android:background="@drawable/shezhi"
                android:layout_height="match_parent"
                android:text="设置"/>
            <RadioButton
                android:id="@+id/btn_wd"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:gravity="center"
                android:button="@null"
                android:layout_height="match_parent"
                android:background="@drawable/daohang"
                android:text="我的"/>
        </RadioGroup>
    </LinearLayout>
    <!--侧边栏页面-->
    <LinearLayout
        android:id="@+id/linear_ce"
        android:orientation="vertical"
        android:layout_width="350dp"
        android:layout_gravity="start"
        android:background="#ffff"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/img_tou"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="50dp"
            android:layout_marginLeft="100dp"
            android:background="@drawable/wnm2"
            />
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listView"></ListView>
    </LinearLayout>

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

猜你喜欢

转载自blog.csdn.net/qq_14876513/article/details/80170771
今日推荐