javascript - Android Fragment片段切換的問題
大家讲道理
大家讲道理 2017-04-10 16:16:58
0
1
717

我有一個頁面是兩個Fragment

點擊第一頁之後裡面有一個按鈕可以連到一個內頁
這時如果我再去點擊上面的第一頁或第二頁都無法再加入到view裡
請問我該如何做呢?

以下附上Main跟第一頁的Code

<mainActivity>
public class MainActivity extends Activity {

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

    Button button1 = (Button) this.findViewById(R.id.btn_fragment1);
    Button button2 = (Button) this.findViewById(R.id.btn_fragment2);
    Button back = (Button) this.findViewById(R.id.button);
    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            onButtonClick(view.getId());
        }
    });

    button2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            onButtonClick(view.getId());
        }
    });

    back.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
}
private void onButtonClick(int id) {
            FragmentManager fm = getFragmentManager();

            Fragment fragment = fm.findFragmentById(R.id.layout_container);
            if (fragment == null) {

                if (id == R.id.btn_fragment1)
                    fragment = new Sample1Fragment();
                else
                    fragment = new Sample2Fragment();

                replaceFragment(fragment);
            } else if (id == R.id.btn_fragment1
                    && fragment instanceof Sample2Fragment) {
                replaceFragment(new Sample1Fragment());
            } else if (id == R.id.btn_fragment2
                    && fragment instanceof Sample1Fragment) {
                replaceFragment(new Sample2Fragment());
    }

}

public void replaceFragment(Fragment fragment) {
    this.getFragmentManager()
            .beginTransaction()                
            .replace(R.id.layout_container, fragment).addToBackStack(null)
            .commit();
}
public void onBackPressed()
{
    if (getFragmentManager().getBackStackEntryCount() > 0){
        getFragmentManager().popBackStack();
    }
    else super.onBackPressed();
}

}

<SampletFragment1>

public class Sample1Fragment extends Fragment {

private String mTimeString;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_sample1, container,
            false);
    TextView txtDateTime = (TextView) view.findViewById(R.id.txtDateTime);
    txtDateTime.setText("這裡是第一頁");

    Button btn = (Button) view.findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onButtonClick(view.getId());
        }
    });
    return view;
}

public void replaceFragment(Fragment fragment) {
    this.getFragmentManager()
            .beginTransaction()
            .replace(R.id.layout_container, fragment).addToBackStack(null)
            .commit();
}

private void onButtonClick(int id) {
    FragmentManager fm = getFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.layout_container);
    if (fragment == null) {

        if (id == R.id.btn_fragment1)
            fragment = new Sample1Fragment();
        else
            fragment = new Sample2Fragment();

        replaceFragment(fragment);
    } else if (id == R.id.btn_fragment1
            && fragment instanceof Sample2Fragment ) {
        replaceFragment(new Sample1Fragment());
    } else if (id == R.id.btn_fragment2
            && fragment instanceof Sample1Fragment ) {
        replaceFragment(new Sample2Fragment());
    }else if (id == R.id.btn_fragment1 && fragment instanceof Sample3Fragment){
        replaceFragment(new Sample1Fragment());
    }

    replaceFragment(new Sample3Fragment());
}

}

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回覆(1)
巴扎黑

其實自己已經找到解決辦法
修改MainActivity下面的nclick就可以了~
附上代碼

private void onButtonClick(int id) {
    FragmentManager fm = getFragmentManager();

    Fragment fragment = fm.findFragmentById(R.id.layout_container);

    //?????Layout???
    if (fragment == null) {
        if (id == R.id.btn_fragment1) {
            fragment = new Sample1Fragment();
        } else {
            fragment = new Sample2Fragment();
        }

        replaceFragment(fragment);
    } else if ((id == R.id.btn_fragment1) &&
            fragment instanceof Sample2Fragment) {
        replaceFragment(new Sample1Fragment());
    } else if ((id == R.id.btn_fragment2) &&
            fragment instanceof Sample1Fragment) {
        replaceFragment(new Sample2Fragment());
    } else if ((id == R.id.btn_fragment1) &&
            fragment instanceof Sample3Fragment) {
        replaceFragment(new Sample1Fragment());
    } else if ((id == R.id.btn_fragment2) &&
            fragment instanceof Sample3Fragment) {
        replaceFragment(new Sample2Fragment());
    }
}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!