gpt4 book ai didi

Android ViewPager setAdapter 无法更新 View 和 fragment

转载 作者:行者123 更新时间:2023-11-30 02:20:57 25 4
gpt4 key购买 nike

我有一个包含输入按钮的 FragmentActivity 和一个 viewpager
当点击入口按钮时,它会设置一个图像 url 到一个新的 FragmentPagerAdapter 并将适配器设置为 viewpager。
FragmentPagerAdpater 持有显示图像的 fragment 。
然后按后退按钮,并再次单击该输入按钮。
它会将一个新的图像 url 设置为一个新的 FragmentPagerAdapter,并将适配器设置为 viewpager。

我的问题 fragment 一直显示第一张图片,但在单击输入按钮时不显示其他图片。好像有些东西更新不了。

会出现什么问题?

TestGallery.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.season.test.TestGalleryFragment;

import java.util.ArrayList;


public class TestGallery extends FragmentActivity {
private ArrayList<Integer> imgURLs;
private GalleryPagerAdapter mAdapter;
private Button btnEntry;
private ViewPager viewPager;
private int curr = 0;
private int[] resourceIDs = {R.drawable.abc_btn_check_material, R.drawable.abc_ic_go_search_api_mtrl_alpha, R.drawable.ic_launcher};

@Override
public void onBackPressed() {
if (viewPager.getVisibility() == View.VISIBLE) {
btnEntry.setVisibility(View.VISIBLE);
viewPager.setVisibility(View.GONE);
} else {
super.onBackPressed();
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_gallery);
btnEntry = (Button) findViewById(R.id.btnEntry);
viewPager = (ViewPager) findViewById(R.id.viewPager);
btnEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (curr >= resourceIDs.length) {
curr = 0;
}
if (imgURLs == null) {
imgURLs = new ArrayList<>();
}
imgURLs.clear();
imgURLs.add(resourceIDs[curr]);

if (mAdapter == null) {
mAdapter = new GalleryPagerAdapter(getSupportFragmentManager(), imgURLs);
viewPager.setAdapter(mAdapter);
}
mAdapter.notifyDataSetChanged();

btnEntry.setVisibility(View.GONE);
viewPager.setVisibility(View.VISIBLE);

curr++;
btnEntry.setText("Click For Next Image: " + curr % resourceIDs.length);
Toast.makeText(TestGallery.this,"Press Back Button",Toast.LENGTH_SHORT).show();
}
});

}

class GalleryPagerAdapter extends FragmentPagerAdapter {
ArrayList<Integer> imgURLs;


GalleryPagerAdapter(FragmentManager fm, ArrayList<Integer> imgURLs) {
super(fm);
this.imgURLs = imgURLs;
}

@Override
public Fragment getItem(int position) {
return TestGalleryFragment.newInstance(imgURLs.get(position));
}

@Override
public int getCount() {
return imgURLs.size();
}


}

}

activity_test_gallery.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layoutDetails">

<Button
android:id="@+id/btnEntry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Click to show next"
android:textSize="30sp"
/>

<android.support.v4.view.ViewPager
android:background="#0000"
android:id="@+id/viewPager"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>


</FrameLayout>

TestGalleryFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.season.test.R;

public class TestGalleryFragment extends Fragment {

public static String KEY_IMG_URL = "imgURL";

private Integer imgURL = R.drawable.ic_launcher;

public static TestGalleryFragment newInstance(Integer imgURL) {
Log.i("url", "frag url: " + imgURL);
TestGalleryFragment fragment = new TestGalleryFragment();
Bundle args = new Bundle();
args.putInt(KEY_IMG_URL, imgURL);
fragment.setArguments(args);
return fragment;
}

public TestGalleryFragment() {
// Required empty public constructor
}

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

if (getArguments() != null) {
imgURL = getArguments().getInt(KEY_IMG_URL);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment

View root = inflater.inflate(R.layout.fragment_gallery, container, false);
ImageView imgPhoto = (ImageView) root.findViewById(R.id.imgPhoto);
imgPhoto.setImageResource(imgURL);
return root;
}


}

fragment_test_gallery.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000">

<ImageView
android:id="@+id/imgPhoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />


</FrameLayout>

最佳答案

我发现了为什么会出现这个问题。
这是因为在调用 notifyDataSetChanged 后 fragment 不会重新
刷新/重新创建。为了在每次数据更改时制作新的 fragment 。最好将 FragmentPagerAdapter 修改为来自
https://stackoverflow.com/a/26944013/2080233 的建议答案

关于Android ViewPager setAdapter 无法更新 View 和 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28558024/

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