我有一个FragmentActivity,其中包含一个输入按钮和一个 viewpager 单击输入按钮后,它会将图像URL设置为新的 FragmentPagerAdapter ,并将适配器设置为viewpager。 FragmentPagerAdpater持有显示图像的片段 然后按后退按钮,再次单击该输入按钮 它会将新图片网址设置为新 FragmentPagerAdapter,并将适配器设置为viewpager。
我的问题是片段一直显示第一张图片,但在点击输入按钮时不显示其他图片。似乎有些东西无法更新。
会出现什么问题?
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>
答案 0 :(得分:1)
我发现了这个问题发生的原因。
这是因为片段在notifyDataSetChanged调用后没有重新创建/重新创建。
每次数据更改时都要创建新的片段。最好根据来自https://stackoverflow.com/a/26944013/2080233