Fragment

来源:互联网 发布:2g手机怎样用4g网络 编辑:程序博客网 时间:2024/06/06 00:14

//会变的图片

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@mipmap/a11" android:state_checked="false"></item>
    <item android:drawable="@mipmap/a12" android:state_checked="true"></item>

</selector>

//主方法

public class Activity_fragment extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private FragmentManager fm;
    private RadioGroup rg1;
    private RadioButton [] rbarray;
    private ArrayList<Fragment> fragmentArrayList;
    private int count=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_fragment);
        initview();
    }
    private void initview() {
        rg1= (RadioGroup) findViewById(R.id.rg2);
        rbarray=new RadioButton[rg1.getChildCount()];
        for (int i = 0; i <rbarray.length ; i++) {
            rbarray[i]= (RadioButton) rg1.getChildAt(i);
        }


        fragmentArrayList=new ArrayList<Fragment>();


        Fragment1_a f1=new Fragment1_a();
        fragmentArrayList.add(new Fragment1_a());
        Fragment1_b f2=new Fragment1_b();
        fragmentArrayList.add(new Fragment1_b());
       Fragment1_c f3=new Fragment1_c();
        fragmentArrayList.add(new Fragment1_c());
        Fragment1_d f4=new Fragment1_d();
        fragmentArrayList.add(new Fragment1_d());


        fm=getSupportFragmentManager();
        FragmentTransaction tt = fm.beginTransaction();
        tt.add(R.id.fl2,fragmentArrayList.get(0));
        rbarray[0].setChecked(true);
       rg1.setOnCheckedChangeListener(this);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction tt = fm.beginTransaction();
        for (int i = 0; i <rbarray.length ; i++) {
            if (rbarray[i].getId()==checkedId){
                if (fragmentArrayList.get(i).isAdded()){
                    tt.show(fragmentArrayList.get(i)).hide(fragmentArrayList.get(count)).commit();
                }else {
                    tt.add(R.id.fl2,fragmentArrayList.get(i)).hide(fragmentArrayList.get(count)).commit();
                }
                count=i;
            }
        }
    }
}



//右键创建Fragment

import com.example.administrator.android_2_weekend2import.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment_1 extends Fragment {




    public Fragment_1() {
        // Required empty public constructor
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v=inflater.inflate(R.layout.fragment_fragment_1, container, false);
        return v;
    }


}












//可能是viewpager的适配器

public class MyAdapterpager extends PagerAdapter{
    private ArrayList<View> list;


    public MyAdapterpager(ArrayList<View> list) {
        this.list = list;
    }


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


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view=list.get(position);
        container.addView(view);
        return view;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view=list.get(position);
        container.removeView(view);
    }
}





//中的布局

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.administrator.android_2_weekend2import.lianfragment_im.Activity_fragment">
    <RadioGroup
        android:id="@+id/rg2"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="60dp">
        <RadioButton
            android:id="@+id/rbu4"
            android:layout_marginTop="20dp"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/rb1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <RadioButton
            android:id="@+id/rbu5"
            android:layout_marginTop="20dp"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/rb2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <RadioButton
            android:id="@+id/rbu6"
            android:layout_marginTop="20dp"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/rb3"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <RadioButton
            android:id="@+id/rbu7"
            android:layout_marginTop="20dp"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/rb1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RadioGroup>
    <FrameLayout
        android:id="@+id/fl2"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"></FrameLayout>


</LinearLayout>




F中只有一个textview

<FrameLayout 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.administrator.android_2_weekend2import.fragment_import.Fragment_1">


    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />


</FrameLayout>

原创粉丝点击