如何通过单击另一个片段中的按钮将一行添加到一个片段中的ListView?

时间:2014-11-24 14:34:56

标签: android listview android-fragments android-listview

我有两个碎片:FragmentA和FragmentB。 FragmentA有一个按钮,FragmentB有一个ListView,其适配器设计为使用自定义行。我想通过单击FragmentA中的按钮在FragmentB中的这个ListView中添加一行。我已经研究了谷歌关于片段之间的沟通的一些文件,我写了一些代码,但我没有成功。我试图将一个按钮和ListView放在一个片段中。单击按钮时,我成功使用了适配器的notifiyDataSetChanged()方法。 Hovewer,我不知道如何解决这个问题。

我的MainActiviy.java

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener, FragmentA.ButtonClickListener{
    private static final int NUM_PAGES = 2;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;
    private ActionBar actionBar;
    private String[] tabs = { "A", "B"};    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);   
        // ViewPager's PageChangeListener.
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }        
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }        
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });        
        // Retrieve actionbar and set some properties.
        actionBar = getSupportActionBar();
        // actionBar.setHomeButtonEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
        Fragment frag1 = new FragmentA();
        Fragment frag2 = new FragmentB();
        FragmentTransaction fragmentTransaction;
        FragmentManager fragMgr = getSupportFragmentManager();
        fragmentTransaction = fragMgr.beginTransaction();
        fragmentTransaction.add(frag1, "frag1Tag");
        fragmentTransaction.add(frag2, "frag2Tag");
        fragmentTransaction.commit();
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }   
    private class ViewPagerAdapter extends FragmentStatePagerAdapter {
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return new FragmentA();
                case 1:
                    return new FragmentB();
            }
            return null;
        }
        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
    @Override
    public void onButtonClicked() {  // IMPLEMENTATION OF INTERFACE METHOD
        FragmentB fragmentB = (FragmentB)getSupportFragmentManager().findFragmentByTag("frag2Tag");
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (fragmentB != null) {
            fragmentB.updateList();
        } else {
            FragmentB newFragment = new  FragmentB();
        }
    }       
}

我的FragmentA.java

public class FragmentA extends Fragment {   
    ButtonClickListener mCallback;
    FragmentTransaction ft;
    List<String> values = new ArrayList<String>();
    // Container Activity must implement this interface.
    // INTERFACE DEFINATION.
    public interface ButtonClickListener {
        public void onButtonClicked();
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);           
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (ButtonClickListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
        }
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup vG = (ViewGroup)inflater.inflate(R.layout.fragment1_layout, container,false);
        Button btn = (Button)vG.findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {         
            @Override
            public void onClick(View v) {               
                mCallback.onButtonClicked();
            }
        });
        return vG;   
    }
}

我的FragmentB.java

public class FragmentB extends Fragment {   
    List<String> values = new ArrayList<String>();
    List<String> valuesRefresh = new ArrayList<String>();
    ListView list;
    MyListAdapter adapter;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        values.add("asda");
        values.add("kasd");
        ViewGroup vG = (ViewGroup)inflater.inflate(R.layout.fragment2_layout, container,false);
        list = (ListView)vG.findViewById(R.id.listView2);   
        adapter = new MyListAdapter(getActivity(),values);
        list.setAdapter(adapter);
        return vG;
    }
    public void updateList(){    
        adapter.notifyDataSetChanged();
    }
    public static class MyListAdapter extends ArrayAdapter<String>{      
        private final Context context;
        private final List<String> values = new ArrayList<String>();
        public MyListAdapter(Context context, List<String> values) {
            super(context, R.layout.list_item, values);
            this.context = context;
            for (int i=0; i<values.size(); i++) {
                this.values.add(values.get(i));
            }
        }
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
        public long getCount(int position) {
            // TODO Auto-generated method stub
            return values.size();
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View list = inflater.inflate(R.layout.list_item,parent,false); 
            return list; 
        }
    }        
}

1 个答案:

答案 0 :(得分:1)

您的问题是,您正在活动的onCreate中创建片段,再次在寻呼机适配器中创建片段,再次在o​​nButtonClicked(FragmentB)中创建片段。你应该只创建一次。由于您在Activity中执行了所有这些操作,因此最简单的方法是在Activity字段中保留对片段的引用。但是,您需要确保处理重新创建活动的情况,因为在这种情况下,Android已经重新创建了您的片段,您不需要再次创建和添加它们。

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener, FragmentA.ButtonClickListener{
    private static final int NUM_PAGES = 2;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;
    private ActionBar actionBar;
    private String[] tabs = { "A", "B"};    
    private Fragment frag1;
    private Fragment frag2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // First check if Android has already created the fragments
        FragmentManager fragMgr = getSupportFragmentManager();
        frag1 = fragMgr.findFragmentByTag("frag1Tag");
        frag2 = fragMgr.findFragmentByTag("frag2Tag");
        if (frag1 == null && frag2 == null) {
            // Create and add fragments only if needed
            frag1 = new FragmentA();
            frag2 = new FragmentB();
            FragmentTransaction fragmentTransaction = fragMgr.beginTransaction();
            fragmentTransaction.add(frag1, "frag1Tag");
            fragmentTransaction.add(frag2, "frag2Tag");
            fragmentTransaction.commit();
        }
        mPager = (ViewPager) findViewById(R.id.pager);
        ...
    }
    ...
    private class ViewPagerAdapter extends FragmentStatePagerAdapter {
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return frag1;
                case 1:
                    return frag2;
            }
            return null;
        }
        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
    @Override
    public void onButtonClicked() {  // IMPLEMENTATION OF INTERFACE METHOD
        frag2.updateList();
    }       
}