public class MainActivity extends FragmentActivity implements OnClickListener {
final String TAG = "MainActivity";
int mCurrentFragmentIndex;
public final static int FRAGMENT_ONE = 0;
public final static int FRAGMENT_TWO = 1;
public final static int FRAGMENT_THREE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_oneFragment = (Button) findViewById(R.id.bt_oneFragment);
bt_oneFragment.setOnClickListener(this);
Button bt_twoFragment = (Button) findViewById(R.id.bt_twoFragment);
bt_twoFragment.setOnClickListener(this);
Button bt_threeFragment = (Button) findViewById(R.id.bt_threeFragment);
bt_threeFragment.setOnClickListener(this);
mCurrentFragmentIndex = FRAGMENT_ONE;
fragmentReplace(mCurrentFragmentIndex);
}
public void fragmentReplace(int reqNewFragmentIndex) {
Fragment newFragment = null;
Log.d(TAG, "fragmentReplace " + reqNewFragmentIndex);
newFragment = getFragment(reqNewFragmentIndex);
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // FragmentTransaction 생성
transaction.replace(R.id.ll_fragment, newFragment);
transaction.commit();
}
private Fragment getFragment(int idx) {
Fragment newFragment = null;
switch (idx) {
case FRAGMENT_ONE:
newFragment = new OneFragment();
break;
case FRAGMENT_TWO:
newFragment = new TwoFragment();
break;
case FRAGMENT_THREE:
newFragment = new ThreeFragment();
break;
default:
Log.d(TAG, "Unhandle case");
break;
}
return newFragment;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_oneFragment:
mCurrentFragmentIndex = FRAGMENT_ONE;
fragmentReplace(mCurrentFragmentIndex);
Button a = (Button) findViewById(R.id.bt_oneFragment);
Button b = (Button) findViewById(R.id.bt_twoFragment);
Button c = (Button) findViewById(R.id.bt_threeFragment);
a.setSelected(true);
b.setSelected(false);
c.setSelected(false);
break;
case R.id.bt_twoFragment:
mCurrentFragmentIndex = FRAGMENT_TWO;
fragmentReplace(mCurrentFragmentIndex);
Button q = (Button) findViewById(R.id.bt_oneFragment);
Button w = (Button) findViewById(R.id.bt_twoFragment);
Button e = (Button) findViewById(R.id.bt_threeFragment);
q.setSelected(false);
w.setSelected(true);
e.setSelected(false);
break;
case R.id.bt_threeFragment:
mCurrentFragmentIndex = FRAGMENT_THREE;
fragmentReplace(mCurrentFragmentIndex);
Button f = (Button) findViewById(R.id.bt_oneFragment);
Button g = (Button) findViewById(R.id.bt_twoFragment);
Button h = (Button) findViewById(R.id.bt_threeFragment);
f.setSelected(false);
g.setSelected(false);
h.setSelected(true);
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
android:weightSum="1810" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="270"
android:background="#ffffffff"
android:orientation="vertical"
android:weightSum="270" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="130"
android:background="#edc951"
android:orientation="horizontal"
android:weightSum="1080" >
<ImageView
android:id="@+id/item_address"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="450"
android:gravity="left"
android:src="@drawable/mjo"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="140"
android:background="#ffffffff"
android:orientation="horizontal"
android:weightSum="1080" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="360" >
<Button
android:id="@+id/bt_oneFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/selector" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="360" >
<Button
android:id="@+id/bt_twoFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/selector2" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="360" >
<Button
android:id="@+id/bt_threeFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/selector3" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1540" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
제목처럼 버튼을 클릭했을때 작성해놓은 프래그먼트로 화면이 안채워지네요. xml문제같은데 봐주세요 제발요!!
며칠째 제자리입니다.