在ViewPager中传递整数

时间:2015-06-03 18:24:36

标签: android android-layout

我在viewpager中有3个片段(LeftFragment,MidFragment和Right Fragment)LeftFragment包含一个listview,当点击listview项时,它会从leftfragment向mid片段发送一个索引(整数),一个图像将被加载到MidFragment上。这里的错误可以是onCreateView在我设置参数之前发生。那么有办法解决我的情况。对不起我的技能英语不好。谢谢!

LeftFragment

public class LeftFragment extends Fragment{
    private  ListView listView;
    private List<ParseObject> ob;
    private ProgressDialog mProgressDialog;
    private ListViewAdapter adapter;
    private List<School> schoollist = null;
    private View v;
    MainActivity mainActivity = new MainActivity();
    MidFragment midFragment = new MidFragment();
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.left_fragment, container, false);
        initComponent();
        new RemoteDataTask().execute();
        return v;
    }

    private void initComponent() {
        listView = (ListView)v.findViewById(R.id.lv_left);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mainActivity.viewPager.setCurrentItem(1);
                School school = schoollist.get(position);
                midFragment.reloadData(Integer.parseInt(school.getIndex()));
            }
        });
    }

    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setTitle("Please wait a moment...");
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            schoollist = new ArrayList<School>();
            try{
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("School");
                query.orderByAscending("index");
                ob = query.find();
                for (ParseObject name : ob){
                    School map = new School();
                    map.setIndex(String.valueOf(name.get("index")));
                    map.setCount(String.valueOf(name.get("count")));
                    map.setName(String.valueOf(name.get("name")));
                    schoollist.add(map);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            adapter = new ListViewAdapter(getActivity(), schoollist);
            listView.setAdapter(adapter);
            mProgressDialog.dismiss();
            super.onPreExecute();
        }
    }
}

MidFragment

public class MidFragment extends Fragment {
    //    private ProgressDialog mProgressDialog;
    private ImageView mImageView;
    private VideoView mVideoView;
    private DisplayImageOptions options;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private Context mContext;
    private ImageLoadingListener animateFirstListener;
    private ArrayList<ParseObject> objectList = new ArrayList<>();
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
//          imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
//          mImageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .build();
        animateFirstListener = new AnimateFirstDisplayListener();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mid_fragment, container, false);


        initComponent(view);
        mContext = container.getContext();
        return view;

    }

    private void initComponent(View view) {
        mImageView = (ImageView) view.findViewById(R.id.image);
        mVideoView = (VideoView) view.findViewById(R.id.video);
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mVideoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
//        mProgressDialog = new ProgressDialog(getActivity());
//        mProgressDialog.setTitle("Please wait a moment...");
//        mProgressDialog.setMessage("Loading...");
//        mProgressDialog.setIndeterminate(false);
    }

    public void reloadData(int index){
//        mProgressDialog.show();
        objectList.clear();
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Data");
        query.whereEqualTo("school",(Number)index);
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> list, ParseException e) {
                objectList = (ArrayList<ParseObject>) list;
//                mProgressDialog.dismiss();
                if (!list.isEmpty())
                    openFirst();
            }
        });
    }


    private void openFirst(){
        ParseObject parseObject = objectList.get(0);
        ParseFile parseFile = parseObject.getParseFile("file");
        if (parseObject.getNumber("type") == (Number)1){
        }else{
//          mVideoView.setVisibility(View.GONE);
//          mImageView.setVisibility(View.VISIBLE);
            imageLoader.displayImage(parseFile.getUrl(), mImageView, options, animateFirstListener);
        }
    }
    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }
}

MainActivity

public class MainActivity extends FragmentActivity{
    public static MyPagerFragment myPagerFragment;
    public static ViewPager viewPager;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager)findViewById(R.id.viewPager);
        myPagerFragment = new MyPagerFragment(getSupportFragmentManager());
        viewPager.setAdapter(myPagerFragment);
        viewPager.setCurrentItem(1);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public static class MyPagerFragment extends FragmentStatePagerAdapter {

        public MyPagerFragment(FragmentManager fm){
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0: return new LeftFragment();
                case 1: return new MidFragment();
                case 2: return new RightFragment();
                default: return null;
            }
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

LeftFragment

var wordArray = sim.split(/\s+/);

主要活动

public class LeftFragment extends Fragment{
OnImageSelectedListener mCallback;

public interface OnImageSelectedListener {
    public void onImageSelected(int position);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        mCallback = (OnImageSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnImageSelectedListener");
    }
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Send the event to the host activity
    mCallback.onImageSelected(position);
}

}

MidFragment

public class MainActivity extends FragmentActivity implements HeadlinesFragment.OnImageSelectedListener{

public void onArticleSelected(int position) {
midFragment.setImage(position);
}
}