Android的ViewPager setAdapter无法更新的观点和片段 [英] Android ViewPager setAdapter can't update views and fragments
问题描述
我有一个FragmentActivity拿着输入按钮和一个 viewpager 。结果
当输入按钮点击后,将图像的URL设置为一个新的 FragmentPagerAdapter 并设置适配器viewpager。结果
并且FragmentPagerAdpater持有片段,显示的图像。结果,
然后preSS返回按钮,然后再次单击该条目按钮。结果
它会在新图片URL设置为新 FragmentPagerAdapter和适配器设置为viewpager。
我的问题的片段继续显示第一个图像,而是点击进入按钮时不显示其他的图像。这似乎有些东西不能更新。结果
会是什么问题?
TestGallery.java
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentPagerAdapter;
进口android.support.v4.view.ViewPager;
进口android.view.View;
进口android.widget.Button;
进口android.widget.Toast;进口com.season.test.TestGalleryFragment;进口的java.util.ArrayList;
公共类TestGallery扩展FragmentActivity {
私人的ArrayList<整数GT; imgURLs;
私人GalleryPagerAdapter mAdapter;
私人按钮btnEntry;
私人ViewPager viewPager;
私人INT CURR = 0;
私人诠释[] = resourceIDs {R.drawable.abc_btn_check_material,R.drawable.abc_ic_go_search_api_mtrl_alpha,R.drawable.ic_launcher}; @覆盖
公共无效onBack pressed(){
如果(viewPager.getVisibility()== View.VISIBLE){
btnEntry.setVisibility(View.VISIBLE);
viewPager.setVisibility(View.GONE);
}其他{
super.onBack pressed();
}
} @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_test_gallery);
btnEntry =(按钮)findViewById(R.id.btnEntry);
viewPager =(ViewPager)findViewById(R.id.viewPager);
btnEntry.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
如果(CURR> = resourceIDs.length){
CURR = 0;
}
如果(imgURLs == NULL){
imgURLs =新的ArrayList<>();
}
imgURLs.clear();
imgURLs.add(resourceIDs [CURR]); 如果(mAdapter == NULL){
mAdapter =新GalleryPagerAdapter(getSupportFragmentManager(),imgURLs);
viewPager.setAdapter(mAdapter);
}
mAdapter.notifyDataSetChanged(); btnEntry.setVisibility(View.GONE);
viewPager.setVisibility(View.VISIBLE); CURR ++;
btnEntry.setText(点击对于下一张图片:+ CURR%resourceIDs.length);
Toast.makeText(TestGallery.this,preSS后退按钮,Toast.LENGTH_SHORT).show();
}
}); } 类GalleryPagerAdapter扩展FragmentPagerAdapter {
ArrayList的<整数GT; imgURLs;
GalleryPagerAdapter(FragmentManager FM,ArrayList的<整数GT; imgURLs){
超(FM);
this.imgURLs = imgURLs;
} @覆盖
公共片段的getItem(INT位置){
返回TestGalleryFragment.newInstance(imgURLs.get(位置));
} @覆盖
公众诠释的getCount(){
返回imgURLs.size();
}
}}
activity_test_gallery.xml
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent> <的FrameLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ID =@ + ID / layoutDetails> <按钮
机器人:ID =@ + ID / btnEntry
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:文字=点击旁边显示
机器人:TEXTSIZE =30sp
/> < android.support.v4.view.ViewPager
机器人:背景=#0000
机器人:ID =@ + ID / viewPager
机器人:知名度=水涨船高
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
< /&的FrameLayout GT;
< /&的FrameLayout GT;
TestGalleryFragment.java
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ImageView;进口com.season.test.R;公共类TestGalleryFragment扩展片段{ 公共静态字符串KEY_IMG_URL =imgUrl的; 私人整数imgUrl的= R.drawable.ic_launcher; 公共静态的newInstance TestGalleryFragment(整数imgUrl的){
Log.i(URL,FRAG网址:+ imgUrl的);
TestGalleryFragment片段=新TestGalleryFragment();
捆绑ARGS =新包();
args.putInt(KEY_IMG_URL,imgUrl的);
fragment.setArguments(参数);
返回片段;
} 公共TestGalleryFragment(){
//必需的空公共构造
} @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 如果(getArguments()!= NULL){
imgUrl的= getArguments()调用getInt(KEY_IMG_URL)。
}
} @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
//充气的布局该片段 查看根= inflater.inflate(R.layout.fragment_gallery,集装箱,FALSE);
ImageView的imgPhoto =(ImageView的)root.findViewById(R.id.imgPhoto);
imgPhoto.setImageResource(imgUrl的);
返回根;
}
}
fragment_test_gallery.xml
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android 机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#0000> < ImageView的
机器人:ID =@ + ID / imgPhoto
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_gravity =中心/>
< /&的FrameLayout GT;
我找到了原因这个问题发生。结果
这是因为片段不重新搜索新鲜/重新创建notifyDataSetChanged称为后。
为了使新的片段,每次数据的变化。更好地修改FragmentPagerAdapter从结果<建议答案href=\"http://stackoverflow.com/a/26944013/2080233\">http://stackoverflow.com/a/26944013/2080233
I have an FragmentActivity holding a entry button and a viewpager.
When the entry button clicked, it will set an image url to a new FragmentPagerAdapter and set the adapter to viewpager.
And the FragmentPagerAdpater is holding fragment that shows an image.
Then press back button, and click that entry button again.
It will set a new image url to a new FragmentPagerAdapter and set the adapter to viewpager.
My problem is the fragment keep showing the first image, but not show other image when entry button is clicked. It seems some stuffs can't update.
What would be the problem?
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>
I found out why this problem happen.
It is because fragment does not re
fresh/re-created after notifyDataSetChanged called.
In order to make new fragments every time data changed. Better modify the FragmentPagerAdapter as suggested answer from
http://stackoverflow.com/a/26944013/2080233
这篇关于Android的ViewPager setAdapter无法更新的观点和片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!