代码之家  ›  专栏  ›  技术社区  ›  Dr.Amit Jain

Android:动态视图寻呼机仅显示第一个片段中的更改

  •  0
  • Dr.Amit Jain  · 技术社区  · 10 年前

    我对android非常陌生,我正在尝试动态创建滑动选项卡,并将可滑动页面(即片段)添加到视图寻呼机。 我看了一些教程,并尝试了一些方法。 我在下面粘贴相同的代码。

    这是我要添加到viewpager的片段类

    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import technovators.mahendraprophecy.R;
    
    /**
     * A simple {@link android.app.Fragment} subclass.
     */
    public class SUB_LIST_FRAGMENT extends android.support.v4.app.Fragment {
    
        static String category;
    
        public SUB_LIST_FRAGMENT() {
            // Required empty public constructor
        }
        public static final SUB_LIST_FRAGMENT newInstance(String message)
    
        {
            category=message;
            SUB_LIST_FRAGMENT f = new SUB_LIST_FRAGMENT();
            Bundle bdl = new Bundle(1);
            return f;
        }
    
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState)
        {
            super.onActivityCreated(savedInstanceState);
            Toast.makeText(getActivity(),category,Toast.LENGTH_LONG).show();
            ((TextView) getActivity().findViewById(R.id.cat)).setText(category);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.sub_list_fragment, container, false);
        }
    
    }
    

    这是一个主要的片段,它带有标签和一个附加的视图寻呼机

    public class LATEST_NEWSLETTERS extends android.support.v4.app.Fragment {
    
    
        MaterialTabs subTabs;
        ViewPager subPager;
        private String[] titles = new String[]{"Weekly Newsletter", "Market News & Updates", "Day Trade Flash News"};
        ArrayList<String> headers,header_colors,category_ids;
        List<Fragment> fList;
        public LATEST_NEWSLETTERS()
        {
    
        }
    
        public LATEST_NEWSLETTERS(String categoryData)
        {
            try
            {
                JSONObject object=new JSONObject(categoryData);
                JSONArray array=object.getJSONArray("categories");
                headers=new ArrayList<>();
                header_colors=new ArrayList<>();
                category_ids=new ArrayList<>();
                fList = new ArrayList<Fragment>();
    
                for(int i=0;i<array.length();i++)
                {
                    object=array.getJSONObject(i);
                    headers.add(object.getString("name"));
                    header_colors.add(object.getString("bgcolor"));
                    category_ids.add(object.getString("cat_id"));
                    fList.add(SUB_LIST_FRAGMENT.newInstance(object.getString("cat_id")));
                }
            }
            catch (JSONException e)
            {
                e.printStackTrace();
            }
    
        }
    
        void setupTabs()
        {
            subTabs= (MaterialTabs) getActivity().findViewById(R.id.subTabs);
            subPager= (ViewPager) getActivity().findViewById(R.id.subPager);
            latestNewLetterAdapter adapter=new latestNewLetterAdapter(getFragmentManager());
            subPager.setAdapter(adapter);
            subTabs.setViewPager(subPager);
            subTabs.setIndicatorColor(Color.parseColor(header_colors.get(0)));
            subTabs.setTextColorSelected(Color.parseColor(header_colors.get(0)));
    
            subPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
            {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }
    
                @Override
                public void onPageSelected(int position)
                {
                    subTabs.setIndicatorColor(Color.parseColor(header_colors.get(position)));
                }
    
                @Override
                public void onPageScrollStateChanged(int state) {
    
                }
            });
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.mainfragment_latest_newsletters, container, false);
        }
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setupTabs();
        }
    
    
        class latestNewLetterAdapter extends FragmentStatePagerAdapter implements MaterialTabs.CustomTabProvider
        {
            public latestNewLetterAdapter(FragmentManager fm)
            {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int position)
            {
                return fList.get(position);
            }
    
    
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                // TODO Auto-generated method stub
    
            }
    
    /*
            @Override
            public CharSequence getPageTitle(int position)
            {
                String x=headers.get(position);
                if(x.contains("&"))
                {
                  x=x.replaceAll("&amp;"," & ");
                }
                return x;
            }
    */
            @Override
            public int getCount()
            {
                return headers.size();
            }
    
            @Override
            public View getCustomTabView(ViewGroup viewGroup, int i) {
                TextView header=new TextView(getActivity());
                String x=headers.get(i);
                if(x.contains("&"))
                {
                    x=x.replaceAll("&amp;"," & ");
                }
                header.setTextColor(Color.parseColor(header_colors.get(i)));
                header.setText(x);
                header.setGravity(Gravity.CENTER);
                return header;
            }
        }
    }
    

    我在上一个活动中使用异步任务从服务器检索json(json数组)数据,并在构造函数中以字符串的形式将其传递给这个片段。

    适配器用标题填充选项卡,并从片段类的自定义构造函数中创建的片段列表中获取项目。我还了解到,使用自定义构造函数不是一种好的做法,我将实现用于相同目的的接口,但这不是主要问题。

    问题

    如果你看到了,我正在将类别id传递给viewpager的每个片段,它应该在每个片段中设置相应的类别,但这里发生的情况是,它只是在第一个片段中设置id,或者将其称为第一个选项卡rest。所有片段只显示文本“类别id”,而不是当我滑动到第二个片段时要设置的类别,第二个碎片的类别id被设置在第一个片段中,当我滑动到第三个片段时,第三个的类别id设置为第一个

    所以基本上所有的动作都只发生在第一个片段中,而不是全部。

    请帮帮我,我哪里错了? 我需要添加动态页面,因为类别的数量每次都会变化,因此页面的数量应该添加到视图寻呼机中,并且每个页面中都会加载一个与该页面的类别id相对应的列表。

    1 回复  |  直到 10 年前
        1
  •  0
  •   Lucas Arrefelt    10 年前

    在SUB_LIST_FRAGMENT中,尝试将代码从onActivityCreated移动到onCreateView。结果应该是这样的:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflatedView = inflater.inflate(R.layout.sub_list_fragment, container, false);
        ((TextView) inflatedView.findViewById(R.id.cat)).setText(category);
    }
    

    onActivityCreated可能只被调用一次,而每次在视图页中创建新片段时都会调用onCreateView。