前言:
如果你是要找像是ListView的setOnItemClickListener 可以看我的這篇。
Android方法(RecyclerView) — RecyclerView外接到activity點擊事件
我的Android年齡實在很短,在我接觸Android的時候,已經是RecyclerView和ListView並存的時候了。
簡而言之RecyclerView的回收機制和各種都趨近完善,比ListView強大很多。
RecyclerView最大的好處就是各種強大的排版方式。
RecylerView在使用上多了幾項必須的東西。
必須而外創建LinearManager ,這可以幫助你不用一直findViewById節省不必要的資源。
ViewHolder也變成強制必須加上的方法。
完全程式碼:
免不了的import
//主要是這一個 compile 'com.android.support:recyclerview-v7:25.2.0'
//這個是網路下載圖片的IMPORT compile 'com.github.bumptech.glide:glide:3.7.0'
我是使用Gidle在讀取圖片:
第一件事情是給權限,我在RecyclerView內的圖片是網路下載
<uses-permission android:name="android.permission.INTERNET"/>
此次動到的xml and java
在 res/value/drawable 內丟一張讀取中(loading)要使用到的圖片
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.chat.a015865.pointoforigin.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:orientation="horizontal"> <ImageView android:id="@+id/image_view" android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="5dp" /> <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="hello" /> </LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerViewAdapter mRecyclerViewAdapter;
private Context mContext;
private String[] urlList;
private ItemData[] itemsData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
initView(); //設定View (Eg:findViewById)
initSet(); //設定功能 (Eg:setAdapter)
initListener(); //設定點擊 (eg:onClick)
}
private void initListener(){
}
private void initView() {
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
}
private void initSet() {
setData();
mRecyclerViewAdapter = new RecyclerViewAdapter(mContext);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(llm);
mRecyclerView.setAdapter(mRecyclerViewAdapter);
mRecyclerViewAdapter.upData(itemsData);
}
private void setData() {
urlList = new String[]{
"http://www.jrtstudio.com/sites/default/files/ico_android.png",
"https://pbs.twimg.com/profile_images/616076655547682816/6gMRtQyY.jpg",
"http://cdn.bgr.com/2012/11/android-icon.jpg?quality=98&strip=all",
"http://www.fastbooting.com/uploads/5/5/9/9/55994511/1474140_orig.png",
"http://www.pendidikan-diy.go.id/dinas_v4/img/up_down/Android.png",
};
itemsData = new ItemData[]{
new ItemData("abc", urlList[0]),
new ItemData("defaf", urlList[1]),
new ItemData("erhrh", urlList[2]),
new ItemData("GDERGE", urlList[3]),
new ItemData("EGER", urlList[4]),
new ItemData("QHRE", urlList[0]),
new ItemData("QEHE", urlList[1]),
new ItemData("RTHRT", urlList[2]),
new ItemData("YJTEY", urlList[3]),
new ItemData("RGEW", urlList[4])
};
}
}
RecyclerViewAdapter.java
/** * Created by 015865 on 2017/3/13. */ public class RecyclerViewAdapter extends RecyclerView.Adapter{ private ItemData[] itemsData; private Context mContext; public RecyclerViewAdapter(Context context) { mContext = context; } public void upData(ItemData[] item) { itemsData = item; notifyDataSetChanged(); } public class MyHolder extends RecyclerView.ViewHolder { private ImageView mImageView; private TextView mTextView; public MyHolder(View itemView) { super(itemView); mImageView = (ImageView) itemView.findViewById(R.id.image_view); mTextView = (TextView) itemView.findViewById(R.id.text_view); } } @Override public RecyclerViewAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false); MyHolder mh = new MyHolder(view); return mh; } @Override public void onBindViewHolder(RecyclerViewAdapter.MyHolder holder, int position) { Glide.with(mContext).load(itemsData[position].getImageUrl()) .error(R.drawable.ico_android) .placeholder(R.drawable.tool) .into(holder.mImageView); holder.mTextView.setText(itemsData[position].getTitle()); } @Override public int getItemCount() { return itemsData == null ? 0 : itemsData.length; } }
ItemData.java
public class ItemData { private String title; private String imageUrl; public ItemData(String title, String imageUrl) { this.title = title; this.imageUrl = imageUrl; Log.d("ItemData", "CheckPoint imageUrl=" + imageUrl); } public String getTitle() { return title; } public String getImageUrl() { return imageUrl; } public void setTitle(String title) { this.title = title; } public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } }
更新(2017/03/14)
RecyclerView的點擊事件item
重點程式碼:
首先先做出RecyclerView每個Item的點擊事件。
兩個地方需要更改!
我們從MyHolder下去著手。
RecyclerViewAdapter.java
1、MyHolder讓每個hold住的畫面都有點擊事件
public class MyHolder extends RecyclerView.ViewHolder { private ImageView mImageView; private TextView mTextView; private View view; public MyHolder(View itemView) { super(itemView); view = itemView; mImageView = (ImageView) itemView.findViewById(R.id.image_view); mTextView = (TextView) itemView.findViewById(R.id.text_view); } public void setOnItemClick(View.OnClickListener l) { this.view.setOnClickListener(l); } }
2、再從onBindViewHolder下手,設定每個點擊事件要做的事情
@Override public void onBindViewHolder(final RecyclerViewAdapter.MyHolder holder, final int position) { Glide.with(mContext).load(itemsData[position].getImageUrl()) .error(R.drawable.loading) .placeholder(R.drawable.tool) .into(holder.mImageView); holder.mTextView.setText(itemsData[position].getTitle()); holder.setOnItemClick(new View.OnClickListener() { @Override public void onClick(View v) { //設定你點擊每個Item後,要做的事情 } }); }
這就能完成每次的點擊事件。
Demo:
https://drive.google.com/open?id=0Byk75IYx-dKXR0lnZW9RQU90MDQ
網路上方式很多也超級詳細,不再贅述
小鰻:http://lp43.blogspot.tw/2014/08/recyclerviewandroid-studio.html
沒有留言:
張貼留言