列表项在滚动时更改序列并在列表视图中重复

时间:2015-07-23 10:15:43

标签: android listview

这是我的代码。

public class MainActivity extends Activity {
    ListView listview;
    CustomListAdapter adapter;
    private static ProgressDialog progressDialog;
    public static int currentItem;
    public static ArrayList<String> videoLIst;
    public static MediaController mediaController ;
    boolean pauseOnScroll = true;
    boolean pauseOnFling = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);
        videoLIst = new ArrayList<String>();
         mediaController = new MediaController(MainActivity.this); 
        videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/experience/165/video/father-and-young-daughter.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/experience/185/video/couple-on-sail-boat.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/experience/39/video/VID-20141231-WA0006.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/experience/26/video/VID-20141216-WA0004.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/experience/165/video/father-and-young-daughter.mp4");
        videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
        listview = (ListView) findViewById(R.id.list);
        listview.setAdapter(new CustomListAdapter(this, videoLIst));

      listview.setOnScrollListener(new AbsListView.OnScrollListener() {
          private int mFirstVisibleRow = -1;
          private int mActiveItem = -1;

          @Override
          public void onScrollStateChanged(AbsListView view, int scrollState) {
          }

          @Override
          public void onScroll(AbsListView view, int firstVisibleItem,
                               int visibleItemCount, int totalItemCount) {
              int firstVisibleRow = listview.getFirstVisiblePosition();
              if( mFirstVisibleRow != firstVisibleRow )
              {
                  mFirstVisibleRow = firstVisibleRow;

                  // Cancel the video of the previous active item
                  VideoView prevActiveVideoView = getVideoViewForRow(mActiveItem);
                  if( prevActiveVideoView != null )
                  {
                      prevActiveVideoView.pause();
                  }

                  // Start the video of the new active item
                  mActiveItem = mFirstVisibleRow + 1;
                  VideoView newActiveVideoView = getVideoViewForRow(mActiveItem);
                  if(newActiveVideoView != null)
                  {
                      newActiveVideoView.start();
                  }
              }
          }

          private VideoView getVideoViewForRow(int row)
          {
              int firstVisibleRow = listview.getFirstVisiblePosition();
              View rowView = listview.getChildAt(row-firstVisibleRow);
              return (rowView == null) ? null : (VideoView)rowView.findViewById(R.id.VideoView);
          }

      });

    }
    static class ViewHolderItem {

                TextView textViewItem;
                VideoView video;
                ProgressBar progressbar;
            }

    public class CustomListAdapter extends BaseAdapter {
        private Activity activity;
        private LayoutInflater inflater;
        private ArrayList<String> videoList;

        public CustomListAdapter(Activity activity, ArrayList<String> videoList) {
            this.activity = activity;
            this.videoList = videoList;
        }

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

        @Override
        public Object getItem(int location) {
            return videoList.get(location);
        }

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

        @SuppressLint("NewApi")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolderItem viewHolder;

            if (inflater == null)
                inflater = (LayoutInflater) activity
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null)
            {
                convertView = inflater.inflate(R.layout.activity_main, null);

            viewHolder = new ViewHolderItem();
            viewHolder.video = (VideoView)convertView.findViewById(R.id.VideoView);
            viewHolder.progressbar = (ProgressBar)convertView.findViewById(R.id.progress);

            viewHolder.textViewItem =(TextView)convertView.findViewById(R.id.txvposition);

            viewHolder.textViewItem.setText("Video"+position);

            mediaController.setAnchorView(viewHolder.video);
            Log.d("video url inadapter:", ""+videoLIst.toString());
            Uri uri = Uri.parse(videoList.get(position));
            viewHolder.video.setMediaController(mediaController);
            viewHolder.video.setVideoURI(uri);
            //viewHolder.video.start();

            viewHolder.video.setOnPreparedListener(new OnPreparedListener() {

                public void onPrepared(MediaPlayer arg0) {
                    viewHolder.progressbar.setVisibility(View.GONE);
                    viewHolder.video.start();
                }
            });
            viewHolder.video.setOnInfoListener(new OnInfoListener() {

                @Override
                public boolean onInfo(MediaPlayer mp, int what, int extra) {
                    // TODO Auto-generated method stub

                    Log.d("BUFFRRING START", "On_InFO");

                    if (MediaPlayer.MEDIA_INFO_BUFFERING_START == what) {
                        viewHolder.progressbar.setVisibility(View.VISIBLE);
                        Log.d("BUFFRRING START", "" + what);
                    }
                    if (MediaPlayer.MEDIA_INFO_BUFFERING_END == what) {
                        viewHolder.progressbar.setVisibility(View.GONE);
                        Log.d("BUFFRRING END", "" + what);
                    }
                    if (MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START == what) {
                        viewHolder.progressbar.setVisibility(View.GONE);
                        Log.d("RENDERING END", "" + what);
                    }
                    return false;
                }
            });
            viewHolder.video.setOnErrorListener(new OnErrorListener() {

                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    // TODO Auto-generated method stub

                    Log.d("Error", "------>error" + what + "/" + extra);
                    if (extra != 0) {
                        Toast.makeText(MainActivity.this, "Can't Play Video",
                                Toast.LENGTH_SHORT).show();
                    }

                    return false;
                }
            });

            int pos;
            pos=viewHolder.video.getCurrentPosition();
            Log.d("current item position:",""+pos);
             convertView.setTag(viewHolder);

        }
            else{

            viewHolder = (ViewHolderItem) convertView.getTag();

         }

        return convertView;
      }    
    }

}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:orientation="vertical"
    android:paddingBottom="2px"
    android:paddingLeft="2px"
    android:paddingRight="2px"
    android:paddingTop="2px" >

    <VideoView
        android:id="@+id/VideoView"
        android:layout_width="match_parent"
        android:layout_height="100dp" >
    </VideoView>
    <ProgressBar 
        android:id="@+id/progress"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        />
<TextView 
    android:id="@+id/txvposition"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginTop="10dp"
    android:text="video"
    />
</LinearLayout>

activity_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_row_selector" />

</RelativeLayout>

我如何解决这个问题?我想在listview中按顺序排列所有项目而不重复。我想按顺序播放所有视频,当我滚动listview时,它必须按顺序排列。当我将第一个视频作为列表视图中的最后一个滚动时,我在listview中有八个视频,序列也改变了我如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您没有以正确的方式使用ListView回收机制,尝试更改您的getView()方法:

    public View getView(int position, View convertView, ViewGroup parent) {
              final ViewHolderItem viewHolder;


              if (convertView == null){
                   inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                   convertView = inflater.inflate(R.layout.activity_main, null);

                    viewHolder = new ViewHolderItem();
                    viewHolder.video = (VideoView)convertView.findViewById(R.id.VideoView);
                    viewHolder.progressbar = (ProgressBar)convertView.findViewById(R.id.progress);

                    viewHolder.textViewItem =(TextView)convertView.findViewById(R.id.txvposition);


                    int pos;
                    pos=viewHolder.video.getCurrentPosition();
                    Log.d("current item position:",""+pos);
                     convertView.setTag(viewHolder);

              }else{
                 viewHolder = (ViewHolderItem) convertView.getTag();

              }

              viewHolder.textViewItem.setText("Video"+position);
              mediaController.setAnchorView(viewHolder.video);
              Log.d("video url inadapter:", ""+videoLIst.toString());
              Uri uri = Uri.parse(videoList.get(position));
              viewHolder.video.setMediaController(mediaController);
              viewHolder.video.setVideoURI(uri);
              //viewHolder.video.start();
              viewHolder.video.setOnPreparedListener(new OnPreparedListener() {
                      public void onPrepared(MediaPlayer arg0) {
                            viewHolder.progressbar.setVisibility(View.GONE);
                            viewHolder.video.start();
                      }
                    });
              viewHolder.video.setOnInfoListener(new OnInfoListener() {
                       @Override
                        public boolean onInfo(MediaPlayer mp, int what, int extra) {
                          // TODO Auto-generated method stub
                          Log.d("BUFFRRING START", "On_InFO");
                          if (MediaPlayer.MEDIA_INFO_BUFFERING_START == what) {
                                viewHolder.progressbar.setVisibility(View.VISIBLE);
                              Log.d("BUFFRRING START", "" + what);
                          }
                          if (MediaPlayer.MEDIA_INFO_BUFFERING_END == what) {
                                viewHolder.progressbar.setVisibility(View.GONE);
                                Log.d("BUFFRRING END", "" + what);
                          }
                          if (MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START == what) {
                                viewHolder.progressbar.setVisibility(View.GONE);
                                Log.d("RENDERING END", "" + what);
                          }
                          return false;
                        }
                    });
               viewHolder.video.setOnErrorListener(new OnErrorListener() {

                        @Override
                        public boolean onError(MediaPlayer mp, int what, int extra) {
                            // TODO Auto-generated method stub

                            Log.d("Error", "------>error" + what + "/" + extra);
                            if (extra != 0) {
                                Toast.makeText(MainActivity.this, "Can't Play Video",
                                        Toast.LENGTH_SHORT).show();
                            }

                            return false;
                        }
                    });
        }

答案 1 :(得分:0)

如果convertView不为null,则表示您没有为列表视图的当前行创建视图。