简体   繁体   中英

Android Fragment refresh doesn't work

I'm trying to sort my picture list in a GridView which is on a seperate fragment. The issue is when I remove old fragment and add new fragment the old one stil prevails and the new one prints top of it. this is how I refresh the View.

Bundle bundle = new Bundle();
    bundle.putString("sort", sort);

    android.support.v4.app.Fragment frg = new MainActivityFragment();
    android.support.v4.app.Fragment currentFrg = getSupportFragmentManager().findFragmentById(R.id.fragment);
    final android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.remove(currentFrg);
    frg.setArguments(bundle);
    ft.add(R.id.fragment, frg).commit();

Please give me some hint regarding this.

this is the Fragment class

public class MainActivityFragment extends Fragment {

private MovieAdaptor movieAdaptor;

GridView gridView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.fragment_main, container, false);

   ImageView iconView = (ImageView) this.getActivity().findViewById(R.id.list_item_icon);

   FetchMovieTask task = new FetchMovieTask();
    String sort = "";
    Bundle bundle = this.getArguments();
    if (bundle != null) {
        sort = bundle.getString("sort", "popularity.desc");
    }

    try {
        movieAdaptor    = new MovieAdaptor(this.getActivity(), (List<Movie>) task.execute(sort).get() );
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }


    gridView = (GridView) rootView.findViewById(R.id.gridview);

    if(movieAdaptor != null){

        Log.e("MOVIE_DATA", "movieAdaptor IS NOT NULL ");
        gridView.setAdapter(movieAdaptor);

    }else {
        Log.e("MOVIE_DATA", "movieAdaptor IS NULL");
    }

    return rootView;
}
}

and in MainActivity when the option menu changes this transaction calls; this is the regarding code;

 @Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    String sort = "";

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_sort_rating) {
        sort = "vote_average.desc";

    }

    if(id == R.id.action_sort_popularity){
        sort = "popularity.desc";

    }

    if(id == R.id.action_sort_release_date){
        sort = "release_date.desc";

    }

    Bundle bundle = new Bundle();
    bundle.putString("sort", sort);
    android.support.v4.app.Fragment frg = new MainActivityFragment();
    final android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    frg.setArguments(bundle);
    ft.replace(R.id.fragment, frg).commit();



    return  true;

    //return super.onOptionsItemSelected(item);
}

this is my fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$MainActivityFragment">


<GridView
    android:id="@+id/gridview"
    android:numColumns="2"
    android:gravity="top"
    android:columnWidth="50dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
   >

</GridView>
</RelativeLayout>

activity_main.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/fragment"
      android:name="com.alpha0.popular_movies_app.MainActivityFragment"
      tools:layout="@layout/fragment_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>
Bundle bundle = new Bundle();
bundle.putString("sort", sort);
android.support.v4.app.Fragment frg = new MainActivityFragment();
    final android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
frg.setArguments(bundle);
ft.replace(R.id.fragment, frg).commit();

Well as @Guillaume Imbert pointed out, your problem is fragment tag. Replce the fragment tag with a RelativeLayout , and inside the relative layout have a FrameLayout with id=fragment. Then dynamically in the activity use the TransactionManager to set the Fragment . This will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM