1

我在 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;
    }
}
4

1 回答 1

1

起初,似乎添加 collection.getContext() 是正确的方向。尽管如此,它只是让我停止关注最终是问题的膨胀视图。每个 case 语句都需要一个视图来膨胀,没有它,应用程序就会崩溃。

基本上我需要在每个案例语句中添加以下内容:

view = inflater.inflate(resId, null);

然后我需要用 view.getContext() 修改我的 SpinnerAdapter :

  SpinnerAdapter new_adapter = new ArrayAdapter<String>(
                  view.getContext(),
                  android.R.layout.simple_spinner_item,
                  clients);     

我希望这对其他人有所帮助,因为 viewPager 是增强用户界面的好方法。

干杯,白兰地李卡马乔

于 2013-09-06T20:29:57.930 回答