gpt4 book ai didi

java - Android Fragment刷新不起作用

转载 作者:行者123 更新时间:2023-12-02 04:13:13 24 4
gpt4 key购买 nike

我正在尝试对位于单独 fragment 上的 GridView 中的图片列表进行排序。问题是,当我删除旧 fragment 并添加新 fragment 时,旧 fragment 仍然占主导地位,而新 fragment 则打印在其顶部。这就是我刷新 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();

请给我一些关于此的提示。

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

最佳答案

正如 @Guillaume Imbert 指出的那样,你的问题是 fragment 标签。将 fragment 标记替换为 RelativeLayout,并且在相对布局内有一个 id=fragment 的 FrameLayout。然后在 Activity 中动态地使用 TransactionManager 来设置 Fragment。这将解决您的问题。

关于java - Android Fragment刷新不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33587863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com