我想排序我的圖片列表在一個單獨的片段上的GridView。 問題是,當我刪除舊的片段,並添加新的片段舊的stil盛行,新的打印它的頂部。 這是我刷新視圖的方式。Android的片段刷新不起作用
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();
請給我一些關於這方面的提示。
這是片段類
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"/>
@rolde哇你的解決方案完美無缺。不是爲了你,謝謝 – shan
實際上,應該移除FrameLout周圍的RelativeLayout來抑制佈局過程,你可以直接使用FrameLayou Ť –