2014-02-06 89 views
-1

對不起,因爲我的英語水平低。 我有一個包含viewpager,4 ListView listview的活動。 我怎麼能實現一個監聽器來知道用戶觸摸哪個項目? 我在網上搜索,整天未果android viewpager with listview,如何爲每個listview實現OnItemClickListener?

代碼片段:

public class Local_mostrar_menuActivity extends ActionBarActivity { 

private ViewPager viewPager; 
private ListView page1; 
private ListView page2; 
private ListView page3; 
private ListView page4; 
ArrayList<ItemListView> itemsListVMenuBEBIDAS = obtenerItemsMenuBEBIDAS(); 
ItemListViewAdapter adapterlistvMenuBEBIDAS = new ItemListViewAdapter(this,  
itemsListVMenuBEBIDAS); 
ArrayList<ItemListView> itemsListVMenuPRIMEROS = obtenerItemsMenuPRIMEROS(); 
ItemListViewAdapter adapterlistvMenuPRIMEROS = new ItemListViewAdapter(this, itemsListVMenuPRIMEROS); 

ArrayList<ItemListView> itemsListVMenuSEGUNDOS = obtenerItemsMenuSEGUNDOS(); 
ItemListViewAdapter adapterlistvMenuSEGUNDOS = new ItemListViewAdapter(this, itemsListVMenuSEGUNDOS); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_local_mostrar_menu); 

繼續: // viewpagerindicator viewpager viewPager =(ViewPager)findViewById(R.id.pager); viewPager.setAdapter(new MainPageAdapter());

  TitlePageIndicator titleIndicator = (TitlePageIndicator)findViewById(R.id.indicator); 

      //TabPageIndicator titleIndicator = (TabPageIndicator)findViewById(R.id.indicator); 
      titleIndicator.setViewPager(viewPager);  
      titleIndicator.setOnPageChangeListener(new OnPageChangeListener() { 

       @Override 
       public void onPageSelected(int position) 
       { 

       } 

       @Override 
       public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) 
       { 

       } 

       @Override 
       public void onPageScrollStateChanged(int state) 
       { 

       } 
      });  




} 

//ActionBar 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 

//Viewpager 
class MainPageAdapter extends PagerAdapter 
{ 

    @Override 
    public CharSequence getPageTitle(int position) 
    { 
     String title = null; 

     switch (position) 
     { 
     case 0: 
      title = "Bebida"; 
      break; 
     case 1: 
      title = "Primeros"; 
      break; 
     case 2: 
      title = "Segundos"; 
      break; 

     default: 
      title = "Postre o café"; 
      break; 
     } 
     return title; 
    } 

    @Override 
    public int getCount() 
    { 
     return 4; 
    } 

    @Override 
    public Object instantiateItem(ViewGroup collection, final int position) 
    { 
     View page = null; 
     switch (position) 
     { 
     case 0: 
      if (page1 == null) 
      { 
       page1 = (ListView) LayoutInflater.from(Local_mostrar_menuActivity.this).inflate(R.layout.page_one_viewpager_listview, null); 
       page1.setAdapter(adapterlistvMenuBEBIDAS); 


      } 
      page = page1; 
      break; 
     case 1: 
      if (page2 == null) 
      { 
       page2 = (ListView) LayoutInflater.from(Local_mostrar_menuActivity.this).inflate(R.layout.page_two_viewpager_listview, null); 

       page2.setAdapter(adapterlistvMenuPRIMEROS); 
      } 
      page = page2; 
      break; 
     case 2: 
      if (page3 == null) 
      { 
       page3 = (ListView) LayoutInflater.from(Local_mostrar_menuActivity.this).inflate(R.layout.page_three_viewpager_listview, null); 

       page3.setAdapter(adapterlistvMenuSEGUNDOS); 
      } 
      page = page3; 
      break; 
     default: 
      if (page4 == null) 
      { 
       page4 = (ListView) LayoutInflater.from(Local_mostrar_menuActivity.this).inflate(R.layout.page_four_viewpager_listview, null); 
      } 
      page = page4; 
      break; 
     } 

     ((ViewPager) collection).addView(page, 0); 

     return page; 

    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) 
    { 
     return view == object; 
    } 

    @Override 
    public void destroyItem(View collection, int position, Object view) 
    { 
     ((ViewPager) collection).removeView((View) view); 
    } 
} 
private ArrayList<ItemListView> obtenerItemsMenuBEBIDAS() { 
    ArrayList<ItemListView> itemsMenuBEBIDAS = new ArrayList<ItemListView>(); 
    itemsMenuBEBIDAS.add(new ItemListView(1, "Cerveza DAMM Inedit", "La cerveza creada en El Bulli", "drawable/damm_inedit")); 
    itemsMenuBEBIDAS.add(new ItemListView(1, "Tercio de cerveza DAMM", "330ml", "drawable/damm")); 
    return itemsMenuBEBIDAS; 
} 
private ArrayList<ItemListView> obtenerItemsMenuPRIMEROS() { 
    ArrayList<ItemListView> itemsMenuPRIMEROS = new ArrayList<ItemListView>(); 


    itemsMenuPRIMEROS.add(new ItemListView(1, "Ensalada cesar", "Lechuga, panecillos tostados,increible salsa cesar, tomate", "drawable/ensaladacesar01")); 
    itemsMenuPRIMEROS.add(new ItemListView(2, "Ensalada del cheff", "Ensalada con una salsa secreta","drawable/ensaladadelchef01")); 

    return itemsMenuPRIMEROS; 
} 
// Añadiendo elementos al 
private ArrayList<ItemListView> obtenerItemsMenuSEGUNDOS() { 
    ArrayList<ItemListView> itemsMenuSEGUNDOS = new ArrayList<ItemListView>(); 

    itemsMenuSEGUNDOS.add(new ItemListView(1, "Patatas con chorizo", "Finas patatas con chorizo manchego", "drawable/bravas01")); 
    itemsMenuSEGUNDOS.add(new ItemListView(2, "Ternequeso", "Filete de ternera con mozzarella fundida y una salsa especial de tomate","drawable/calamares01")); 
    return itemsMenuSEGUNDOS; 
} 

}

回答

0

你應該設定一個點擊監聽,每個ListView控件

例如:

page1.setOnItemClickListener(new OnItemClickListener(){ 
    do something 
}); 

做你膨脹的佈局後(如果你是如此的使用片段onViewCreated例如)

+0

非常感謝你,我已經能夠感謝你的建議。我很高興。 這個問題之前已經完成了,沒有人說什麼,但是很清楚。也許我的英語水平低,或者因爲我解釋了邪惡。謝謝你的朋友 –

+0

你很歡迎,很高興我能幫忙( - : – royB

相關問題