ViewPager 上的按钮返回错误值
Button on ViewPager returning wrong value
我有一个使用 PagerAdapter 的 ViewPager。在页面上,我有一个 TextView 和一个 Button。我希望按钮从 DataRecord 中提取相关数据(我显然知道我可以从 TextView 中获取它,但这只是我更复杂代码的简化版本,用于说明目的)
ViewPager 工作正常,我可以按预期在屏幕之间滑动。但是,单击该按钮时,它不会显示来自 PagerAdapter 的正确数据。
例如,第一页按预期显示包含 "Data Item 1" 的 TextView,但单击第一页上的按钮 Log.d 显示 "Data Item 3"
我很确定这与寻呼机存储当前不在屏幕上的数据的方式有关,
但是不知道怎么解决?
StartActivity 类
public class StartActivity extends Activity {
FullScreenImageAdapter adapter;
ViewPager viewPager;
ArrayList<DataRecord> datalist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);
datalist = new ArrayList<DataRecord>();
viewPager = (ViewPager) findViewById(R.id.pager);
DataRecord dataRecord = new DataRecord("Data Item 1 ");
datalist.add(dataRecord);
dataRecord = new DataRecord("Data Item 2 ");
datalist.add(dataRecord);
dataRecord = new DataRecord("Data Item 3");
datalist.add(dataRecord);
adapter = new FullScreenImageAdapter(this, datalist);
viewPager.setAdapter(adapter);
}
public int getCurrentPage() {
return viewPager.getCurrentItem();
}
}
FullScreenImageAdapter 类
public class FullScreenImageAdapter extends PagerAdapter {
private Activity _activity;
private LayoutInflater inflater;
private ArrayList<DataRecord> dataRecords;
public String info;
public FullScreenImageAdapter (Activity activity,
ArrayList<DataRecord> dataRecords) {
this._activity = activity;
this.dataRecords = dataRecords;
}
@Override
public int getCount() {
return this.dataRecords.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(final ViewGroup container, int position) {
Button btn;
TextView textField;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.main_screen, container,
false);
btn = (Button) viewLayout.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Log.d("Info - ", info);
}
catch (ActivityNotFoundException e) {
Log.d("Error", e.toString());
}
}
});
textField = (TextView) viewLayout.findViewById(R.id.text_field);
info = dataRecords.get(position).information;
textField.setText(info);
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
使用 btn.setTag
和 v.getTag
获取当前按钮的点击值:
info = dataRecords.get(position).information;
btn.setTag(info);
然后单击按钮从按钮取回值:
public void onClick(View v) {
try {
Log.d("Info - ", (String)v.getTag());
}
catch (ActivityNotFoundException e) {
Log.d("Error", e.toString());
}
}
我有一个使用 PagerAdapter 的 ViewPager。在页面上,我有一个 TextView 和一个 Button。我希望按钮从 DataRecord 中提取相关数据(我显然知道我可以从 TextView 中获取它,但这只是我更复杂代码的简化版本,用于说明目的)
ViewPager 工作正常,我可以按预期在屏幕之间滑动。但是,单击该按钮时,它不会显示来自 PagerAdapter 的正确数据。
例如,第一页按预期显示包含 "Data Item 1" 的 TextView,但单击第一页上的按钮 Log.d 显示 "Data Item 3"
我很确定这与寻呼机存储当前不在屏幕上的数据的方式有关, 但是不知道怎么解决?
StartActivity 类
public class StartActivity extends Activity {
FullScreenImageAdapter adapter;
ViewPager viewPager;
ArrayList<DataRecord> datalist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);
datalist = new ArrayList<DataRecord>();
viewPager = (ViewPager) findViewById(R.id.pager);
DataRecord dataRecord = new DataRecord("Data Item 1 ");
datalist.add(dataRecord);
dataRecord = new DataRecord("Data Item 2 ");
datalist.add(dataRecord);
dataRecord = new DataRecord("Data Item 3");
datalist.add(dataRecord);
adapter = new FullScreenImageAdapter(this, datalist);
viewPager.setAdapter(adapter);
}
public int getCurrentPage() {
return viewPager.getCurrentItem();
}
}
FullScreenImageAdapter 类
public class FullScreenImageAdapter extends PagerAdapter {
private Activity _activity;
private LayoutInflater inflater;
private ArrayList<DataRecord> dataRecords;
public String info;
public FullScreenImageAdapter (Activity activity,
ArrayList<DataRecord> dataRecords) {
this._activity = activity;
this.dataRecords = dataRecords;
}
@Override
public int getCount() {
return this.dataRecords.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(final ViewGroup container, int position) {
Button btn;
TextView textField;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.main_screen, container,
false);
btn = (Button) viewLayout.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Log.d("Info - ", info);
}
catch (ActivityNotFoundException e) {
Log.d("Error", e.toString());
}
}
});
textField = (TextView) viewLayout.findViewById(R.id.text_field);
info = dataRecords.get(position).information;
textField.setText(info);
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
使用 btn.setTag
和 v.getTag
获取当前按钮的点击值:
info = dataRecords.get(position).information;
btn.setTag(info);
然后单击按钮从按钮取回值:
public void onClick(View v) {
try {
Log.d("Info - ", (String)v.getTag());
}
catch (ActivityNotFoundException e) {
Log.d("Error", e.toString());
}
}