1. 程式人生 > >Android Fragment碎片-片段(不同佈局,仿手機設定介面)

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>