Android Fragmnet-Fragment data exchange and use of ListFragment

Android Fragmnet-Fragment data exchange and use of ListFragment

    我把Activity的视图分成三个部分,并且在第二部分加载一个listFragment、第三部分加载一个Fragment。
    用来实现Fragmnet-Fragment、Activity-Fragment的数据交换测试。下面是效果图:

Write a picture description here

First, the layout file:
Write a picture description here

There are a total of 4 XML files, the first is the layout of the Activity, the second is the layout of the custom adapter, the third is the layout of the middle ListFragment, and the fourth is the layout of the right Fragment.

// ————————- Activity layout file ————————

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#cccccc"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/timg" 
            android:paddingTop="30sp"
            android:paddingBottom="50sp"/>

        <Button
            android:id="@+id/left_button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="英雄列表" />

        <Button
            android:id="@+id/left_button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="皮肤列表" />
           <Button
            android:id="@+id/left_button3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="五杀截图" />
        <Button
            android:id="@+id/left_button4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="好友列表" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/center"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ccdddd"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#ccddcc"
        android:gravity="center_horizontal"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

// ————————- Custom adapter layout file ————————

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/adapter_imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="5sp"
        android:paddingLeft="10sp"
        android:paddingTop="5sp"
        android:src="@drawable/aa" />

    <TextView
        android:id="@+id/adapter_textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="5sp"
        android:paddingLeft="10sp"
        android:paddingTop="6sp"
        android:textColor="#2828ff"
        android:text="TextView" />

</LinearLayout>

// ————————- ListFragment layout file ————————

PS:

The API specification of ListFragment: The ID of ListView must be defined as : android: id = ”@ id / android: list”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

// ————————- Fragmnet layout on the right ————————

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center_horizontal">

     <TextView
        android:id="@+id/right_textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#2828ff"
        android:textSize="18sp" 
        android:paddingTop="30dp"
        android:text="英雄介绍"/>
    <TextView
        android:id="@+id/right_textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#2828ff"
        android:paddingTop="20dp"
        android:textSize="18sp" />

</LinearLayout>
    我定义了一个XML数组文件,用来描述对英雄的介绍:

Write a picture description here

Code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="hero_introduced">
        <item>拉克丝出生于享有声望的冕卫家族,在德玛西亚军队模范家庭的氛围里,她注定是不平凡的。</item>
        <item>盖伦是德玛西亚军队的骄傲,是无畏先锋军团的领袖。</item>
        <item>吉格斯生来就有着捣鼓机械的天赋,但他自由散漫、过度亢奋的天性在约德尔科学家当中实属罕</item>
        <item>艾欧尼亚人英勇奋战,却无法阻止外敌,铁蹄踏过,大地浸染血迹。</item>
        <item>特朗德尔是一个粗鄙且狡猾的巨魔,性格非常顽劣。</item>
        <item>伊泽瑞尔的血液中流淌着与生俱来的魔法天赋。。</item>
    </string-array>
</resources>

// —————————————— MainActivity code ————————————————
Steps to load Fragment in Activity:
1. FragmentManager fragmentManager = getFragmentManager (); Get Fragment management class
2. FragmentTransaction transaction = fragmentManager.beginTransaction (); Get FragmentTransaction class
3. Get an instance of Fragment inherited class Center_Fragment center_Fragment = new Center_Fragment ();
4. Load Fragment through FragmentTransaction add method or replace it with a new one Fragment.

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.left_button1).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                Center_Fragment center_Fragment = new Center_Fragment();
                transaction.replace(R.id.center, center_Fragment,"center_Fragment");
                transaction.commit();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

// ——————– ListFragment subclass code ————————
First, the use of ListFragment.
Create Center_Fragment to inherit ListFragment class, and rewrite
a, onCreate // ListFragment and listview are different: when adding adapter, use setListAdapter method to add
b, onCreateView
c, onPause
d, onListItemClick // click event listener

Two, Fragment Fragment data-exchange
method using setArguments Fragment may be transferred in a class Fragment Bundle, Bundle implementation classes may be used putString ( "name", "San") add the key to the method of various types of data, in In the accepted Fragement use getArguments (). GetString ("name"); Accept, name is the key

import java.util.ArrayList;
import java.util.List;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Center_Fragment extends ListFragment {
    private String[] data = { "光辉女郎","德玛西亚之力","炸弹人","刀锋意志", "诅咒巨魔", "探险家", "牛头酋长", "殇之木乃伊", "冰晶凤凰",
            "黑暗之女", "寒冰射手", "露露","提莫队长","炸弹人","刀锋意志", "诅咒巨魔", "探险家", "牛头酋长", "殇之木乃伊", "冰晶凤凰",
            "黑暗之女", "寒冰射手" };
    private myAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        adapter = new myAdapter(getData());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.center,
                null);
        setListAdapter(adapter);
        return view;
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Right_Fragment right_Fragment = new Right_Fragment();
        Bundle bundle = new Bundle();
        bundle.putString("name", getListAdapter().getItem(position).toString());  //fargment间值传递
        right_Fragment.setArguments(bundle);
        transaction.replace(R.id.right, right_Fragment, "right_Fragment");
        transaction.commit();
    }

    // ----------------------------------------------------
    // 获取数据

    public List<String> getData() {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < data.length; i++) {
            list.add(data[i]);
        }
        return list;
    }

    // 自定义适配器
    class myAdapter extends BaseAdapter {
        private List<String> list;

        public myAdapter(List<String> list) {
            // TODO Auto-generated constructor stub
            this.list = list;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View view = null;
            if (convertView != null) {
                view = convertView;
            } else {
                view = LayoutInflater.from(getActivity()).inflate(
                        R.layout.adapter, null);
            }
            TextView textView = (TextView) view
                    .findViewById(R.id.adapter_textView1);
            textView.setText(list.get(position).toString());
            return view;
        }

    }
}

// ———————————————— Fragment code on the right ————————————————

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Right_Fragment extends Fragment {
    private String stringCenterFragment;
    private String[] data;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        stringCenterFragment = getArguments().getString("name"); // 获取另一个Fragment传递过来的,标记为"name"的值
        Toast.makeText(getActivity(),
                "---Right_Fragment->>" + stringCenterFragment, 1).show();
        data = getResources().getStringArray(R.array.hero_introduced);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.right,
                null);
        TextView textView = (TextView) view.findViewById(R.id.right_textView2);
        int i = 0;
        if (stringCenterFragment.equals("光辉女郎")) {
            i = 0;
        } else if (stringCenterFragment.equals("德玛西亚之力")) {
            i = 1;
        } else if (stringCenterFragment.equals("炸弹人")) {
            i = 2;
        } else if (stringCenterFragment.equals("刀锋意志")) {
            i = 3;
        } else if (stringCenterFragment.equals("诅咒巨魔")) {
            i = 4;
        } else if (stringCenterFragment.equals("探险家")) {
            i = 5;
        } else
            i = 0;
        textView.setText(data[i]);
        return view;
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }
}

http://blog.csdn.net/q296264785/article/details/53167961

Published 34 original articles · Like 10 · Visits 30,000+

Guess you like

Origin blog.csdn.net/q296264785/article/details/53169440