选项卡片段更改操作栏微调器

时间:2014-12-08 06:46:11

标签: android android-fragments android-actionbar android-spinner

我有片段活动,有标签片段。像这样。

enter image description here

但是当我点击第二个标签时,操作栏上的微调器会变空,就像这样。

enter image description here

我认为这是一个常见问题,但如果不告诉我,我也会提供一些代码。

我没有在标签更改上做任何与之相关的事情,但我的代码可能对你有帮助。

mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

    @Override
    public void onTabChanged(String arg0) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    InterestTabFragment tabOneFrgment = (InterestTabFragment) fragmentManager
                .findFragmentByTag("tab1");

    InteractionTabFragment tabTwoFrgment = (InteractionTabFragment) fragmentManager
                            .findFragmentByTag("tab2");

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    if (tabOneFrgment != null && tabOneFrgment.isVisible()) {
        if (isInterestDataChanged) {                            
            // delete cache filed of interest
            }
        }else if(tabTwoFrgment != null && tabTwoFrgment.isVisible()){
            if (isInteractionDataChanged) {                         
            // delete cache filed of interaction

            }
        }else{
        if (isHangoutDataChanged) {
            // delete cache filed of hangout
            }

            }
                    fragmentTransaction.commit();
                }
            });

这是在OnCreateOptionMenu

MenuItem item = menu.findItem(R.id.spListOpt);
spinner = (Spinner) MenuItemCompat.getActionView(item);
spinner.setAdapter(YearSpinnerAdapter);
spinner.setBackgroundResource(R.drawable.spinner);

这是对数据的调用,我在其后执行代码中设置了适配器。

    class joingYearCall extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... uri) {
        String responseString = null;
        responseString = HttpManager.GetData(uri[0]);           
            JSONObject joiningResult;
            JSONObject year;
            try {
                joiningResult = new JSONObject(responseString);
                year = joiningResult.getJSONObject("ProfileInfoResult");
                joiningYear = Integer.parseInt(year.getString("joiningYear"));
                items.clear();
                Calendar now = Calendar.getInstance();
                int currentYear = now.get(Calendar.YEAR);
                items.add("All");
                while(currentYear >= joiningYear)
                {
                    items.add(String.valueOf(currentYear--));
                }
            } catch (JSONException e) {                 
                e.printStackTrace();
            }
        }
        return "";
    }

    @Override
    protected void onPostExecute(String result) {
        spinner.setAdapter(YearsSpinnerAdapter);
    }

    @Override
    protected void onPreExecute() {
    }
}

这是我的适配器。

    private SpinnerAdapter YearsSpinnerAdapter = new BaseAdapter() {

    private TextView text;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflator.inflate(R.layout.spinner_hint_item, null);
        }
        text = (TextView) convertView.findViewById(R.id.spinnerTarget);
        text.setSingleLine(true);
        if (!selected) {
            Drawable img = getResources().getDrawable( R.drawable.time_stalk);
            img.setBounds( 0, 0, 30, 30 );
            text.setCompoundDrawables( img, null, null, null );
        } else {
            text.setText(items.get(position));
        }
        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public int getCount() {
        return items.size();
    }

    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflator.inflate(R.layout.spinner_item, null);
        }
        text = (TextView) convertView.findViewById(R.id.spinnerTarget);
        text.setSingleLine(true);
        text.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                selected = true;
                ((BaseAdapter) YearsSpinnerAdapter).notifyDataSetChanged();
                return false;
            }
        });
        text.setText(items.get(position));
        return convertView;
    };
};

0 个答案:

没有答案