diff --git a/app/src/main/java/com/github/rubensousa/recyclerviewsnap/MainActivity.java b/app/src/main/java/com/github/rubensousa/recyclerviewsnap/MainActivity.java index 55ff8e2..acdb61f 100644 --- a/app/src/main/java/com/github/rubensousa/recyclerviewsnap/MainActivity.java +++ b/app/src/main/java/com/github/rubensousa/recyclerviewsnap/MainActivity.java @@ -6,22 +6,23 @@ import android.view.Gravity; import android.view.MenuItem; -import com.github.rubensousa.gravitysnaphelper.GravitySnapHelper; - -import java.util.ArrayList; -import java.util.List; - import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import com.github.rubensousa.gravitysnaphelper.GravitySnapHelper; + +import java.util.ArrayList; +import java.util.List; + public class MainActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener { public static final String ORIENTATION = "orientation"; private RecyclerView mRecyclerView; private boolean mHorizontal; + private GravitySnapHelper mGravitySnapHelper; @Override protected void onCreate(Bundle savedInstanceState) { @@ -29,6 +30,13 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); + mGravitySnapHelper = new GravitySnapHelper(Gravity.TOP, false, + new GravitySnapHelper.SnapListener() { + @Override + public void onSnap(int position) { + Log.d("Snapped", position + ""); + } + }); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.inflateMenu(R.menu.main); toolbar.setOnMenuItemClickListener(this); @@ -62,15 +70,11 @@ private void setupAdapter() { snapAdapter.addSnap(new Snap(Gravity.END, "Snap end with padding", true, apps)); snapAdapter.addSnap(new Snap(Gravity.CENTER, "GravityPager snap", false, apps)); mRecyclerView.setAdapter(snapAdapter); + mGravitySnapHelper.attachToRecyclerView(null); } else { Adapter adapter = new Adapter(false, false, apps); mRecyclerView.setAdapter(adapter); - new GravitySnapHelper(Gravity.TOP, false, new GravitySnapHelper.SnapListener() { - @Override - public void onSnap(int position) { - Log.d("Snapped", position + ""); - } - }).attachToRecyclerView(mRecyclerView); + mGravitySnapHelper.attachToRecyclerView(mRecyclerView); } }