主页面,Fragment点击切换,布局

package com.example.android_fuxi_20180427;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.android_fuxi_20180427.adapter.MyAdapter2;
import com.example.android_fuxi_20180427.data.ReaData2;
import com.example.android_fuxi_20180427.fragment.Fragment01;
import com.example.android_fuxi_20180427.fragment.Fragment02;
import com.example.android_fuxi_20180427.fragment.Fragment03;
import com.example.android_fuxi_20180427.fragment.Fragment04;
import com.google.gson.Gson;

import java.util.List;

public class Main2Activity extends AppCompatActivity {

    private RadioGroup group;
    private ListView listView;
private String path="http://120.27.23.105/product/getCatagory";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main2 );
        group = findViewById( R.id.rg );
        listView = findViewById( R.id.lv );

        initData();
listView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent( Main2Activity.this, Main3Activity.class );
        startActivity( intent );
    }
} );
        getSupportFragmentManager().beginTransaction().replace( R.id.fcontent,new Fragment01() ).commit();
        group.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.but1:
                        getSupportFragmentManager().beginTransaction().replace( R.id.fcontent,new Fragment01() ).commit();
                        break;
                    case R.id.but2:
                        getSupportFragmentManager().beginTransaction().replace( R.id.fcontent,new Fragment02() ).commit();
                        break;
                    case R.id.but3:
                        getSupportFragmentManager().beginTransaction().replace( R.id.fcontent,new Fragment03() ).commit();
                        break;
                    case R.id.but4:
                        getSupportFragmentManager().beginTransaction().replace( R.id.fcontent,new Fragment04() ).commit();
                        break;
                }
            }
        } );
    }

    private void initData() {
        MyTask task = new MyTask( new MyTask.ICallBacks() {
            @Override
            public void getJsonData(String jsonstr) {
                Gson gson = new Gson();
                ReaData2 reaData2 = gson.fromJson( jsonstr, ReaData2.class );
                List<ReaData2.DataBean> data = reaData2.getData();
                MyAdapter2 myAdapter2 = new MyAdapter2( data, Main2Activity.this );
                listView.setAdapter( myAdapter2 );
            }
        } );
        task.execute( path );
    }
}
 
 

*************************************************************************




<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:id="@+id/rg">
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="56dp"
            android:button="@null"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/selector_home"
            android:id="@+id/but1"/>
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="56dp"
            android:background="@drawable/selector_find"
            android:button="@null"
            android:layout_weight="1"
            android:gravity="center"
            android:id="@+id/but2"/>
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="56dp"
            android:button="@null"
            android:background="@drawable/selector_shop"
            android:layout_weight="1"
            android:gravity="center"
            android:id="@+id/but3"/>
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="56dp"
            android:background="@drawable/selector_mine"
            android:button="@null"
            android:layout_weight="1"
            android:gravity="center"
            android:id="@+id/but4"/>
    </RadioGroup>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fcontent"
        android:layout_above="@id/rg"
        >
    </FrameLayout>

</RelativeLayout>
    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#aff"
        >
        <ImageView
            android:layout_width="1800dp"
            android:layout_height="120dp"
            android:layout_marginLeft="5dp"
            android:src="@drawable/a6"
            android:id="@+id/iii"/>
<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lv"
    android:layout_below="@id/iii"></ListView>
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>


猜你喜欢

转载自blog.csdn.net/qq_41927301/article/details/80176799
今日推荐