在baseadapter的getview中执行片段事务的问题?

时间:2013-09-12 11:44:25

标签: android android-fragments adapter flipview

在内部具有3个不同的framelayout的Linearlayout。在该框架布局中,我将在adaper getview中动态添加片段。最初,适配器getview被调用两次。然后它为每次翻转调用一次。添加了翻转视图功能。在任何翻转之前的第一个屏幕中,动态片段不会被添加到主视图中。但对于后来翻转的屏幕,它正在被正确添加。

请帮我解决这个问题。

提前致谢。

public class MainActivity extends Activity {

private FragmentTransaction fragMentTra = null;
protected FlipViewController flipView;
private LayoutInflater inflater;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    flipView = new FlipViewController(this);

    flipView.setAdapter(new BaseAdapter() {
        public int getCount() {
            return 2;
        }

        public Object getItem(int position) {
            return position;
        }

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

        @SuppressLint("NewApi")
        public View getView(int position, View convertView, ViewGroup parent) {

            View layout = convertView;


            if (convertView == null) {
                inflater = LayoutInflater.from(parent.getContext());
                layout = inflater.inflate(R.layout.activity_main, null);
                AphidLog.d("created new view from adapter: %d", position);
            }
            NewsFragment[] newsFragment_obj = new NewsFragment[GlobalValues.titile.length];

            fragMentTra = getFragmentManager().beginTransaction();

            for (int i = 0; i < GlobalValues.titile.length; i++) {
                newsFragment_obj[i] = new NewsFragment(
                        GlobalValues.titile[i], GlobalValues.content[i]);
                AphidLog.d("Array Loc: %d", i);
            }

            fragMentTra.add(R.id.fragment_container1, newsFragment_obj[0],
                    "Fragment1");
            fragMentTra.add(R.id.fragment_container2, newsFragment_obj[1],
                    "Fragment2");
            fragMentTra.add(R.id.fragment_container3, newsFragment_obj[2],
                    "Fragment3");

            fragMentTra.commit();

            return layout;
        }
    });
    setContentView(flipView);
}

0 个答案:

没有答案