android片段版本4文本问题

时间:2015-12-06 22:20:36

标签: java android fragment

我使用片段(Im a newbe)编写了android代码,我想在片段切换到文本视图中放置一些文本时。我的片段正在进行切换(如意图),但是文本不会改变。这是代码:     公共类Map_Frag扩展Fragment {

---interface---
    Communicator callback;
    TextView tv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View v = inflater.inflate(R.layout.frag_map, container, false);

            tv = (TextView)v.findViewById(R.id.textView);

        return v;
    }


    @Override
    public void onStart() {
        super.onStart();
        Bundle bundle = getArguments();
        if (bundle != null) {
            if (bundle.getBoolean("str1"))
                changTextToWalla1("str1",bundle.getInt("x"));
        }
    }

    public void changTextToWalla1(String str1, int x) {
        switch (x){
            case 1:
                tv.setText("number one");
                break;
            case 2:
                tv.setText("number two");
                break;
        }

    }

}

/*



        if (bundle != null) {
            if (bundle.getBoolean("button1"))
                changTextToWalla1("Selected", bundle.getInt("num"));
        }
    }
---control class--
public class Control_Frag extends Fragment {
    Communicator callback;
    private Button button1;
    private Button button2;



    @Nullable
    @Override

    public void onAttach(Context context){
        super.onAttach(context);
        callback= (Communicator) context;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_control,container,false);
        button1 = (Button)v.findViewById(R.id.button_1);
        button2 = (Button)v.findViewById(R.id.button_2);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.button1Clicked("button1", 1);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.button2clicked("button2", 2);
            }
        });
        return v;

    }
----communicator---
public interface Communicator {
    //here i write the functions that im going to use in the project -
    //all the functions that move from one fragmant to another
 public void button1Clicked (String str1,int x);
 public void button2clicked (String str2,int x);


}
-----main---
public class MainActivity extends FragmentActivity implements Communicator{

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

        if (findViewById(R.id.small_device) != null) {
            Control_Frag cf = new Control_Frag();

            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().add(R.id.small_device, cf , "cf").addToBackStack("cf").commit();
            // In small device it will show only Control_Frag to begin with
        }
    }

    @Override
    public void button1Clicked(String str1, int x) {
        //When button 1 is pressed - change text to walla 1
        // Identifying Map_Frag in Fragment Manager
        Map_Frag ma = (Map_Frag) getSupportFragmentManager().findFragmentById(R.id.frag_content);


        // TABLET
        if( ma != null){
            ma.changTextToWalla1(str1 ,x);
        }
        else {
            ma = new Map_Frag();
            Bundle bundle = new Bundle();
            bundle.putBoolean(str1, true);
            bundle.putInt(str1 ,x);
            ma.setArguments(bundle);
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.small_device,ma,"sadf").addToBackStack("sadf").commit();
        }
    }



    @Override
    public void button2clicked(String str2, int x) {
        Map_Frag ma = (Map_Frag)getSupportFragmentManager().findFragmentById(R.id.frag_content);

        if (ma != null) {
            ma.changTextToWalla1(str2, x);
        }else{
            ma = new Map_Frag();
            Bundle bundle = new Bundle();
            bundle.putBoolean(str2, true);
            bundle.putInt(str2, x);
            ma.setArguments(bundle);
            FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().replace(R.id.small_device,ma,"asd").addToBackStack("asd").commit();
        }

    }


}
I could use some help to get it done

1 个答案:

答案 0 :(得分:0)

为什么不使用您想要的设计创建单独的片段xml,只需在用户决定在它们之间切换时调用片段。

public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = new Fragment1();
            break;
        case 1:
            fragment = new Fragment2();
            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, fragment).commit();

    }
}

public void onSectionAttached(int number) {
    switch (number) {
        case 1:

            mTitle = getString(R.string.title_section1);
            break;
        case 2:
            mTitle = getString(R.string.title_section2);
            break;
        case 3:
            mTitle = getString(R.string.title_section3);
            break;
    }
}