遇到的異常:java
java.lang.ExceptionInInitializerError靜態塊初始化異常android
NetworkOnMainThreadException訪問網線不能在主線程中進行app
個人參考資料:ide
http://www.oschina.net/question/1764894_246656?fromerr=ZElwOffs
老師給的源代碼佈局
recyclerView 和cartView的導入方法post
別人的都是:
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'動畫
可是個人自動添加是這樣的:
compile 'com.android.support:recyclerview-v7:24.0.0 -beat'
compile 'com.android.support:cardview-v7:24.0.0 -beat'
可是就算這樣也是不成功的this
個人只能這樣寫:
compile 'com.android.support:recyclerview-v7:21.+'
compile 'com.android.support:cardview-v7:21.+'spa
RecyclerView的使用.net
1頁面佈局
1 <android.support.v7.widget.RecyclerView 2 android:id="@+id/rcView" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 />
2初始化控件
RecyclerView rcView; rcView = (RecyclerView) this.findViewById(R.id.rcView);
3給它數據(圖片、文字等等)
List<Integer> imageUrls; Integer[] imgs ={ R.drawable.happy1,R.drawable.happy2,R.drawable.happy3,R.drawable.happy4, R.drawable.happy5,R.drawable.happy6,R.drawable.happy7,R.drawable.happy8 }; imageUrls = new ArrayList<>(); imageUrls.addAll(Arrays.asList(imgs));
4適配器
public class MyApdater extends RecyclerView.Adapter<MyApdater.MyViewHolder> { private Context context; private LayoutInflater inflater; private List<Integer> imageList; private OnItemClickListener itemClickListener; public interface OnItemClickListener{ void onItemClick(View view, int position); } public void setOnItemClickListener(OnItemClickListener itemClickListener){ this.itemClickListener = itemClickListener; } public MyApdater(Context context, List<Integer> imageList) { this.context = context; this.imageList = imageList; inflater = LayoutInflater.from(context); setHasStableIds(true); } @Override public long getItemId(int position) { return position; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new MyViewHolder(inflater.inflate(R.layout.list_item,parent,false)); } @Override public int getItemCount() { return imageList.size(); } @Override public void onBindViewHolder(final MyViewHolder holder, final int position) { int e = imageList.get(position); holder.img_item.setImageResource(e); // x.image().bind(holder.img_item,imageList.get(position),options); /*if(itemClickListener != null){ holder.img_item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // int pos = holder.getPosition(); //獲取當前Item的postion itemClickListener.onItemClick(holder.img_item,position); } }); }*/ } class MyViewHolder extends RecyclerView.ViewHolder{ private ImageView img_item; public MyViewHolder(View itemView) { super(itemView); img_item = (ImageView) itemView.findViewById(R.id.img_item); } } }
5素材xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:orientation="horizontal" card_view:cardBackgroundColor="#EEEEEE" card_view:cardCornerRadius="5dp"> <ImageView android:src="@drawable/happy1" android:id="@+id/img_item" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.v7.widget.CardView>
6在MainActivity中使用適配器
MyApdater myApdater; myApdater = new MyApdater(this,imageUrls); rcView.setAdapter(myApdater);
7設置RecyclerView的動畫、顯示等效果
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL); rcView.setItemAnimator(new DefaultItemAnimator()); //rcView.addItemDecoration(); rcView.setLayoutManager(layoutManager);
附:MainActivity全代碼
package com.firefly.recyclerview; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class MainActivity extends ActionBarActivity { RecyclerView rcView; MyApdater myApdater; List<Integer> imageUrls; Integer[] imgs ={ R.drawable.happy1,R.drawable.happy2,R.drawable.happy3,R.drawable.happy4, R.drawable.happy5,R.drawable.happy6,R.drawable.happy7,R.drawable.happy8 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); init(); } private void init() { rcView = (RecyclerView) this.findViewById(R.id.rcView); //GridLayoutManager layoutManager = new GridLayoutManager(this,2); //LinearLayoutManager layoutManager = new LinearLayoutManager(this); //layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL); rcView.setItemAnimator(new DefaultItemAnimator()); //rcView.addItemDecoration(); rcView.setLayoutManager(layoutManager); myApdater = new MyApdater(this,imageUrls); rcView.setAdapter(myApdater); /* myApdater.setOnItemClickListener(new MyApdater.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this, position + " click", Toast.LENGTH_SHORT).show(); imageUrls.remove(position); myApdater.notifyItemRemoved(position); } });*/ } protected void initData() { imageUrls = new ArrayList<>(); imageUrls.addAll(Arrays.asList(imgs)); } }