我在 PagerAdapter 下使用微调器玩得很开心。请查看提供您可能有的任何建议。我只是树桩,可能为时已晚。我试图将代码包装到单独的类中,然后调用该类但没有成功。接下来我想我需要做的就是getActivity()
从片段中添加,这也是我遇到的问题。
public class NewSRO extends PagerAdapter {
//---return the total number of pages---
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
View view = null; // setting view variable with null value
switch (position) {
case 0:
resId = R.layout.new_client_1;
view = inflater.inflate(resId, null); // inflating view to attach spinner data
final Spinner s1_clients;
final String[] clients = {
"client1",
"client2",
"3",
"4"
};
//---Spinner View---
s1_clients = (Spinner) view.findViewById(R.id.spinner_client_select);
SpinnerAdapter new_adapter = new ArrayAdapter<String>(
NewSRO.this,
android.R.layout.simple_spinner_item,
clients);
s1_clients.setAdapter(new_adapter);
s1_clients.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = s1_clients.getSelectedItemPosition(); Toast.makeText(null, "You have selected item : " + clients[index],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
break;
case 1:
resId = R.layout.new_client_1;
break;
case 2:
resId = R.layout.new_client_1;
break;
}
//View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}