마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 fragment 색 바꾸기

0 추천

메인 엑티비티를 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의 함수입니다.

개발자가되자! (450 포인트) 님이 2018년 10월 27일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...