Android之---RecycleView实现简单的图片查看功能(类似相册查看器)

RecycleView实现简单的图片查看功能(类似相册查看器)

效果图

自定义PreviewRecycleView继承自RecycleView

public class PreviewRecycleView extends RecyclerView {

    public PreviewRecycleView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            /**
             *
             * 滚动时,判断当前第一个View是否发生变化,发生才回调
             */
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                    View newView = getChildAt(0);

                    if (mItemScrollChangeListener != null)
                    {
                        if (newView != null && newView != mCurrentView)
                        {
                            mCurrentView = newView ;
                            mItemScrollChangeListener.onChange(mCurrentView,
                                    getChildPosition(mCurrentView));

                        }
                    }

            }
        });
    }

    /**
     * 记录当前第一个View
     */
    private View mCurrentView;

    private OnItemScrollChangeListener mItemScrollChangeListener;

    public void setOnItemScrollChangeListener(
            OnItemScrollChangeListener mItemScrollChangeListener)
    {
        this.mItemScrollChangeListener = mItemScrollChangeListener;
    }

    public interface OnItemScrollChangeListener
    {
        void onChange(View view, int position);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);

        mCurrentView = getChildAt(0);

        if (mItemScrollChangeListener != null)
        {
            mItemScrollChangeListener.onChange(mCurrentView,
                    getChildPosition(mCurrentView));
        }
    }
}

PreviewRecycleView的Adapter

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> {
    /**
     * ItemClick的回调接口
     *
     */
    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    private LayoutInflater mInflater;
    private List mDatas;

    public RecycleViewAdapter(Context context, List datats)
    {
        mInflater = LayoutInflater.from(context);
        mDatas = datats;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        public ViewHolder(View arg0)
        {
            super(arg0);
        }

        ImageView mImg;
    }

    @Override
    public int getItemCount()
    {
        return mDatas.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        View view = mInflater.inflate(R.layout.activity_index_gallery_item,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);

        viewHolder.mImg = (ImageView) view
                .findViewById(R.id.id_index_gallery_item_image);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i)
    {
        viewHolder.mImg.setImageResource(mDatas.get(i));

        //如果设置了回调,则设置点击事件
        if (mOnItemClickLitener != null)
        {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    mOnItemClickLitener.onItemClick(viewHolder.itemView, i);
                }
            });

        }

    }

}

MainActicity.java

public class MainActivity extends AppCompatActivity {

    private PreviewRecycleView mRecyclerView;
    private RecycleViewAdapter mAdapter;
    private List mDatas;
    private ImageView mImg ;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        mImg = (ImageView) findViewById(R.id.id_content);

       initData();

        mRecyclerView = (PreviewRecycleView) findViewById(R.id.id_recyclerview_horizontal);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new RecycleViewAdapter(this, mDatas);
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.setOnItemScrollChangeListener(new PreviewRecycleView.OnItemScrollChangeListener()
        {
            @Override
            public void onChange(View view, int position)
            {
                mImg.setImageResource(mDatas.get(position));
            };
        });

        mAdapter.setOnItemClickLitener(new RecycleViewAdapter.OnItemClickLitener()
        {
            @Override
            public void onItemClick(View view, int position)
            {
              Toast.makeText(getApplicationContext(), position + "", Toast.LENGTH_SHORT)
                      .show();
                mImg.setImageResource(mDatas.get(position));
            }
        });

    }

    /*

    初始化数据
     */
    private void initData() {
        mDatas=new ArrayList(
                Arrays.asList(
                        R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d,
                        R.mipmap.e,R.mipmap.f,R.mipmap.g,R.mipmap.h,
                        R.mipmap.r,R.mipmap.g,R.mipmap.k,R.mipmap.l,
                        R.mipmap.m,R.mipmap.n,R.mipmap.o
                )
        );
    }
}

底部指示器的Item布局

"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    android:layout_width="120dp"
    android:layout_height="120dp">

    "@+id/id_index_gallery_item_image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:layout_margin="5dp"
        android:scaleType="centerCrop" />

activity_main.xml

"http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    "fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        "@+id/id_content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher" />
    

    "@+id/id_recyclerview_horizontal"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_gravity="bottom"
        android:scrollbars="none" />

Github源码地址:https://github.com/xiaokele/RecycleView_Preview

你可能感兴趣的:(Android)