Android Fragment刷新无效

时间:2015-11-07 21:15:51

标签: java android android-fragments

我正在尝试在GridView中对我的图片列表进行排序,这是一个单独的片段。 问题是当我删除旧片段并添加新片段时,旧片段占主导地位并且新片段打印出顶部片段。 这是我刷新视图的方式。

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();

请给我一些暗示。

这是Fragment类

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;
}
}

并在MainActivity中选项菜单更改此事务调用时; 这是相关的代码;

 @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);
}

这是我的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"/>

2 个答案:

答案 0 :(得分:1)

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();

答案 1 :(得分:1)

正如@Guillaume Imbert指出的那样,你的问题是fragment标签。使用fragment复制RelativeLayout标记,并在相对布局内部使用id = fragment的FrameLayout。然后在活动中动态使用TransactionManager设置Fragment。这将解决您的问题。