我正在使用带分段的view-pager并通过bundle实例将hash-map传递给fragment。但它不会在view-pager中显示所有图像,默认情况下仅显示第3张图像。 我坚持这个......任何帮助都会很明显。 下面是我的java代码
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
List<Fragment> fragments1;
public MyPagerAdapter adapter1;
static final ArrayList<HashMap<String, Integer>> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.pager1);
fragments1 = getFragments1();
adapter1 = new MyPagerAdapter(getSupportFragmentManager(), fragments1);
viewPager.setAdapter(adapter1);
}
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public int getItemPosition (Object object)
{
if (object instanceof MyImageSlider ) {
return POSITION_NONE;
}
return POSITION_UNCHANGED;
}
}
private List<Fragment> getFragments1() {
List<Fragment> fList = new ArrayList<>();
for(int i = 0; i < 5; i++){
HashMap<String, Integer> map = new HashMap<>();
map.put("0",R.drawable.first);
map.put("1",R.drawable.second);
map.put("2",R.drawable.three);
map.put("3",R.drawable.four);
map.put("4",R.drawable.xiaomi);
list.add(map);
fList.add(MyImageSlider.newInstance(list.get(i)));
}
return fList;
}
public static class MyImageSlider extends Fragment {
HashMap<String, Integer> imageid;
ArrayList<HashMap<String, Integer>> listArray = new ArrayList<>();
public static MyImageSlider newInstance(HashMap<String, Integer> id)
{
MyImageSlider slider=new MyImageSlider();
Bundle b=new Bundle();
b.putSerializable("imageid", id);
slider.setArguments(b);
return slider;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageid=(HashMap<String, Integer>) getArguments().getSerializable("imageid");
listArray.add(imageid);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_main, container, false);
ImageView iv=(ImageView)view.findViewById(R.id.img);
for (String key : imageid.keySet()) {
iv.setImageResource(imageid.get(key);
}
return view;
}
}
}
答案 0 :(得分:0)
为什么要创建片段列表,你可以创建一个片段,它持有imageView并通过适配器传递图像列表并在该片段上显示图像。
请检查以下代码 - 如果您有任何疑虑,请告诉我。
//这是你的MainActivity.java
package com.sample.stack;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager1);
HashMap<String,Integer> sliderDataList = new HashMap<String,Integer>();
sliderDataList.put("0",R.drawable.tc);
sliderDataList.put("1",R.drawable.tc);
sliderDataList.put("2",R.drawable.tc);
sliderDataList.put("3",R.drawable.tc);
sliderDataList.put("4",R.drawable.tc);
PagerAdapter adapter1 = new PagerAdapter(getSupportFragmentManager(), sliderDataList, MainActivity.this);
viewPager.setAdapter(adapter1);
}
}
//这是你的FragmentPagerAdapter
package com.sample.stack;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.HashMap;
/**
* Created by on 06-12-2016.
*/
public class PagerAdapter extends FragmentPagerAdapter {
private final HashMap<String, Integer> hMap;
private final Context context;
public PagerAdapter(FragmentManager fm, HashMap<String,Integer> hMap, Context context) {
super(fm);
this.hMap=hMap;
this.context=context;
}
@Override
public Fragment getItem(int position) {
return new SliderFragment(hMap,context,position);
}
@Override
public int getCount() {
return hMap.size();
}
}
//这是你的Fragent包含ImageView
package com.sample.stack;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by Syscraft on 06-12-2016.
*/
@SuppressLint("ValidFragment")
public class SliderFragment extends Fragment {
private final HashMap<String, Integer> urls;
private int imageResourceId;
private Context ctx;
public SliderFragment(HashMap<String,Integer> urls, Context c,
int pos) {
this.urls = urls;
this.imageResourceId = pos;
this.ctx = c;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.slider_adapter, container, false);
final ImageView imageView = (ImageView) view
.findViewById(R.id.sliderImages);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(urls.get(imageResourceId+""));
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return view;
}
}
答案 1 :(得分:0)
公共类MainActivity扩展了AppCompatActivity {
ViewPager viewPager;
public PagerAdapter adapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager1);
final HashMap<String,Integer> sliderDataList = new HashMap<String,Integer>();
sliderDataList.put("0",R.drawable.first);
sliderDataList.put("1",R.drawable.second);
sliderDataList.put("2",R.drawable.three);
sliderDataList.put("3",R.drawable.four);
sliderDataList.put("4",R.drawable.xiaomi);
adapter1 = new PagerAdapter(getSupportFragmentManager(), sliderDataList, MainActivity.this);
viewPager.setAdapter(adapter1);
// sliderDataList.remove("3");
adapter1.notifyDataSetChanged();
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
remove("3");
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
public void remove(String s){
adapter1.remove(s);
}
public class PagerAdapter extends FragmentPagerAdapter {
private final HashMap<String, Integer> hMap;
private final Context context;
public PagerAdapter(FragmentManager fm, HashMap<String,Integer> hMap, Context context) {
super(fm);
this.hMap=hMap;
this.context=context;
}
@Override
public Fragment getItem(int position) {
return new SliderFragment(hMap,context,position);
}
@Override
public int getCount() {
return hMap.size();
}
public void remove(String s){
Iterator<Map.Entry<String,Integer>> iter = hMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String,Integer> entry = iter.next();
if(s.equalsIgnoreCase(entry.getKey())){
iter.remove();
}
}
hMap.remove(s);
adapter1.notifyDataSetChanged();
}
}
@SuppressLint("ValidFragment")
public static class SliderFragment extends Fragment {
private final HashMap<String, Integer> urls;
private int imageResourceId;
private Context ctx;
public SliderFragment(HashMap<String,Integer> urls, Context c, int pos) {
this.urls = urls;
this.imageResourceId = pos;
this.ctx = c;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_main, container, false);
final ImageView imageView = (ImageView) view
.findViewById(R.id.img);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(urls.get(imageResourceId +""));
return view;
}
}
}