如何控制另一个片段

时间:2014-07-17 11:53:13

标签: android fragment

我已经编写了这段代码,我有关于数据库的数据,我希望按下按钮时按年龄对listview进行排序,listView应该按年龄再次排序,现在方法工作正常但我不能按年龄求助列表视图,这里是列表片段代码:

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class FragmentList extends Fragment {
ListView lst;
DB db;
public static ArrayList<String> LIST;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragmentlist, container,
            false);

    lst = (ListView) rootView.findViewById(R.id.lstViewf);
    db = new DB(getActivity());
    LIST = new ArrayList<String>();

    for (int i = 0; i < db.getAllContacts().size(); i++) {
        LIST.add(db.getAllContacts().get(i).name.toLowerCase());

    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, LIST);
    lst.setAdapter(adapter);

    return rootView;
}
}

这里是Radio片段,我想点击其中一个进行更改

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast; 

public class FragmentRadio extends Fragment {
RadioButton rad1, rad2, rad3;
RadioGroup radioGroup;
DB db;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_radio, container,
            false);
    db = new DB(getActivity());
    rad1 = (RadioButton) rootView.findViewById(R.id.frad1);
    rad2 = (RadioButton) rootView.findViewById(R.id.frad2);
    rad3 = (RadioButton) rootView.findViewById(R.id.frad3);
    radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup1);
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (rad1.getId() == checkedId) {
                Toast.makeText(getActivity(), "!", Toast.LENGTH_SHORT)
                        .show();

                startActivity(new Intent(getActivity(), ListActivity.class));
            }

            else if (rad2.getId() == checkedId) {

                Toast.makeText(getActivity(), "!", Toast.LENGTH_SHORT)
                        .show();

                startActivity(new Intent(getActivity(), ListActivity.class));

                // }

            } else if (rad3.getId() == checkedId) {

            }
        }
    });

    return rootView;
}
}

最后这里是包含两个片段的布局

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class ListActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    getSupportFragmentManager().beginTransaction()
            .add(R.id.f1, new FragmentList()).commit();
    getSupportFragmentManager().beginTransaction()
            .add(R.id.f2, new FragmentRadio()).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.list, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


}  

XML文件  无线电片段          

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="153dp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" >

    <RadioButton
        android:id="@+id/frad1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sort By Name" />

    <RadioButton
        android:id="@+id/frad2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sort By age" />

    <RadioButton
        android:id="@+id/frad3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sort By ID" />
</RadioGroup>

</RelativeLayout>

这是listfragment xml     

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

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

您可以为FragmentList设置TAG,如下所示

getSupportFragmentManager().beginTransaction().add(R.id.f1, new FragmentList(),"YOUR_TAG").commit();

然后在FragmentRadio课程中,您可以获得对FragmentList的引用,如下所示

FragmentList fragmentList = (FragmentList) getActivity().getSupportFragmentManager().findFragmentByTag("YOUR_TAG");

之后,您可以在sortList()类中创建FragmentList方法,并直接从其他片段调用它 fragmentList.sortList();

但我不推荐片段之间的直接通信,最好创建一个回调接口,并将该活动用作两个片段之间的中间体。

选中此项以了解如何实现回调接口并使用它来传递片段之间http://developer.android.com/guide/components/fragments.html#EventCallbacks

答案 1 :(得分:0)

您可以在片段和活动之间创建一个界面。您希望成为控制器的片段将使用该活动向您的片段发送回调。