activity_main.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="vertical"
tools:context=".MainActivity" >
<fragment
android:id="@+id/fragment_color_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:name="com.example.fragmentex.ColorListFragment"
/>
<fragment
android:id="@+id/fragment_color"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:name="com.example.fragmentex.ColorFragment"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity
implements ColorListFragment.onColorSelectedListener {
private ColorFragment mColorFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mColorFragment = (ColorFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment_color);
mColorFragment.setColor(Color.RED);
}
@Override
public void onColorSelected(int color) {
mColorFragment.setColor(color);
}
}
ColorListFragment.java
public class ColorListFragment extends ListFragment {
private onColorSelectedListener mListener;
interface onColorSelectedListener{
void onColorSelected(int color);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try{
mListener = (onColorSelectedListener) context;
}catch (ClassCastException e){
throw new ClassCastException(((Activity) context).getLocalClassName()+
"는 OnClolorSelectedListener를 구현해야 합니다.");
}
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
List<String> colorList = Arrays.asList("Red", "Green", "Blue");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,colorList);
setListAdapter(adapter);
}
@Override
public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
//프래그먼트에 있는 어뎁터를 가져온다.
ArrayAdapter<String> adapter = (ArrayAdapter<String>) l.getAdapter();
String colorString = adapter.getItem(position);
int color = Color.RED;
switch (colorString){
case "Red":
color = Color.RED;
break;
case "Green":
color = Color.GREEN;
break;
case "Blue":
color = Color.BLUE;
break;
}
if(mListener != null)
mListener.onColorSelected(color);
}
}
ColorFragment.java
public class ColorFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return new View(getActivity());
}
//색상추가
public void setColor(int color){
getView().setBackgroundColor(color);
}
}
'PROGRAM > Android' 카테고리의 다른 글
안드로이드 검색 로그(ver.20-05-08) (0) | 2020.05.09 |
---|---|
설명문 띄우기 (0) | 2020.05.08 |
ListView 아이템 추가,수정,삭제 (0) | 2020.05.01 |
ListView -02 (extends ListActivity) (0) | 2020.04.30 |
ListView 추가하기 - 01 (0) | 2020.04.30 |