将数据从片段传递到片段

时间:2014-02-10 11:09:48

标签: android android-fragments

我在两个片段之间传递数据时遇到问题。 一个活动中有两个片段。在Fragment1中,我有一个带布局的listView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >

<ImageView
    android:id="@+id/imageView01"
    android:layout_width="100dip"
    android:layout_height="100dip"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:scaleType="fitXY"
    android:src="@drawable/icon" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/titleView1"
    android:layout_alignTop="@+id/imageView01"
    android:text="add to favorites" />

<TextView
    android:id="@+id/titleView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/checkBox1"
    android:layout_toRightOf="@+id/imageView01"
    android:text="text"
    android:textColor="#FF2F22"
    android:textSize="16sp"
    android:textStyle="bold" />

</RelativeLayout>

活动代码

public class MainActivity extends FragmentActivity {
private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpager_layout);
    initialisePaging();     
}

private void initialisePaging() {       
    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));       
    mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);
    ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
    pager.setAdapter(mPagerAdapter);
}
}

fragment1代码

public class Fragment1 extends Fragment {
private ListView listViewImages;
private EditText txtSearchText;
private ListViewImageAdapter adapter;
private ArrayList<Object> listImages;
private Fragment activity;
String strSearch = null;    

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = (LinearLayout) inflater.inflate(R.layout.fragment1_layout, container, false);
    activity = this;
    listViewImages = (ListView) view.findViewById(R.id.lviewImages);
    ...
    return view;
}

在我的ListViewAdapter中,我用ImageView,TextView和CheckBox填充listView。对于checkBox,我想设置一个onClickListener(当我检查checkBox时,必须将数据传递给Fragment2)

CheckBox call = (CheckBox) vi.findViewById(R.id.checkBox1);         
   call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
        // HERE DATA MUST BE PASSED TO Fragment2
        RelativeLayout rl = (RelativeLayout) v.getParent();
    TextView txtViewTitle = (TextView) rl.findViewById(R.id.titleView1);
    String text = txtViewTitle.getText().toString();                    
    Log.i("!!!", "Click on CheckBox"+text);
}
  });

因此。我的问题是,当我按下checkBox(不是ListView项目!!!)时,如何将数据传递给另一个片段?我没有ListFragment,它是一个带ListView的简单片段,ListView的每个项目都包含ImageView,CheckBox,TextBox。 感谢。

1 个答案:

答案 0 :(得分:2)

我没有自己的IDE,但我给你一个简单的例子,向你展示了通过MainActivity将数据传递给其他片段的原理。

<强> MainActivity

class MainActivity{
    //Globalize your Fragments
    Fragment1 mFrag1;
    Fragment2 mFrag2;
    //Init those two Fragments and add them to your layout;

    public void sendDataToFrag1(String data){
        mFrag1.setData(data);
    }


    public void sendDataToFrag2(String data){
        mFrag2.setData(data);
    }
}

Frag1或Frag2

class Fragment1{
//.... oncreate and so on

   //perhaps on Buttonclick
   new OnClickListeneter{
        @Override
        onClick(View v){
            passData("The Button was clicked");
        }
   }

   //here comes the value from the other fragment
    public void setData(String data){
        myTextView.setText(data);
    }

    //Here you send some data over MainActivity to the other Fragment
    private void passData(String data){
        ((MainActivity)getActitivity()).sendDataToFrag2(data)
    }

}