MainActivity
package com.jj.day14_ten;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.jj.day14_ten.home1.home1Fragment;
import com.jj.day14_ten.home2.home2Fragment;
import com.jj.day14_ten.home3.home3Fragment;
import com.jj.day14_ten.home4.home4Fragment;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
private RadioGroup rg;
private RadioButton[] rbArry;
private FragmentManager fm;
private ArrayList<Fragment> fragments;
private int current;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initFragment();
}
private void initFragment() {
FragmentTransaction ft=fm.beginTransaction();
home1Fragment fm1=new home1Fragment();
home2Fragment fm2=new home2Fragment();
home3Fragment fm3=new home3Fragment();
home4Fragment fm4=new home4Fragment();
fragments.add(fm1);
fragments.add(fm2);
fragments.add(fm3);
fragments.add(fm4);
ft.add(R.id.layout,fragments.get(0)).commit();
}
private void initView() {
rg=(RadioGroup)findViewById(R.id.rg);
rbArry=new RadioButton[rg.getChildCount()];
for (int i = 0; i <rg.getChildCount() ; i++) {
rbArry[i]=(RadioButton)rg.getChildAt(i);
}
fm=getSupportFragmentManager();
fragments=new ArrayList<Fragment>();
rbArry[0].setChecked(true);
rg.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
FragmentTransaction ft=fm.beginTransaction();
for (int i= 0; i <rg.getChildCount() ; i++) {
if (rbArry[i].getId()==checkedId) {
if (fragments.get(i).isAdded()){
ft.show(fragments.get(i)).hide(fragments.get(current)).commit();
}else {
ft.add(R.id.layout,fragments.get(i)).hide(fragments.get(current)).commit();
}
current=i;
}
}
}
}
fragment
package com.jj.day14_ten.home3;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.jj.day14_ten.R;
/**
* A simple {@link Fragment} subclass.
*/
public class home3Fragment extends Fragment {
private TextView tv;
private String url="http://218.244.149.129:9010/api/companylist.php?industryid=98";
private String postUrl="http://218.244.149.129:9010/api/companylist.php";
private String body="industryid=98";
public home3Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_home3, container, false);
initView(view);
return view;
}
private void initView(View view) {
tv=(TextView)view.findViewById(R.id.tv);
}
}
drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/home_critic_focus" android:state_checked="true"/>
<item android:drawable="@mipmap/home_critic" android:state_checked="false"/>
</selector>
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="com.jj.day14_ten.MainActivity">
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:background="@android:color/black">
<RadioButton
android:id="@+id/box1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:button="@null"
android:drawableTop="@drawable/home1"/>
<RadioButton
android:id="@+id/box2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:button="@null"
android:drawableTop="@drawable/home2"/>
<RadioButton
android:id="@+id/box3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:button="@null"
android:drawableTop="@drawable/home3"/>
<RadioButton
android:id="@+id/box4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:button="@null"
android:drawableTop="@drawable/home4"/>
</RadioGroup>
<FrameLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/rg"
android:layout_weight="1"
></FrameLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="360dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="20sp"
android:layout_marginTop="15dp"
android:layout_marginLeft="20dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="摄影师:animee"
android:textSize="16sp"
android:layout_marginLeft="20dp"
android:textColor="#888" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#aaa"
android:layout_margin="20dp"/>
<TextView
android:id="@+id/summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我很少主动开口,过了三十岁之后,我就觉得向别人倾诉是一件多么没意义的事。但是在青少年时代,我曾疯狂地爱,熬夜地哭。对于年轻时候的我而言,所有的矛盾都不是问题,无非是一种忙碌状态下的空虚。"
android:textSize="12sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:padding="10dp"
android:background="#eeeeee"/>
<TextView
android:id="@+id/summary_author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="---无所谓"
android:textSize="12sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:padding="10dp"
android:gravity="right"
android:background="#eeeeee"/>
</LinearLayout>
</ScrollView>