当前位置:   article > 正文

基于RecyclerView实现ViewPager的功能(横向)_横向recycalview

横向recycalview

说下大概思想吧,RecyclerView的使用大家都很熟悉了(不熟悉的请自行百度),在做项目的时候,需要实现一个图片界面横向滑动的功能,之前用RecycleView时一直做的都是垂直滑动的,当然使用ViewPager可以轻易完成。然而我就是想用RecycleView完成((⊙﹏⊙)程序员死磕病又犯了)。

1.首先item文件,因为需要,每个Item都是宽度与屏幕等长,要在代码中实现,不然图片宽度会不一样
item_photo_preview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    >

    <ImageView
        android:id="@+id/photo_preview_img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/face"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2.适配器
PhotoPreviewAdapter.java

package com.phc.act.myphcapp.adapter;

import android.content.Context;
import android.graphics.Point;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.phc.act.myphcapp.R;
import com.phc.act.myphcapp.app.MyApp;
import com.phc.act.myphcapp.viewholder.PhotoPreviewViewHolder;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/11/2.
 */
public class PhotoPreviewAdapter extends RecyclerView.Adapter<PhotoPreviewViewHolder> {
    ArrayList<CharSequence> data;
    Context context;

    public PhotoPreviewAdapter() {
        data = new ArrayList<>();
    }

    @Override
    public PhotoPreviewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null)
            context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo_preview, parent, false);
        PhotoPreviewViewHolder viewHolder = new PhotoPreviewViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(PhotoPreviewViewHolder holder, int position) {
        Point p = new Point();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getSize(p);
        //之所以高度是MATCH_PARENT 是因为图片会根据高度居中显示,否则会在屏幕上方
        holder.img.setLayoutParams(new LinearLayout.LayoutParams(p.x, ViewGroup.LayoutParams.MATCH_PARENT));

        MyApp.loadImage(data.get(position).toString(), holder.img, context);
    }

    public ArrayList<CharSequence> getData() {
        return data;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

3.ViewHolder

PhotoPreviewViewHolder.java

package com.phc.act.myphcapp.viewholder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.phc.act.myphcapp.R;

/**
 * Created by Administrator on 2015/11/2.
 */
public class PhotoPreviewViewHolder extends RecyclerView.ViewHolder  {
    public ImageView img;
    public PhotoPreviewViewHolder(final View itemView) {
        super(itemView);
        img = (ImageView) itemView.findViewById(R.id.photo_preview_img);

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

4.主界面
重头戏来了,主要是实现两个接口
View.OnTouchListener, GestureDetector.OnGestureListener
前一个是获取用户触摸的Event,之后传递给GestureDetector对象,分析用户的手势
PhotoPreviewActivity.java

package com.phc.act.myphcapp.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.phc.act.myphcapp.R;
import com.phc.act.myphcapp.adapter.PhotoPreviewAdapter;
import com.phc.act.myphcapp.constant.Constant;

public class PhotoPreviewActivity extends AppCompatActivity implements View.OnTouchListener, GestureDetector.OnGestureListener {

    RecyclerView recyclerView;
    PhotoPreviewAdapter adapter;
    int currentPosition = 0;
    GestureDetector detector;
    RelativeLayout relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_preview);
        relativeLayout = (RelativeLayout) findViewById(R.id.photo_preview_mainLayout);
        recyclerView = (RecyclerView) findViewById(R.id.photo_preview_recycleView);
        recyclerView.setOnTouchListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new PhotoPreviewAdapter();
        detector = new GestureDetector(this, this);

        recyclerView.setHasFixedSize(true);
        adapter.getData().addAll(getIntent().getCharSequenceArrayListExtra(Constant.PHOTOARRAYLIST));
        recyclerView.setAdapter(adapter);
        recyclerView.setHorizontalScrollBarEnabled(false);
        adapter.notifyDataSetChanged();
        recyclerView.smoothScrollToPosition(1);

    }


    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.i("phc", "onFling");
        if (e1.getX() - e2.getX() > 0 && currentPosition < recyclerView.getChildCount()) {
            currentPosition++;
            // 手向左滑动,图片下一张
        } else if (e2.getX() - e1.getX() > 0 && currentPosition > 0) {
            // 向右滑动,图片上一张
            currentPosition--;

        }
        recyclerView.smoothScrollToPosition(currentPosition);

        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.i("phc", "onTouch");
        //传递event给GestureDetector对象
        detector.onTouchEvent(event);
        return true;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

5.主视图
activity_photo_preview.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.phc.act.myphcapp.activity.PhotoPreviewActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

    </android.support.design.widget.AppBarLayout>

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:id="@+id/photo_preview_mainLayout">


    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/photo_preview_recycleView"
        android:scrollbars="horizontal"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        />

</RelativeLayout>



</android.support.design.widget.CoordinatorLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

PS:因为是用android studio做的,所以自动生成的代码有点多,有错的地方请指正!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/174755
推荐阅读
相关标签
  

闽ICP备14008679号