這篇就寫簡單基本的用法跟應用。
準備好一些圖片放到專案的drawable資料夾裡 (這邊準備 img_1.png~img_9.png)
首先要先加入所需要的工具,在 app 的 build.gradle 裡面的 dependencies 加入這行:
implementation 'com.android.support:design:26.1.0' // for RecyclerView
在 res/layout 中建立一個要放在RecyclerView裡面的單一物件的 layout.xml
recycler_item.xml:
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/id_ivItem"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:src="@drawable/img_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
接著在專案java資料夾中建立一個 class 來處理RecyclerView的一些細節設計
RecyclerMainAdapter.java:
public class RecyclerMainAdapter extends RecyclerView.Adapter{ private String TAG = "RecyclerMainAdapter"; private List mData; class ViewHolder extends RecyclerView.ViewHolder { ImageView mImageView; ViewHolder(View v) { super(v); // find item view mImageView = (ImageView)v.findViewById(R.id.id_ivItem); } } RecyclerMainAdapter(List data) { mData = data; } @Override public RecyclerMainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false); return new ViewHolder(v); } @Override public void onBindViewHolder(final RecyclerMainAdapter.ViewHolder holder, final int position) { // set the image from imgUrls by position for(int i = 0; i < MainActivity.imgArray.length; i++){ if(position == i){ holder.mImageView.setImageResource(MainActivity.imgArray[i]); } } } @Override public int getItemCount() { return mData.size(); } }
在 onBindViewHolder 中可以設定每個 item 的圖片來源,這邊是抓 MainActivity 的 imgArray 陣列已經存好的圖檔路徑。
另外,如果要針對每個 item 加入 click 事件的話,可以在 onBindViewHolder 裡面加入:
// set onclick listener on recyclerView
holder.mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick " + position);
}
});
在主要layout中加入RecyclerView物件
activity_main.xml:
<android.support.v7.widget.RecyclerView
android:id="@+id/id_rv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
最後加入主程式碼
MainActivity.java
public class MainActivity extends AppCompatActivity{
private String TAG = "MainActivity";
public static int imgArray[] = {
R.drawable.img_1, R.drawable.img_2, R.drawable.img_3,
R.drawable.img_4, R.drawable.img_5, R.drawable.img_6,
R.drawable.img_7, R.drawable.img_8, R.drawable.img_9};
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.id_rv);
// init recyclerView
InitRecyclerView();
}
private void InitRecyclerView(){
ArrayList myDataset = new ArrayList<>();
for(int i = 0; i < imgArray.length; i++){
myDataset.add(Integer.toString(i));
}
// set layout for recyclerview
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); // set HORIZONTAL or VERTICAL
// set cover adapter and recyclerview
RecyclerMainAdapter mRecyclerMainAdapter = new RecyclerMainAdapter(myDataset);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mRecyclerMainAdapter);
}
}
程式執行後就能看到一個滑動的圖片集了。
在這行可以設定RecyclerView的垂直或水平方向:
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); // set HORIZONTAL or VERTICAL
如果放開 RecyclerView 的時候,想要讓圖片每次自動固定在中間,可以加入:
// set snap on recyclerview final SnapHelper helper = new LinearSnapHelper(); helper.attachToRecyclerView(mRecyclerView);
如果想要知道目前滑到哪一個 item,則可以加入:
// get current position from recycleView
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
View centerView = helper.findSnapView(layoutManager);
int pos = 0;
if (centerView != null) {
pos = layoutManager.getPosition(centerView);
Log.d(TAG, "RecyclerView: " + pos);
}
}
}
});
其中 pos 就是目前滑到的 item。
另外,如果要直接跳到某個 item 的話,例如要跳到第二個 item 則可以用:
// jump to certain position mRecyclerView.smoothScrollToPosition(2);
做完之後發現 RecyclerView 滑到底的時候,邊邊會跑出一個半透明的提示圖案。
只要加一行到 activity_main.xml 裡面的 RecyclerView 物件就好了:
android:overScrollMode="never"
這邊有碰到一個小問題就是,當滑到第一個跟最後一個 item 會沒辦法置中,
一個簡單的解法就是在第一個跟最後一個用透明圖檔的 item 來當作緩衝。
public static int imgArray[] = {
R.drawable.transparent,
R.drawable.img_1, R.drawable.img_2, R.drawable.img_3,
R.drawable.img_4, R.drawable.img_5, R.drawable.img_6,
R.drawable.img_7, R.drawable.img_8, R.drawable.img_9,
R.drawable.transparent};
然後在 mRecyclerView.addOnScrollListener 的 onScrollStateChanged 裡面加入判斷,
當滑到第一個或最後一個時,會往後或往前推一下
// get current position from recycleView
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
View centerView = helper.findSnapView(layoutManager);
int pos = 0;
if (centerView != null) {
pos = layoutManager.getPosition(centerView);
Log.d(TAG, "RecyclerView: " + pos);
}
// block first and last space
if(pos == 0){
mRecyclerView.smoothScrollToPosition(1);
}
else if(pos == imgArray.length - 1){
mRecyclerView.smoothScrollToPosition(imgArray.length - 2);
}
}
}
});
END
沒有留言:
張貼留言