안드로이드

 

 

 

 

 

 

 

 

 

 

 

 

class MainActivity

package kr.co.braverokmc.myfragment2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements FragmentList.ImageSelectedListener {

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

    }


    @Override
    public void onImageSelected(int index, int resId) {
        //프레그먼트 FragmentViewer 제어를 위한 호출
         FragmentViewer fragmentViewer=(FragmentViewer)getSupportFragmentManager().findFragmentById(R.id.fragmentViewer);
         fragmentViewer.updateImage(index, resId);

    }



}

 

 

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_main"
    android:orientation="vertical">



    <fragment
        android:name="kr.co.braverokmc.myfragment2.FragmentList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:id="@+id/fragmentList"
        >

    </fragment>

    <fragment
        android:name="kr.co.braverokmc.myfragment2.FragmentViewer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/fragmentViewer"
        >

    </fragment>

</LinearLayout>

 

class FragmentList

 

package kr.co.braverokmc.myfragment2;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by choi on 2017-04-22.
 */

public class FragmentList extends Fragment {

    public static int images[] ={R.drawable.c1, R.drawable.c2, R.drawable.c4};

    public static interface ImageSelectedListener{

        //index - 0, 1, 2,,,,,몇번째 이미지
        // 이미지 id =images[0]
        public void onImageSelected(int index, int resId);
    }

    ImageSelectedListener listener;

    @Override
    public void onAttach(Activity activity) {

        // 메인 액티비티에 올라가면
        //만약 현재 activity 가 ImageSelectedListener 맞다면
        if(activity instanceof  ImageSelectedListener){

            listener=(ImageSelectedListener)activity;
        }

        super.onAttach(activity);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        //바로 붙는 것이 아니라 생명주기 에맞게 붙는다
        ViewGroup rootview=(ViewGroup)inflater.inflate(R.layout.fragment_list, container, false);

        Button button1=(Button)rootview.findViewById(R.id.button1);
        Button button2=(Button)rootview.findViewById(R.id.button2);
        Button button3=(Button)rootview.findViewById(R.id.button3);


        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                listener.onImageSelected(0, images[0]);

            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImageSelected(1, images[1]);
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImageSelected(2, images[2]);

            }
        });


        return  rootview;
    }




    @Override
    public void onDetach() {
        super.onDetach();
    }



}

 

 

R.layout.fragment_list

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="첫번째 이미지"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:textAlignment="center"
        android:layout_gravity="center_horizontal"
        android:onClick="onButton1Clicked"
        android:background="@android:color/holo_blue_light"
        android:textColor="@android:color/background_light"
        android:layout_marginBottom="2dp"
        />

    <Button
        android:text="두번째 이미지"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:textAlignment="center"
        android:layout_gravity="center_horizontal"
        android:onClick="onButton2Clicked"
        android:background="@android:color/holo_green_light"
        android:textColor="@android:color/background_light"
        android:layout_marginBottom="2dp"
        />

    <Button
        android:text="세번째 이미지"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:textAlignment="center"
        android:layout_gravity="center_horizontal"
        android:onClick="onButton3Clicked"
        android:background="@android:color/holo_orange_light"
        android:textColor="@android:color/background_light"
        android:layout_marginBottom="2dp"
        />



</LinearLayout>

 

 

class FragmentViewer

 

package kr.co.braverokmc.myfragment2;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by choi on 2017-04-22.
 */

public class FragmentViewer extends Fragment{
    TextView textView;
    ImageView imageView;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        //바로 붙는 것이 아니라 생명주기 에맞게 붙는다
        ViewGroup rootview=(ViewGroup)inflater.inflate(R.layout.fragment_viewr, container, false);

        textView=(TextView)rootview.findViewById(R.id.textView);
        imageView=(ImageView)rootview.findViewById(R.id.imageView);

        return  rootview;
    }



    public void updateImage(int index, int resId){
        textView.setText("선택된 이미지 : #" +index);
        imageView.setImageResource(resId);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}

 

R.layout.fragment_viewr

<?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"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="선택된 이미지"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textSize="30sp"
        android:textAlignment="center" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        />


</LinearLayout>

 

 

 

 

 

137강

 

 

 

138강

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

의복은 새 것일수록 좋고 사람은 옛 사람이 더욱 정답다. -고시원

댓글 ( 4)

댓글 남기기

작성