I'm sure you've met the effect of galleries in projects. There are many ways on the Internet, such as viewPager, recyclerview and so on. Today, we recommend a three-party Gallery layoutmanager, which is easy to realize quickly and solve the urgent problem.
rely on
gradle dependence
compile 'github.hellocsl:GalleryLayoutManager:1.0.6'
Realization
Layout file
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
adapter set by recyclerView
private class Adapter extends RecyclerView.Adapter<RecyclerHolder> { private Context context; private Adapter(Context context) { this.context = context; } @Override public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_view, null); //Customize the width of the view and control the number of displays on one screen LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); int width = context.getResources().getDisplayMetrics().widthPixels; params.width = width / 3; view.setLayoutParams(params); return new RecyclerHolder(view); } @Override public void onBindViewHolder(RecyclerHolder holder, int position) { } @Override public int getItemCount() { return 10; } } private class RecyclerHolder extends RecyclerView.ViewHolder { private View view; public RecyclerHolder(View itemView) { super(itemView); view = itemView; } public View getView() { return view; } }
Each item simply places a layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="100dp" android:layout_height="wrap_content" android:src="@mipmap/dota" /> </LinearLayout>
The third-party GalleryLayoutManager is used to bind with recyclerView and set as horizontal sliding layout
GalleryLayoutManager manager = new GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL); manager.attach(recycler); //Set sliding zoom effect manager.setItemTransformer(new Transformer()); recycler.setAdapter(new Adapter(this));
Zoom effect processing
//Scaling during sliding public class Transformer implements GalleryLayoutManager.ItemTransformer { @Override public void transformItem(GalleryLayoutManager layoutManager, View item, float fraction) { //Scale with center item.setPivotX(item.getWidth() / 2.0f); item.setPivotY(item.getHeight() / 2.0f); float scale = 1 - 0.3f * Math.abs(fraction); item.setScaleX(scale); item.setScaleY(scale); } }
You can customize the width of each view in RecyclerView, control the display effect of one screen, and operate in onCreateViewHolder of adapter
@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_view, null); //Customize the width of the view and control the number of displays on one screen LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); int width = context.getResources().getDisplayMetrics().widthPixels; params.width = width / 3; view.setLayoutParams(params); return new RecyclerHolder(view); }
Sliding Monitoring
Slide to monitor the final stop position
manager.setOnItemSelectedListener(new GalleryLayoutManager.OnItemSelectedListener() { @Override public void onItemSelected(RecyclerView recyclerView, View item, int position) { //Slide to position of an item } });
At the same time, it supports clicking on each item to switch, and in the onBindViewHolder method of adapter
@Override public void onBindViewHolder(RecyclerHolder holder, final int position) { holder.getView().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { recycler.smoothScrollToPosition(position); } }); }
So far, a simple gallery effect implementation, if your project just needs this, and I have it.
Of course, this is just a brief introduction to the realization of gallery effect. This library also provides the effect of sliding up and down