Last active
June 8, 2018 06:43
-
-
Save wightwulf1944/16b1f1cf9cf1a3155df5e8e284f52a71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.ConstraintLayout 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" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Hello World!" | |
android:textSize="50sp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<View | |
android:id="@+id/bottom_bar" | |
android:layout_width="0dp" | |
android:layout_height="60dp" | |
android:background="@android:color/holo_red_dark" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" /> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/page_carousel" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
app:layout_constraintBottom_toBottomOf="@id/bottom_bar" | |
app:layout_constraintEnd_toEndOf="@id/bottom_bar" | |
app:layout_constraintStart_toStartOf="@id/bottom_bar" | |
app:layout_constraintTop_toTopOf="@id/bottom_bar" | |
tools:listitem="@layout/item_pagecarousel" /> | |
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package shiro.am.i.sliderexample; | |
import android.content.Context; | |
import android.support.annotation.LayoutRes; | |
import android.support.annotation.NonNull; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.LinearSnapHelper; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
public class CarouselDecorator { | |
private final Context context; | |
private final int itemLayout; | |
private final Adapter adapter; | |
private final LinearLayoutManager layoutManager; | |
private int pageCount; | |
private OnPageChangeListener onPageChangeListener; | |
public CarouselDecorator(Context context, @LayoutRes int itemLayout) { | |
this.context = context; | |
this.itemLayout = itemLayout; | |
adapter = new Adapter(); | |
layoutManager = new LinearLayoutManager(context); | |
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); | |
} | |
public void setPageCount(int pageCount) { | |
this.pageCount = pageCount; | |
adapter.notifyDataSetChanged(); | |
} | |
public void setCurrentPage(int page) { | |
layoutManager.scrollToPosition(page - 1); | |
} | |
public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener) { | |
this.onPageChangeListener = onPageChangeListener; | |
} | |
public void decorate(RecyclerView recyclerView) { | |
recyclerView.setLayoutManager(layoutManager); | |
recyclerView.setAdapter(adapter); | |
recyclerView.addOnScrollListener(new OnScrollListener()); | |
LinearSnapHelper snapHelper = new LinearSnapHelper(); | |
snapHelper.attachToRecyclerView(recyclerView); | |
} | |
private class OnScrollListener extends RecyclerView.OnScrollListener { | |
@Override | |
public void onScrollStateChanged(RecyclerView recyclerView, int newState) { | |
if (newState != RecyclerView.SCROLL_STATE_IDLE) return; | |
int position = layoutManager.findFirstVisibleItemPosition(); | |
onPageChangeListener.onPageChange(position + 1); | |
} | |
} | |
private class Adapter extends RecyclerView.Adapter<ViewHolder> { | |
@NonNull | |
@Override | |
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
LayoutInflater inflater = LayoutInflater.from(context); | |
View view = inflater.inflate(itemLayout, parent, false); | |
return new ViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { | |
holder.textView.setText(String.valueOf(position + 1)); | |
} | |
@Override | |
public int getItemCount() { | |
return pageCount; | |
} | |
} | |
private static class ViewHolder extends RecyclerView.ViewHolder { | |
private final TextView textView; | |
private ViewHolder(View itemView) { | |
super(itemView); | |
textView = (TextView) itemView; | |
} | |
} | |
public interface OnPageChangeListener { | |
void onPageChange(int page); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<TextView 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" | |
android:background="@android:color/darker_gray" | |
android:gravity="center" | |
android:textSize="18sp" | |
tools:text="1" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package shiro.am.i.sliderexample; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.RecyclerView; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
private TextView text; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
text = findViewById(R.id.text); | |
RecyclerView pageCarousel = findViewById(R.id.page_carousel); | |
pageCarousel.setHasFixedSize(true); | |
CarouselDecorator decorator = new CarouselDecorator(this, R.layout.item_pagecarousel); | |
decorator.setPageCount(5); // default 0 | |
decorator.setCurrentPage(1); // default 1 | |
decorator.setOnPageChangeListener(this::onPageChange); // will throw NPE if not set | |
decorator.decorate(pageCarousel); | |
} | |
private void onPageChange(int page) { | |
text.setText(String.valueOf(page)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment