对不起,我的英语水平低。我有一个由ViewPager,4 ListView ListView组成的活动。我如何实现侦听器以知道用户触摸哪个项目?我整天搜索了网络,失败了
代码片段:
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;
}
}
您应该为每个listView
设置单击侦听器例如:
page1.setOnItemClickListener(new OnItemClickListener(){
do something
});
在夸大布局后(如果您使用的片段上的片段,则可以在视图上进行)