其他分享
首页 > 其他分享> > Android从其父活动刷新片段列表

Android从其父活动刷新片段列表

作者:互联网

我有一个主活动,其中包含带有3个菜单按钮的操作栏.

然后我在这个主要活动中有一个片段,里面有一个列表.

我希望能够刷新主活动片段中的列表,当单击其中一个菜单按钮时,或者最好只删除列表中的所有行.

任何帮助表示赞赏.

谢谢.

public class Favourite extends SherlockFragmentActivity {

    ActionBar actionBar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favourite);

        actionBar = getSupportActionBar();

        actionBar.setDisplayShowTitleEnabled(false);

        BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.actionbar_bg);
        bg.setTileModeX(TileMode.REPEAT); 
        getSupportActionBar().setBackgroundDrawable(bg);

        getSupportActionBar().setIcon(R.drawable.favourite_title);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.Tab tabAll = actionBar.newTab();
        ActionBar.Tab tabfavs = actionBar.newTab();
        ActionBar.Tab tabhist = actionBar.newTab();
        tabAll.setText("all");
        tabfavs.setText("favs");
        tabhist.setText("hist");
        tabAll.setTabListener(new MyTabListener());
        tabfavs.setTabListener(new MyTabListener());
        tabhist.setTabListener(new MyTabListener());
        actionBar.addTab(tabAll);
        actionBar.addTab(tabfavs);
        actionBar.addTab(tabhist);


        try{

        }
        catch(Exception e)
        {
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.actionbar_itemlist_favourite, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
                case R.id.history:
                    break;
                case R.id.favourite:
                    Intent favAct = new Intent(this, Favourite.class);
                    startActivity(favAct);
                    break;
                case R.id.delete:
                    ///I WANT TO BE ABLE TO REFRESH FRAGMENTLIST FROM HERE
                }
                return true;
    }

  }

class MyTabListener implements ActionBar.TabListener {

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if(tab.getPosition()==0)
        {
            FavouriteAllWords frag = new FavouriteAllWords();
            ft.replace(android.R.id.content, frag);

        }
        else if(tab.getPosition()==1)
        {
            FavouriteFavWords frag = new FavouriteFavWords();
            ft.replace(android.R.id.content, frag);

        }
        else if(tab.getPosition()==2)
        {
            FavouriteHistWords frag = new FavouriteHistWords();
            ft.replace(android.R.id.content, frag);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

}

////////////////////我的列表碎片类

public class FavouriteAllWords extends ListFragment {

    ArrayAdapter<String> adapter;

    List<String> stringOfFavWords;

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
    {   
        adapter = new ArrayAdapter<String>(  
                inflater.getContext(), R.layout.row, stringOfFavWords);  
                setListAdapter(adapter);  

        return super.onCreateView(inflater, group, saved);  
    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState)
    {   
        super.onActivityCreated(savedInstanceState);
    }

}

解决方法:

您可以使用INTERFACE轻松实现此目的

MainActivity.java

public class MainActivity extends Activity {

    public FragmentRefreshListener getFragmentRefreshListener() {
        return fragmentRefreshListener;
    }

    public void setFragmentRefreshListener(FragmentRefreshListener fragmentRefreshListener) {
        this.fragmentRefreshListener = fragmentRefreshListener;
    }

    private FragmentRefreshListener fragmentRefreshListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button b = (Button)findViewById(R.id.btnRefreshFragment);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(getFragmentRefreshListener()!=null){
                    getFragmentRefreshListener().onRefresh();
                }
            }
        });


    }


    public interface FragmentRefreshListener{
        void onRefresh();
    }

}

MyFragment.java

public class MyFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = null; // some view

        /// Your Code


        ((MainActivity)getActivity()).setFragmentRefreshListener(new MainActivity.FragmentRefreshListener() {
            @Override
            public void onRefresh() {

                // Refresh Your Fragment
            }
        });


        return v;
    }
}

标签:android-listfragment,android,android-fragments
来源: https://codeday.me/bug/20191004/1853438.html