在fragmentTransaction.Replace()之后获取视图

时间:2012-11-20 12:09:51

标签: android android-fragments android-listfragment

我有一个活动,里面有两个片段(都是动态创建的),其中一个是ListFragment。我在活动中实现了一个onListItemClick处理程序。单击某个项目时,我想用其他片段替换这两个片段,并填充TextView。但是,在替换片段后,我似乎无法获取View对象,我需要在新的Details片段中操作TextView - 它返回null。这是一些相关的代码(onListItemSelected是在主活动中处理onListItemClick的处理程序)。

@Override
public void onListItemSelected(int index) {
    inflateCheckinFragment();
    FragmentManager fm = getFragmentManager();

    FragmentTransaction fragmentTransaction = fm.beginTransaction();
        cif = new checkInFragment();
        fragmentTransaction.replace(R.id.action_container, cif, "ACTIONS");
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit()

    FragmentTransaction fragmentTransaction2 = fm.beginTransaction();
        gdf = new GeolocDetailsFragment();
        fragmentTransaction2.replace(R.id.fragment_container, gdf, "DETAILS");
        fragmentTransaction2.addToBackStack(null);
        fragmentTransaction2.commit();

    View gdfView = gdf.getView();

    TextView tv = (TextView)  gdfView.findViewById(R.id.textPOI);
    tv.setText(printPOI(poiList.get(index)));
}

2 个答案:

答案 0 :(得分:1)

我最终只是在onListItemSelected方法中设置数据。 selectedPOI是一个私人类成员。

public void onListItemSelected(int index) {
        FragmentManager fm = getFragmentManager();

        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.action_container, cif, TAG_CHECKIN);
        fragmentTransaction.replace(R.id.fragment_container, gdf, TAG_DETAILS);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

        selectedPOI = poiList.get(index);
    }

然后在GeolocDetailsFragment类中,我设置了一个处理程序,在Fragment的onCreateView方法的Activity中调用它来设置TextView值。

public class GeolocDetailsFragment extends Fragment {
    private TextSetter textSetter;

    public interface TextSetter {
        public String getActivityText();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.geoloc_details_fragment, container, false);
        TextView detailsText = (TextView) view.findViewById(R.id.textPOI);

        detailsText.setText(textSetter.getActivityText());

        return view;
    }

    public void onAttach(Activity activity) {

        super.onAttach(activity);

        try {
            textSetter = (TextSetter) activity;
        }
        catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnGetPOIListener");
        }
    }

}

最后,我在main活动中实现了getActivityText()以获取传递给TextView的字符串。

    @Override
    public String getActivityText() {

        return printPOI(selectedPOI);
    }

答案 1 :(得分:0)

您应该以这样的方式使用片段,使片段中的TextView公开有助于Activity直接访问它,然后更改值。

Class GeolocDetailsFragment extends Fragments{

public TextView tv;

onCreateView(){

    tv= (TextView)  rootView.findViewById(R.id.textPOI);

    }


}

//在您的活动中

@Override
public void onListItemSelected(int index) {

      gdf = new GeolocDetailsFragment();
      gdf.tv.setText(printPOI(poiList.get(index)));

}