我现在使用从http://viewpagerindicator.com/下载的 viewpagerindicator 。现在我可以在 viewpage 适配器中创建两个静态页面,我可以在两个页面之间交换。但是,我无法管理如何处理不同页面中的不同活动。例如。第 1 页是图像,我希望人们单击图像,然后他们将开始新的活动。然后第 2 页是列表视图。我想要单击列表的人,然后我可以显示与列表项相关的不同内容。这是适配器中的源代码。希望任何人都可以帮助我,非常感谢并祝愿你们所有人。我只是一个初学者......
public class ViewPagerAdapter extends PagerAdapter
{
private static String[] titles = new String[]
{
"A",
"B",
};
private final Context context;
public ViewPagerAdapter( Context context )
{
this.context = context;
}
@Override
public String getPageTitle( int position )
{
return titles[ position ];
}
@Override
public int getCount()
{
return titles.length;
}
@Override
public Object instantiateItem( View pager, int position )
{
if (position == 0)
{
ImageView v = new ImageView(context);
v.setImageResource(R.drawable.am);
((ViewPager)pager).addView( v, 0 );
return v;
}else if (position==1)
{
ListView v = new ListView( context );
String[] from = new String[] { "str" };
int[] to = new int[] { android.R.id.text1 };
List<Map<String, String>> items =
new ArrayList<Map<String, String>>();
for ( int i = 0; i < 20; i++ )
{
Map<String, String> map =
new HashMap<String, String>();
map.put( "str", String.format( "Item %d", i + 1 ) );
items.add( map );
}
SimpleAdapter adapter = new SimpleAdapter( context, items,
android.R.layout.simple_list_item_1, from, to );
v.setAdapter( adapter );
( (ViewPager) pager ).addView( v, 0 );
v.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent a =new Intent(context,SecondActivity.class);<---(dont know how to start new activity)
this.startActivity(a); <-----(dont know how to start new activity, idea is to start new activity here)
}
});
((ViewPager)pager).addView( v, 0 );
return v;
}
@Override
public void destroyItem( View pager, int position, Object view )
{
if (position == 0)
{
((ViewPager)pager).removeView( (ImageView)view );
}else if (position == 1)
{
((ViewPager)pager).removeView( (ListView)view );
}else
{
((ViewPager)pager).removeView( (TextView)view );
}
}
我的主要是:
公共类 MainActivity 扩展 Activity {
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
ViewPagerAdapter adapter = new ViewPagerAdapter( this );
ViewPager pager =
(ViewPager)findViewById( R.id.viewpager );
TitlePageIndicator indicator =
(TitlePageIndicator)findViewById( R.id.indicator );
pager.setAdapter( adapter );
indicator.setViewPager( pager );
}
}
布局xml是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>