setAdapter在片段中查看寻呼机

时间:2017-05-15 15:56:11

标签: java android android-fragments android-viewpager

我希望在“extends Fragment {”中使用ViewPager的图片幻灯片,但在活动“Beranda” <中是错误和红线 / p> 你能帮帮我吗?拜托:')

Beranda [本活动]

package com.dolog.om.radarfutsalbooking;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import me.relex.circleindicator.CircleIndicator;

public class Beranda extends Fragment {
    private static ViewPager mPager;
    private static int currentPage = 0;
    private static final Integer[] XMEN = {R.drawable.beast, R.drawable.charles, R.drawable.magneto, R.drawable.mystique, R.drawable.wolverine};
    private ArrayList<Integer> XMENArray = new ArrayList<Integer>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_beranda, container, false);
    for (int i = 0; i < XMEN.length; i++)
        XMENArray.add(XMEN[i]);

    mPager = (ViewPager) rootView.findViewById(R.id.pager);
    mPager.setAdapter(new MyAdapter(Beranda.this, XMENArray));
    CircleIndicator indicator = (CircleIndicator) rootView.findViewById(R.id.indicator);
    indicator.setViewPager(mPager);

    // Auto start of viewpager
    final Handler handler = new Handler();
    final Runnable Update = new Runnable() {
        public void run() {
            if (currentPage == XMEN.length) {
                currentPage = 0;
            }
            mPager.setCurrentItem(currentPage++, true);
        }
    };
    Timer swipeTimer = new Timer();
    swipeTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(Update);
        }
    }, 5000, 5000);

    FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fabberanda);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    return rootView;
  }

}

MyAdapter [此活动]

package com.dolog.om.radarfutsalbooking;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

public class MyAdapter extends PagerAdapter {

private ArrayList<Integer> images;
private LayoutInflater inflater;
private Context context;

public MyAdapter(Context context, ArrayList<Integer> images) {
    this.context = context;
    this.images=images;
    inflater = LayoutInflater.from(context);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
}

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

@Override
public Object instantiateItem(ViewGroup view, int position) {
    View myImageLayout = inflater.inflate(R.layout.slide, view, false);
    ImageView myImage = (ImageView) myImageLayout.findViewById(R.id.image);
    myImage.setImageResource(images.get(position));
    view.addView(myImageLayout, 0);
    return myImageLayout;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view.equals(object);
}

这个问题enter image description here

2 个答案:

答案 0 :(得分:1)

转换

mPager.setAdapter(new MyAdapter(Beranda.this, XMENArray));

mPager.setAdapter(new MyAdapter(getActivity(), XMENArray));

请注意,问题是您的类扩展了Fragment并且您无法将片段类实例传递给上下文。因此,您必须传递使用此片段的Activity的上下文。

答案 1 :(得分:0)

MyAdapter adapter =new MyAdapter(CurrentActivityname,ArrayListName);
viewPager.setAdapter(adapter);

在这里您可以添加多个arrayList这样

MyAdapter adapter =new MyAdapter(CurrentActivityname,ArrayListName1,ArrayListName1,
          ArrayListName2,ArrayListName2,ArrayListName4);
viewPager.setAdapter(adapter);