Android视图寻呼机作为列表视图项目在滚动时崩溃

时间:2016-08-09 09:37:27

标签: android listview android-fragments

我已经实施了listviewview pager作为列表项,问题在我scroll listviewapp崩溃时开始。  我在listview中实现了分页,这很好用。 我尝试了同样的事情,并评论了寻呼机的adapter集,没有崩溃。看起来我的寻呼机有一些问题。请帮忙!

logcat:


java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
at java.util.ArrayList.get(ArrayList.java:308)
at android.support.v4.view.ViewPager.getChildDrawingOrder(ViewPager.java:795)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3704)
at android.view.View.draw(View.java:17472)
at android.support.v4.view.ViewPager.draw(ViewPager.java:2405)
at android.view.View.updateDisplayListIfDirty(View.java:16464)
at android.view.View.draw(View.java:17238)
at android.view.ViewGroup.drawChild(ViewGroup.java:3921)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711)
at android.view.View.updateDisplayListIfDirty(View.java:16459)
at android.view.View.draw(View.java:17238)
at android.view.ViewGroup.drawChild(ViewGroup.java:3921)
at android.widget.ListView.drawChild(ListView.java:3538)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711)
at android.widget.AbsListView.dispatchDraw(AbsListView.java:3221)
 at android.widget.ListView.dispatchDraw(ListView.java:3533)
 at android.view.View.draw(View.java:17472)
 at android.widget.AbsListView.draw(AbsListView.java:6240)
 at android.view.View.updateDisplayListIfDirty(View.java:16464)
 at android.view.View.draw(View.java:17238)
 at android.view.ViewGroup.drawChild(ViewGroup.java:3921)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711)
 at android.view.View.updateDisplayListIfDirty(View.java:16459)
 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
 at android.view.View.updateDisplayListIfDirty(View.java:16424)
 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
 at android.view.View.updateDisplayListIfDirty(View.java:16424)
 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
 at android.view.View.updateDisplayListIfDirty(View.java:16424)
 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
 at android.view.View.updateDisplayListIfDirty(View.java:16424)
 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
 at android.view.View.updateDisplayListIfDirty(View.java:16424)
 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
 at android.view.View.updateDisplayListIfDirty(View.java:16424)
 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
 at android.view.View.updateDisplayListIfDirty(View.java:16424)
 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
 at android.view.View.updateDisplayListIfDirty(View.java:16424)
 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
 at android.view.View.updateDisplayListIfDirty(View.java:16424)
 at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:325)
 at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:331)
 at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:366)
 at android.view.ViewRootImpl.draw(ViewRootImpl.java:3134)
 at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2933)
 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2522)
 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
 at android.view.Choreographer.doCallbacks(Choreographer.java:695

ListAdapter:

package com.spicex.adapter;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.android.spicex.R;
import com.spicex.data.spicex_result_list.Datum;
import com.spicex.utility.Logger;
import com.spicex.utility.ZoomOutPageTransformer;

import java.util.ArrayList;

public class SpicexListAdapter extends BaseAdapter {

    private LayoutInflater mInflator;
    private Context mContext;
    private FragmentManager fragmentManager;
    private ArrayList<Datum> mResultScreenData;

    public SpicexListAdapter(Context mContext, FragmentManager fragmentManager, ArrayList<Datum> resultScreenData) {
        this.fragmentManager = fragmentManager;
        mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mContext = mContext;
        this.mResultScreenData = resultScreenData;
    }

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

    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SpiceXHolder holder = null;
        if (convertView == null) {
            holder = new SpiceXHolder();
            convertView = mInflator.inflate(R.layout.list_item_spicex_result_screen, parent, false);
            holder.imagePagerVP = (ViewPager) convertView.findViewById(R.id.imagePagerVP);
            holder.imagePagerVP.setPageTransformer(true, new ZoomOutPageTransformer());
            holder.userInfoLL = (LinearLayout) convertView.findViewById(R.id.userInfoLL);
            holder.usernameTV = (TextView) convertView.findViewById(R.id.usernameTV);
            holder.distanceTV = (TextView) convertView.findViewById(R.id.distanceTV);
            holder.availablityTV = (TextView) convertView.findViewById(R.id.availablityTV);
            convertView.setTag(holder);
        } else {
            holder = (SpiceXHolder) convertView.getTag();
        }
        if (mResultScreenData.size() > position) {
            holder.distanceTV.setText(mResultScreenData.get(position).getDistance());
            Logger.e(SpicexListAdapter.class.getName(), "POS" + position);
            Logger.e(SpicexListAdapter.class.getName(), "SIZE" + mResultScreenData.get(position).getMedia().size());

            if (mResultScreenData.get(position).getMedia() != null && mResultScreenData.get(position).getMedia().size() > 0) {
                holder.imagePagerVP.setAdapter(new SpiceXListPagerAdapter(fragmentManager, mResultScreenData.get(position).getMedia(), mContext));
            } else {
                holder.imagePagerVP.setAdapter(null);
            }
            holder.usernameTV.setText(mResultScreenData.get(position).getUserName());
        }
        return convertView;
    }

    class SpiceXHolder {
        ViewPager imagePagerVP;
        LinearLayout userInfoLL;
        TextView usernameTV;
        TextView distanceTV;
        TextView availablityTV;
    }
}

SpicexPagerAdapter:

package com.spicex.adapter;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.spicex.constants.Constants;
import com.spicex.fragment.spicex.listing.PagerFragment;
import com.spicex.utility.Logger;

import java.util.List;

/**
 * Created by AKSHAY_SOOD on 07/08/16.
 */
public class SpiceXListPagerAdapter extends FragmentPagerAdapter {

    private List<Object> path;
    private Context context;

    public SpiceXListPagerAdapter(FragmentManager fm, List<Object> path, Context context) {
        super(fm);
        this.path = path;
        this.context = context;
    }


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


    @Override
    public Fragment getItem(int position) {
        Fragment fragment = new PagerFragment();
        Bundle bundle = new Bundle();
        String actualPath = "";
        if (path.size() > position)
            if (path.get(position).toString().endsWith(".mp4") || path.get(position).toString().endsWith(".avi")) {
                actualPath = Constants.URL_VIDEO + path.get(position).toString();
            } else {
                actualPath = Constants.URL_IMAGE + path.get(position).toString();
            }
        bundle.putString("path", actualPath);
        fragment.setArguments(bundle);
        return fragment;
    }
}

PagerFragment:

package com.spicex.fragment.spicex.listing;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.example.android.spicex.R;
import com.spicex.utility.RoundedCornersTransformation;

/**
 * Created by AKSHAY_SOOD on 07/08/16.
 */
public class PagerFragment extends Fragment implements View.OnClickListener {

    private View mView;
    private ImageView mSpicexProfilePicIV;
    private String mPath;
    private VideoView mVideoViewVV;
    private ProgressBar progressLoader;
    private Handler mHandler;
    private ImageView mMuteIV;
    private boolean mIsMuted=false;
    private MediaPlayer mMp;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.view_pager_items, null);
        Bundle bundle = getArguments();
        if (bundle != null)
            mPath = bundle.getString("path");
        initUI(mView);
        return mView;
    }

    /**
     * init ui component
     *
     * @param mView
     */
    private void initUI(View mView) {
        mMuteIV = (ImageView) mView.findViewById(R.id.muteIV);
        progressLoader = (ProgressBar) mView.findViewById(R.id.progressLoader);
        mVideoViewVV = (VideoView) mView.findViewById(R.id.videoViewVV);
        mSpicexProfilePicIV = (ImageView) mView.findViewById(R.id.spicexProfilePicIV);
        mSpicexProfilePicIV.setDrawingCacheEnabled(true);
        mMuteIV.setOnClickListener(this);
        if (mPath.endsWith(".mp4") || mPath.endsWith(".avi") || mPath.endsWith(".mkv")) {
            mSpicexProfilePicIV.setVisibility(View.INVISIBLE);
            mMuteIV.setVisibility(View.VISIBLE);
            mVideoViewVV.setVisibility(View.VISIBLE);
            mVideoViewVV.setVideoPath(mPath);
            mVideoViewVV.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mMp = mp;
                    mp.start();
                }
            });

            mHandler = new Handler();

            mHandler.postDelayed(runnable, 0);
        } else if (mPath.endsWith(".jpg") || mPath.endsWith(".png")
                || mPath.endsWith(".jpeg")) {
            if (mVideoViewVV.isPlaying()) {
                mVideoViewVV.stopPlayback();
                mVideoViewVV.setVisibility(View.INVISIBLE);
            }
            mMuteIV.setVisibility(View.INVISIBLE);
            mSpicexProfilePicIV.setVisibility(View.VISIBLE);
            if (mPath != null) {
                Glide.with(getActivity()).load(mPath).asBitmap()
                        .transform(new RoundedCornersTransformation(getActivity(), 10, 0))
                        .placeholder(R.mipmap.ic_spicex_placeholder).into(mSpicexProfilePicIV);
            }
        }
    }

    @Override
    public void onDestroyView() {
        if (mHandler != null)
            mHandler.removeCallbacks(runnable);
        super.onDestroyView();
    }

    private int mOld_duration;
    /**
     * show progress bar when video is loading
     */
    Runnable runnable = new Runnable() {
        public void run() {
            int duration = mVideoViewVV.getCurrentPosition();
            if (mOld_duration == duration && mVideoViewVV.isPlaying()) {
                progressLoader.setVisibility(View.VISIBLE);
            } else {
                progressLoader.setVisibility(View.INVISIBLE);
            }
            mOld_duration = duration;
            mHandler.postDelayed(runnable, 1000);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.muteIV:
                if(mIsMuted){
                    mIsMuted = false;
                    if(mMp!= null){
                        mMp.setVolume(100,100);
                    }
                }else{
                    mIsMuted = true;
                    if(mMp!= null){
                        mMp.setVolume(0,0);
                    }
                }
                break;
        }
    }
}

0 个答案:

没有答案