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

[Help Me] 뷰페이저 구조 관련해서 질문있습니다.

0 추천

 

안녕하세요.

뷰페이저 관련된 부분에 있어서 구조가 눈에 쉽게 들어오질 않아서 글을 올려봅니다.

내용과 전체적인 구조에 대해서 알고 싶습니다.

 

일단 프로그램은 처음 시작하면, 뷰페이저에 어레이리스트를 사용해서 이미지를 3개를 추가하고(at_thumnail.add), 오른쪽으로 넘겨보면서 왔다갔다 할수 있는 구조입니다. 그리고 그 이미지 아래에 간단한 정보들을 출력하는 at_title과 at_Reservation이 있습니다.

 

제가 이해한 아래의 3개 파일의 관계는 이와 같습니다.

Life.xml 부분에서 뷰페이저 하나를 만듭니다. 그리고 Life.자바파일 부분에서 mViewPager를 통해서

xml에서 선언했든 뷰페이저의 값을 받고, mSectionPagerAdapter을 셋어뎁터로 한다.

(이부분 잘 모르겠습니다.)

그리고 thumnail에 대한 각각의 이미지에 대해서 현재 몇번째 뷰페이저인지 숫자를 카운트해서 Next 클래스

로 넘기고 fragment_sub2에서 buy2를 받은 btn에 대한 부분은 Date_and_Seat 클래스로 마찬가지로 몇번째

클릭된건지에 따라값을 넘깁니다. 그리고, fragment_sub2 파일은 뷰페이저 내에 이미지 아래에 존재하고 있

는 오른쪽으로 이미지를 넘길때마다, 변하는 구조로 만들어야 해서 프래그넌트로 구성을 하게 되었습니다.

 

좀 여러모로 구조적으로도 정보적으로도 많이 미숙한 설명인데.. 어떻게 해야, 구조적으로 눈에 딱딱 들어맞

게 설명이 될까요. adapter, placeholder,  onCreateView에 대해서 설명도 없었고, SectonPageAdapter에 대한

설명도 없었습니다. 이 3가지 파일의 유기적인 관계에 대해서 알고 싶은데 제 눈에 쉽게 보이지가 않네요 ㅠ

머리가 많이 안좋은 편이라, 자세하게 설명해주시면 정말 정말 감사드리겠습니다.

소스가 8000자 이상인 부분은, 아래의 댓글에 붙여보았습니다.

 

< Life.xml 부분 >

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_top"
        android:layout_above="@+id/toolbar_bottom"/>

</RelativeLayout>

 

 


 

 

비선아 (920 포인트) 님이 2016년 11월 9일 질문
< Life.자바부분 >

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class Life extends AppCompatActivity {

    

    private SectionsPagerAdapter mSectionsPagerAdapter;

    public static ArrayList<Integer> at_thumnail = new ArrayList<>();           
    public static ArrayList<String> at_title = new ArrayList<>();               
    public static ArrayList<String> at_ReservationPercent = new ArrayList<>();  

    private ViewPager mViewPager;

    static String  ARG_SECTION_NUMBER = "section_number";

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


        // 뷰페이저의 나타나는 이미지
        at_thumnail.add(R.drawable.img1);
        at_thumnail.add(R.drawable.img2);
        at_thumnail.add(R.drawable.img3);
     
        at_title.add("1. 1번텍스트");
        at_title.add("2. 2번텍스트");
        at_title.add("3. 3번텍스트");

        at_ReservationPercent.add("1번 내용");
        at_ReservationPercent.add("2번 내용");
        at_ReservationPercent.add("3번 내용");

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }

   
    public static class PlaceholderFragment extends Fragment {
        // 생성자
        public PlaceholderFragment() {
        }

      
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            final int position = getArguments().getInt(ARG_SECTION_NUMBER);

            // 레이아웃을 객체화시킴.
            View rootView = inflater.inflate(R.layout.fragment_sub2, container, false);

            TextView title = (TextView) rootView.findViewById(R.id.title);
            title.setText(at_title.get(position));

            TextView Percent = (TextView) rootView.findViewById(R.id.Reservation_Percent);
            Percent.setText(at_ReservationPercent.get(position));

            ImageView thunmail = (ImageView) rootView.findViewById(R.id.thunmail);
            thunmail.setImageResource(at_thumnail.get(position));

            Button btn = (Button) rootView.findViewById(R.id.buy);

            // 이미지에 클릭 리너스를 단다.
            thunmail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent = new Intent(getContext(),Next.class);
                    intent.putExtra("pos",position); //int타입의 데이터를 전달한다.

                    startActivity(intent);
                }
            });

            return rootView;
        }
    }


    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        // 번들 : 객체를 저장할수 있는것
        // getItem : 페이지를 생성할때
        @Override
        public Fragment getItem(int position) {

            Bundle args = new Bundle(); // 번들 생성
            args.putInt(ARG_SECTION_NUMBER, position);

            //프래그먼트를 생성
            PlaceholderFragment fragment = new PlaceholderFragment();
            fragment.setArguments(args); // 생성된 프래그먼트 객체에 번들데이터를 저장.

            return fragment; // 객체가 반환되는순간.
        }

        @Override
        public int getCount() {
            return at_thumnail.size();
        }
    }
}

< fragment_sub2.xml 파일 >
<RelativeLayout 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:background="#262626">

    <ImageView
        android:id="@+id/thunmail"
        android:layout_width="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_height="280dp" />

    <LinearLayout
        android:id="@+id/Side"
        android:orientation="horizontal"
        android:layout_width="160dp"
        android:layout_height="2dp"
        android:background="#ffffff"
        android:layout_below="@+id/title"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp">

    </LinearLayout>


    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textAlignment="center"
        android:layout_below="@+id/thunmail"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="11dp" />

    <TextView
        android:id="@+id/Reservation_Percent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_below="@+id/Side"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/buy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="구매"
        android:layout_below="@+id/Reservation_Percent"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" />

</RelativeLayout>

1개의 답변

0 추천

xml에서 선언했든 뷰페이저의 값을 받고, mSectionPagerAdapter을 셋어뎁터로 한다.

(이부분 잘 모르겠습니다.)

http://berabue.blogspot.kr/2014/05/android-listview.html

일단 간단한(?) 리스트뷰를 먼저 구현해 보시면 쉽게 이해하실수 있을것 같습니다.

예를들어 왜 어댑터를 사용하는지...뷰 홀더 패턴을 사용하는지등등...

구현해 보시고 잘 모르는 내용을 올려주세요

익명사용자 님이 2016년 11월 10일 답변
...