2013-04-12 5 views
0

나는 안드로이드에 매우 익숙하다. 끔찍한 질문이라면 용서해 주겠다. 그러나 나는 높고 낮은 것을 찾았고, 많은 자료를 읽었으며, 이걸 알아내는 것 같아. 기본 뷰 (고정 탭 + 스 와이프) 중 하나를 사용하여 Eclipse에서 앱을 만들었습니다. 나는 listview와 함께 두 번째 클래스를 만들었고이 클래스를 추가하여 탭 중 하나에로드하려고합니다.viewpager에 listview 클래스를 추가하는 방법

EDIT 전체 MainActivity.java를 포함하는

package com.sonnyparlin.gracietampa; 

import java.util.Locale; 
import android.app.ActionBar; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.text.Html; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class MainActivity extends FragmentActivity implements 
    ActionBar.TabListener { 

    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    ViewPager mViewPager; 

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

     // Set up the action bar. 
     final ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(
       getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     // When swiping between different sections, select the corresponding 
     // tab. We can also use ActionBar.Tab#select() to do this if we have 
     // a reference to the Tab. 
     mViewPager 
       .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
        @Override 
        public void onPageSelected(int position) { 
         actionBar.setSelectedNavigationItem(position); 
        } 
       }); 

     // For each of the sections in the app, add a tab to the action bar. 
     for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
      // Create a tab with text corresponding to the page title defined by 
      // the adapter. Also specify this Activity object, which implements 
      // the TabListener interface, as the callback (listener) for when 
      // this tab is selected. 
      actionBar.addTab(actionBar.newTab() 
        .setText(mSectionsPagerAdapter.getPageTitle(i)) 
        .setTabListener(this)); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public void onTabSelected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
     // When the given tab is selected, switch to the corresponding page in 
     // the ViewPager. 
     mViewPager.setCurrentItem(tab.getPosition()); 
    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, 
     FragmentTransaction fragmentTransaction) { 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

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

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
     // Return a DummySectionFragment (defined as a static inner class 
      // below) with the page number as its lone argument. 
      Fragment fragment = new DummySectionFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public int getCount() { 
     // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      switch (position) { 
      case 0: 
       return getString(R.string.title_section1).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_section2).toUpperCase(l); 
      case 2: 
       return getString(R.string.title_section3).toUpperCase(l); 
      } 
      return null; 
     } 
    } 

    /** 
    * A dummy fragment representing a section of the app, but that simply 
    * displays dummy text. 
    */ 
    public static class DummySectionFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     public static final String ARG_SECTION_NUMBER = "section_number"; 

     public DummySectionFragment() { 
     } 

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

      if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) { 
       rootView = inflater.inflate(R.layout.fragment_main_dummy, 
          container, false); 
       TextView dummyTextView = (TextView) rootView 
         .findViewById(R.id.section_label); 
        dummyTextView.setText(Html.fromHtml(getString(R.string.page1text))); 
      } else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) { 


        // I want to add my listview here 


      } else { 
       rootView = inflater.inflate(R.layout.fragment_main_dummy, 
         container, false); 
       TextView dummyTextView = (TextView) rootView 
         .findViewById(R.id.section_label); 
       dummyTextView.setText(Integer.toString(getArguments().getInt(
         ARG_SECTION_NUMBER))); 
      } 
      return rootView; 
     } 
    } 

} 

내 TechniqueActivity.java 파일 : 누군가가 올바른 방향으로 날 지점 수 있다면

public class TechniqueActivity extends ListActivity{ 

    public TechniqueActivity() { 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // storing string resources into Array 
     String[] technique_list = getResources().getStringArray(R.array.technique_list); 

     // Binding resources Array to ListAdapter 
     this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, technique_list)); 

    } 

} 

난 정말 감사하겠습니다 있도록 I 내 응용 프로그램의 두 번째 탭에 TechniqueActivity.java에서 만든 목록보기를 채울 수 있습니다. 아니면 완전히 다른/더 나은 방법이 있을까요?

답변

1

실제로 어떤 탭을 사용하고있는 것처럼 보이지 않습니다. 기본 아이디어는 this tutorial을 확인하십시오. 탭을 사용하려면 (1) 탭을 만든 다음 (2) 해당 탭에서로드 할 활동을 설정해야합니다. 위의 코드는 하나의 액티비티를 만든 다음 해당 레이아웃을 부 풀리는 것처럼 보입니다. 블로그 게시물은 간단한 3 탭 응용 프로그램에 대한 좋은 개요를 제공하며이 탭 응용 프로그램을 사용하여 자신의 사례에 적용 할 수 있습니다.

이렇게하려면 ViewPager을 사용하는 것이 좋습니다. read more about here을 사용할 수 있습니다. ViewPager 접근 방식은 이 아니라 Fragment을 사용합니다. 기본적으로 ViewPager 및 어댑터의 인스턴스를 추가하는 어댑터를 만듭니다. 스 와이프하면 ViewPager이 어댑터에 추가 한 순서대로 조각을 회전합니다.

이러한 상황은이 상황에 가장 근접한 2 가지 방법으로 보입니다.

1

좋아요, 더 많은 조사와 놀고 난 후에, 저는 Rarw가 대답 대신에 Activity 대신 Fragment를 사용하는 두 번째 부분에서 말한 해결책을 발견했습니다. 그래서 TechniqueActivity.java를 제거하고 대신 TechniqueFragment를 만들었습니다. 그런 다음 getItem()에서 호출합니다.

package com.sonnyparlin.gracietampa; 

import java.util.Locale; 

import android.app.ActionBar; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.text.Html; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class MainActivity extends FragmentActivity implements 
     ActionBar.TabListener { 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
    * will keep every loaded fragment in memory. If this becomes too memory 
    * intensive, it may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    ViewPager mViewPager; 

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

     // Set up the action bar. 
     final ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(
       getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     // When swiping between different sections, select the corresponding 
     // tab. We can also use ActionBar.Tab#select() to do this if we have 
     // a reference to the Tab. 
     mViewPager 
       .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
        @Override 
        public void onPageSelected(int position) { 
         actionBar.setSelectedNavigationItem(position); 
        } 
       }); 

     // For each of the sections in the app, add a tab to the action bar. 
     for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
      // Create a tab with text corresponding to the page title defined by 
      // the adapter. Also specify this Activity object, which implements 
      // the TabListener interface, as the callback (listener) for when 
      // this tab is selected. 
      actionBar.addTab(actionBar.newTab() 
        .setText(mSectionsPagerAdapter.getPageTitle(i)) 
        .setTabListener(this)); 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public void onTabSelected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
     // When the given tab is selected, switch to the corresponding page in 
     // the ViewPager. 
     mViewPager.setCurrentItem(tab.getPosition()); 
    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

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

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a DummySectionFragment (defined as a static inner class 
      // below) with the page number as its lone argument. 

      Fragment fragment; 
      if (position == 0 || position == 2) { 
       fragment = new DummySectionFragment(); 
       Bundle args = new Bundle(); 
       args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
       fragment.setArguments(args); 
      } else { 
       fragment = new TechniqueFragment(); 
       //setContentView(R.layout.technique_activity); 
      } 
      return fragment; 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      switch (position) { 
      case 0: 
       return getString(R.string.title_section1).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_section2).toUpperCase(l); 
      case 2: 
       return getString(R.string.title_section3).toUpperCase(l); 
      } 
      return null; 
     } 
    } 

    /** 
    * A dummy fragment representing a section of the app, but that simply 
    * displays dummy text. 
    */ 
    public static class DummySectionFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     public static final String ARG_SECTION_NUMBER = "section_number"; 

     public DummySectionFragment() { 
     } 

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

      if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) { 
       rootView = inflater.inflate(R.layout.fragment_main_dummy, 
          container, false); 
       TextView dummyTextView = (TextView) rootView 
         .findViewById(R.id.section_label); 
       dummyTextView.setText(Html.fromHtml(getString(R.string.page1text))); 
      } else { 
       rootView = inflater.inflate(R.layout.fragment_main_dummy, 
         container, false); 
       TextView dummyTextView = (TextView) rootView 
         .findViewById(R.id.section_label); 
       dummyTextView.setText(Integer.toString(getArguments().getInt(
         ARG_SECTION_NUMBER))); 
      } 
      return rootView; 
     } 
    } 

    public static class TechniqueFragment extends Fragment { 

     public TechniqueFragment() { 
      // TODO Auto-generated constructor stub 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      View rootView; 

      rootView = inflater.inflate(R.layout.technique_activity, 
        container, false); 

      ListView lv1 = (ListView) rootView.findViewById(R.id.ListView01); 
      String[] technique_list = getResources().getStringArray(R.array.technique_list); 
      lv1.setAdapter(new ArrayAdapter<String>(lv1.getContext(), R.layout.list_item, technique_list)); 

      return rootView; 
     } 

    } 

} 
+0

다행 이네 – Rarw

관련 문제