메인 엑티비티를 LeftFragment와 RightFragment 둘로 1:1로 나누고 left에서 누른 색으로 오른쪽이 변하게 만들었는데 오른쪽 색상이 원하는 색으로 변하질 않고 세개다 같은 색으로 됩니다. 개발자님들 도와주십
<메인 액티비티>
package com.example.instargram;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
//import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.app.Fragment;
public class Main2Activity extends AppCompatActivity implements OnColorButtonListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
FirstFragment firstFragment = new FirstFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.right_Fragment, firstFragment)
.commit();
}
@Override
public void onColorClick(int color) {
switch (color) {
case 0: {
FirstFragment firstFragment = new FirstFragment();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.right_Fragment, firstFragment)
.commit();
}
case 1: {
SecondFragment secondFragment = new SecondFragment();
secondFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.right_Fragment, secondFragment)
.commit();
}
case 2: {
ThirdFragment thirdFragment = new ThirdFragment();
thirdFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.right_Fragment, thirdFragment)
.commit();
}
}
}
}
LeftFragment
package com.example.instargram;
import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.FragmentManager;
/**
* A simple {@link Fragment} subclass.
*/
public class LeftFragment extends Fragment implements View.OnClickListener {
private OnColorButtonListener listener;
public LeftFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_left, container, false);
view.findViewById(R.id.first).setOnClickListener(this);
view.findViewById(R.id.second).setOnClickListener(this);
view.findViewById(R.id.third).setOnClickListener(this);
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
listener = (OnColorButtonListener)context;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.first: {
listener.onColorClick(0);
}
case R.id.second: {
listener.onColorClick(1);
}
case R.id.third: {
listener.onColorClick(2);
}
}
}
}
<MainActivity XML>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".Main2Activity">
<fragment
android:name="com.example.instargram.LeftFragment"
android:id="@+id/left_Fragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/right_Fragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LeftFragment XML>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LeftFragment">
<View
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5259"/>
<View
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#4452"/>
<View
android:id="@+id/third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#5991"/>
</LinearLayout>
그리고 onColorClick 함수는 interface OnColorButtonListener의 함수입니다.