Android Fragment碎片-片段(不同布局,仿手机设置界面)

package com.example.android_11;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
//对应xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity">
	<!--帧布局标签-->
    <fragment
        android:layout_width="0dp"
        android:id="@+id/f_main_1"
        android:name="com.example.android_11.FragmentLeft"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@layout/fragment_left"></fragment>


    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/f_main_2"
        android:layout_weight="3"></FrameLayout>

</LinearLayout>

package com.example.android_11;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Created by Administrator on 2018/1/28 0028.
 */
public class FragmentLeft extends Fragment {
    private String[] data = {"张三","李四","王五"};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //获取碎片
        View v = inflater.inflate(R.layout.fragment_left, null);
        //给左侧碎片视图
        ListView lv_fragment_left_content = v.findViewById(R.id.lv_fragment_left_content);
        //给ListView赋值
        lv_fragment_left_content.setAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,data));
        //给每个元素加点击事件
        lv_fragment_left_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //根据不同的成员获取不同的碎片
                //获取碎片管理器
                FragmentManager fragmentManager = getFragmentManager();
                //根据碎片来开启事务
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                //将activity_main.xml的右侧碎片指定为自己想要的展示的布局
                switch (i) {
                    case 0:
                        transaction.replace(R.id.f_main_2,new FragmentRightOne());
                        break;
                    case 1:
                        transaction.replace(R.id.f_main_2,new FragmentRightTwo());
                        break;
                    case 2:
                        transaction.replace(R.id.f_main_2,new FragmentRightThree());
                        break;
                }
                //提交事务
                transaction.commit();
            }
        });
        return v;
    }
    //对应的xml
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:background="#ff0000"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:id="@+id/lv_fragment_left_content"
        android:layout_height="wrap_content"></ListView>

</LinearLayout>
  //第一个布局
    package com.example.android_11;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2018/1/28 0028.
 */

public class FragmentRightOne extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_right_one,null);
    }
}
}
//对应的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox"
        tools:layout_editor_absoluteX="70dp"
        tools:layout_editor_absoluteY="90dp" />

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton"
        tools:layout_editor_absoluteX="28dp"
        tools:layout_editor_absoluteY="133dp" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton"
        tools:layout_editor_absoluteX="54dp"
        tools:layout_editor_absoluteY="204dp" />

    <CheckedTextView
        android:id="@+id/checkedTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckedTextView"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="265dp" />
</LinearLayout>
//第二个布局
package com.example.android_11;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2018/1/28 0028.
 */

public class FragmentRightTwo extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_right_two,null);
    }
}
//对应xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="107dp" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="78dp"
        tools:layout_editor_absoluteY="146dp" />

    <ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="90dp"
        tools:layout_editor_absoluteY="194dp" />

    <ProgressBar
        android:id="@+id/progressBar3"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="40dp"
        tools:layout_editor_absoluteY="282dp" />
</LinearLayout>
//第三个布局
package com.example.android_11;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2018/1/28 0028.
 */

public class FragmentRightThree extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_right_three,null);
    }
}
//对应xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <SeekBar
        android:id="@+id/seekBar"
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:max="10"
        android:progress="3"
        tools:layout_editor_absoluteX="246dp"
        tools:layout_editor_absoluteY="128dp" />

    <QuickContactBadge
        android:id="@+id/quickContactBadge"
        android:layout_width="138dp"
        android:layout_height="154dp"
        app:srcCompat="@mipmap/ic_launcher"
        tools:layout_editor_absoluteX="86dp"
        tools:layout_editor_absoluteY="173dp" />
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/f_1314520/article/details/82815239
今日推荐