Android Fragmnet-Fragmentデータ交換とListFragmentの使用

Android Fragmnet-Fragmentデータ交換とListFragmentの使用

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

ここに写真の説明を書きます

まず、レイアウトファイル:
ここに写真の説明を書きます

合計4つのXMLファイルがあり、1つ目はアクティビティのレイアウト、2つ目はカスタムアダプターのレイアウト、3つ目は中央のListFragmentのレイアウト、4つ目は右側のフラグメントのレイアウトです。

// ————————-アクティビティレイアウトファイル————————

<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>

// ————————-カスタムアダプタレイアウトファイル————————

<?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レイアウトファイル————————

PS:

ListFragmentのAPI仕様:ListViewのIDは次のように定義する必要があります: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レイアウト————————

<?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数组文件,用来描述对英雄的介绍:

ここに写真の説明を書きます

コード:

<?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コード————————————————
フラグメントをアクティビティにロードする手順:
1. FragmentManager fragmentManager = getFragmentManager(); Getフラグメント管理クラス
2. FragmentTransactionトランザクション= fragmentManager.beginTransaction(); Get FragmentTransactionクラス
3. Fragment継承クラスのインスタンスを取得Center_Fragment center_Fragment = new Center_Fragment();
4. FragmentTransaction addメソッドを使用してフラグメントをロードするか、新しいフラグメントに置き換えますフラグメント。

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サブクラスコード————————
最初に、ListFragmentの使用。
Center_Fragmentを作成してListFragmentクラスを継承し
、a を書き換えます。onCreate // ListFragmentとlistviewは異なります。アダプターを追加するときは、setListAdapterメソッドを使用して
b、onCreateView
c、onPause
d、onListItemClick //クリックイベントリスナーを追加します。

二つ、フラグメントフラグメントデータ交換
setArguments断片を用いる方法は、バンドルの実装クラスがで、putString(「名前」、「SAN」)データの様々な種類の方法にキーを追加使用することができる、クラスフラグメントバンドルに転送することができます受け入れられたFragementでgetArguments()。GetString( "name");を使用します、受け入れ、名前がキーです

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;
        }

    }
}

// ————————————————右側のフラグメントコード————————————————

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

元の記事34件を公開 10のような 30,000以上の訪問

おすすめ

転載: blog.csdn.net/q296264785/article/details/53169440