RecyclerView(1)線性佈局
阿新 • • 發佈:2018-12-12
首先需要匯入依賴和開啟許可權
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
開啟許可權
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
效果圖
Activity程式碼
package com.example.recyclerview.activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.OrientationHelper; import android.support.v7.widget.RecyclerView; import com.example.recyclerview.R; import com.example.recyclerview.User; import com.example.recyclerview.adapter.OneAdapter; public class OneActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //①類似於ListView的linearLayoutManager的線性佈局,水平排練:橫向滑動ScrollView //②類似於GridView的網格佈局 //三。瀑布流StaggeredGridLayoutManager羅列混排佈局 RecyclerView recyclerView = findViewById(R.id.recyclerview); LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(OrientationHelper.VERTICAL); //設定佈局管理器 recyclerView.setLayoutManager(layoutManager); //設定為垂直佈局,這也是預設的 OneAdapter normalAdapter = new OneAdapter(); for (int i = 0; i < 10; i++) { User user = new User(); user.setName("張"+i); normalAdapter.addItem(user); } //設定Adapter recyclerView.setAdapter(normalAdapter); //設定分隔線 // recyclerView.addItemDecoration( new DividerGridItemDecoration(this )); //設定增加或刪除條目的動畫 recyclerView.setItemAnimator( new DefaultItemAnimator()); } }
對應的xml程式碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
Adapter程式碼
package com.example.recyclerview.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.recyclerview.R;
import com.example.recyclerview.User;
import java.util.ArrayList;
import java.util.List;
public class OneAdapter extends RecyclerView.Adapter<OneAdapter.VH> {
public void addItem(User user) {
if (user != null)
mDatas.add(user);
}
//② 建立ViewHolder
public static class VH extends RecyclerView.ViewHolder {
public final TextView title;
public final ImageView avatar;
public VH(View v) {
super(v);
title = v.findViewById(R.id.title);
avatar = v.findViewById(R.id.avatar);
}
}
private List<User> mDatas;
public OneAdapter() {
this.mDatas = new ArrayList<>();
}
//③ 在Adapter中實現3個方法
@Override
public void onBindViewHolder(VH holder, int position) {
User user = mDatas.get(position);
holder.title.setText(user.getName());
// holder.avatar.setImageResource(user.getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//item 點選事件
}
});
}
@Override
public int getItemCount() {
return mDatas.size();
}
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
//LayoutInflater.from指定寫法
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.one, parent, false);
return new VH(v);
}
}
adapter對應的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/avatar"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"/>
</LinearLayout>