Github代码:https://github.com/jjvang/AnimationDemo
您好,我不确定这是否可行,但我想将主要活动中的新数据发送到我的类,该类扩展了PagerAdapter。这个应用程序只是一个具有自定义适配器的ViewPager的简单演示。
我目前想要单击主活动上的按钮,然后ViewPager的信息将更改为新的。
我理解如何使用意图将信息从一个活动发送到另一个活动,但这似乎并不相同,因为它不会进入新的活动。
请告知。
谢谢!
MainActivity:
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
customSwip customSwip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager)findViewById(R.id.viewPager);
customSwip=new customSwip(this);
viewPager.setAdapter(customSwip);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// SOMEHOW PASS THESE DATA TO customSWIP to set new values
int [] imageResources ={R.drawable.capture1,R.drawable.capture2,R.drawable.capture3,R.drawable.capture4,R.drawable.capture5};
String[] imageText = {"new", "two", "three", "four", "five"};
String[] imageHmong = {"new", "new", "new", "new", "new"};
}
});
}
}
customSwip:
public class customSwip extends PagerAdapter {
private int [] imageResources ={R.drawable.capture1,R.drawable.capture2,R.drawable.capture3,R.drawable.capture4,R.drawable.capture5};
private String[] imageText = {"one", "two", "three", "four", "five"};
private String[] imageDifferent = {"61236", "612361", "612361", "612361", "612361"};
private Context ctx;
private LayoutInflater layoutInflater;
public customSwip(Context c) {
ctx=c;
}
@Override
public int getCount() {
return imageResources.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView=layoutInflater.inflate(R.layout.activity_custom_swip,container,false);
ImageView imageView=(ImageView) itemView.findViewById(R.id.swip_image_view);
TextView textHmong=(TextView) itemView.findViewById(R.id.different);
TextView textEnglish=(TextView) itemView.findViewById(R.id.english);
imageView.setImageResource(imageResources[position]);
final String dummytext = imageText[position];
textHmong.setText(imageDifferent[position]);
textEnglish.setText(imageText[position]);
container.addView(itemView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ctx, "" + dummytext, Toast.LENGTH_SHORT).show();
}
});
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view==object);
}
}
答案 0 :(得分:0)
为要在adapter.like imageresource,text中显示的所有数据创建一个pojo类。 现在这里的YourData就是那个pojo类。 所以现在你在适配器和适配器中使用相同的数据。 使用您想要的数据创建customSwip的构造函数。
public class customSwip extends PagerAdapter {
YourPojo yourPojo;
private int [] imageResources ={R.drawable.capture1,R.drawable.capture2,R.drawable.capture3,R.drawable.capture4,R.drawable.capture5};
private String[] imageText = {"one", "two", "three", "four", "five"};
private String[] imageDifferent = {"61236", "612361", "612361", "612361", "612361"};
private Context ctx;
private LayoutInflater layoutInflater;
public customSwip(Context c,YourPojo yourPojo) {
ctx=c;
this.yourPojo=yourPojo;
}
@Override
public int getCount() {
return imageResources.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView=layoutInflater.inflate(R.layout.activity_custom_swip,container,false);
ImageView imageView=(ImageView) itemView.findViewById(R.id.swip_image_view);
TextView textHmong=(TextView) itemView.findViewById(R.id.different);
TextView textEnglish=(TextView) itemView.findViewById(R.id.english);
imageView.setImageResource(imageResources[position]);
final String dummytext = imageText[position];
textHmong.setText(imageDifferent[position]);
textEnglish.setText(imageText[position]);
container.addView(itemView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ctx, "" + dummytext, Toast.LENGTH_SHORT).show();
}
});
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view==object);
}
}
在适配器中创建一个方法。 当您使用适配器对象单击MainActivity调用方法中的按钮时,您可以在其中更新viewPagers数据。