在android中工作我已经按照在线教程创建了一个视图寻呼机,我想以编程方式添加文本。我已成功创建了大量添加4个视图,但是当我在视图之间滚动时,我总是得到相同的视图。我的代码如下。有没有办法让初始视图简单地说是Page1然后在滚动时它会说出Page2等等。
修改
为了澄清,我已经相当简化了我想要做的事情,最终目标是每个"视图"与众不同。例如,第1页包含textview,第2页包含图像视图,第3页包含一些较大的文本。
Page 1 = "This is page 1"
Page 2 = An image of a sheep
Page 3 = Big red text saying "BANG!"
XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="300dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/myImage"
android:id="@+id/imageView13" />
</LinearLayout>
</RelativeLayout>
爪哇
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class Testing extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testing);
ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(new MyPagerAdapter(this));
}
}
class MyPagerAdapter extends PagerAdapter {
private Context ctx;
public MyPagerAdapter(Context context) {
ctx = context;
}
// Set number of slides
@Override
public int getCount() {
return 4;
}
@Override
public Object instantiateItem(View collection, int position) {
TextView tv = new TextView(ctx);
tv.setTextSize(50);
tv.setTextColor(Color.WHITE);
tv.setText("PAGE1");
((ViewPager) collection).addView(tv);
return tv;
}
// Destroy
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
// View from object
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
// Parceable
@Override
public Parcelable saveState() {
return null;
}
// Restore state
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
//Start Update
@Override
public void startUpdate(View arg0) {
}
// Finish update
@Override
public void finishUpdate(View arg0) {
}
}
答案 0 :(得分:0)
位置更改...
@Override
public Object instantiateItem(View collection, int position) {
if(position==0){
TextView tv = new TextView(ctx);
tv.setTextSize(50);
tv.setTextColor(Color.WHITE);
tv.setText("PAGE "+position);
collection.addView(tv);
return tv;
}else if(position==1){
ImageView iv = new ImageView(ctx);
iv.setImageResource(R.mipmap.ic_launcher);
collection.addView(iv);
return iv;
}else if(position==2){
TextView tv = new TextView(ctx);
tv.setTextSize(50);
tv.setTextColor(Color.WHITE);
tv.setText("BANG");
collection.addView(tv);
return tv;
}else if(position==3){
ImageView iv = new ImageView(ctx);
iv.setImageResource(R.mipmap.ic_launcher);
collection.addView(tv);
return iv;
}