recycleView 簡單模板框架
阿新 • • 發佈:2018-11-15
文章目錄
1、功能簡介
實現recycle 和 自定義 item 的適配
讀取 姓名
2、檔案結構
3、build.gradle(Module:app)
新增 recycleView 編譯引用
compile ‘com.android.support:recyclerview-v7:26.1.0’
4、activity_main.xml 檔案
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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"> <LinearLayout android:id="@+id/lay_id" android:layout_marginLeft="5dp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal"> <android.support.v7.widget.RecyclerView android:id="@+id/recycle_list_id" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout> </LinearLayout>
5、recycleview_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="wrap_content" android:layout_height="match_parent" > <RelativeLayout android:id="@+id/item_lay_id" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_gravity="center" android:gravity="center"> <LinearLayout android:id="@+id/item_id" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#ff5a5a5a" android:alpha="0.7" android:gravity="center" > <TextView android:id="@+id/item_name_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name" android:textColor="#FFFFFF" android:textSize="30dp" /> </LinearLayout> </RelativeLayout> </LinearLayout>
6、RecycleViewAdapter 檔案
package com.example.lum.myrecycleview;
import android.content.Context;
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.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> implements View.OnClickListener {
private static final String TAG = "RecycleViewAdapter";
Context mContext;
private ArrayList<StudentData> mrecycleDataList;
// Provide a suitable constructor (depends on the kind of dataset)
public RecycleViewAdapter(Context context, ArrayList<StudentData> mrecycleDataList) {
this.mContext = context;
this.mrecycleDataList = mrecycleDataList;
}
private void populateLauncherView(final ViewHolder holder, StudentData studentData, int positon) {
System.out.println(TAG + " 位置 item Position:" + positon);
holder.textViewName.setText(studentData.getStudentName()+ ""); //設定
holder.textViewName.setTag(positon);
holder.relativeLayout.setTag(positon);
holder.itemView.setTag(positon);
holder.textViewName.setOnClickListener(this);
}
@Override
public int getItemViewType(int position) {
return position;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
StudentData studentData = (StudentData) mrecycleDataList.get(position);
populateLauncherView(holder, studentData,position);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mrecycleDataList.size();
}
@Override
public void onClick(View v) {
Toast.makeText(mContext, "name view this click", Toast.LENGTH_SHORT).show();
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
//初始化 icon 介面
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public RelativeLayout relativeLayout;
public TextView textViewName;
public LinearLayout mRootView;
public ViewGroup mParent;
public ViewHolder(LinearLayout v, ViewGroup parent) {
super(v);
mRootView = v;
mParent = parent;
relativeLayout = (RelativeLayout) v.findViewById(R.id.item_lay_id);
textViewName = (TextView) v.findViewById(R.id.item_name_id);
}
}
// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
LinearLayout v = (LinearLayout) LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycleview_item, parent, false);
ViewHolder vh = new ViewHolder(v, parent);
return vh;
}
}
7、StudentData 檔案
package com.example.lum.myrecycleview;
import android.util.Log;
/**
* Created by menglux on 5/11/2018.
*/
public class StudentData {
private String TAG = "StudentData: ";
private String name = "";
public StudentData(){}
public void setStudentName(String name) {
this.name = name;
Log.i(TAG,"setName: " + name );
}
public String getStudentName() {
return name;
}
}
8、MainActivity 檔案
package com.example.lum.myrecycleview;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity: ";
private RecyclerView recyclerView; //recycleView 物件
public ArrayList<StudentData> mStuDataList; //studentData 類的物件 集合
private RecycleViewAdapter mRecycleViewAdapter; //recycle 介面卡類
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// listvew 顯示
recyclerView = (RecyclerView) findViewById(R.id.recycle_list_id);
LinearLayoutManager layoutManager = new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL,false); //設定recycleView 水平
recyclerView.setLayoutManager(layoutManager);
creatListData(); //初始化 資料
mRecycleViewAdapter = new RecycleViewAdapter(this, mStuDataList); //介面卡 和 view適配
recyclerView.setAdapter(mRecycleViewAdapter);
mRecycleViewAdapter.notifyDataSetChanged(); //重新整理 view 資料
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL)); //recycleView 間隔
}
private void setTextColor() {
int position = 2; //選定的顯示學生的位置
//通過 activity 獲得adapter 裡面的元件
LinearLayoutManager mlayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int firstPosition = mlayoutManager.findFirstVisibleItemPosition();
int lastPosition = mlayoutManager.findLastVisibleItemPosition();
if (position < firstPosition || position > lastPosition) {
recyclerView.smoothScrollToPosition(position); //滾動到指定的位置
Log.i(TAG, "```````````不在顯示區域 滾動到顯示區域");
} else { //直接獲取
Log.i(TAG, "```````````在顯示區域 字型顏色改變");
//獲取view 某個位置的 item
View viewItem = mlayoutManager.getChildAt(position - (int) mlayoutManager.getChildAt(0).getTag());//通過獲取Child(0)的tag得到第一個Child的實際位置 }
if (viewItem == null) {
Toast.makeText(MainActivity.this, "viewItem is null", Toast.LENGTH_SHORT).show();
}
//獲取某個位置 item 的 viewHold
RecycleViewAdapter.ViewHolder viewHolder = (RecycleViewAdapter.ViewHolder) recyclerView.getChildViewHolder(viewItem);
//獲取某個位置 item 的 的 某個元件
viewHolder.textViewName.setTextColor(getResources().getColor(R.color.colorPrimary));//設定此位置 字型顏色藍色
}
}
protected void onResume() {
super.onResume();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
setTextColor();
}
}).start();
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void creatListData() {
Log.i(TAG,"建立 recycleList 資料");
mStuDataList = new ArrayList<StudentData>();
mStuDataList.clear();
for (int i=0; i<10; i++) { //新增 10 個空的 物件
StudentData studentData = new StudentData();
studentData.setStudentName("name: " + i); //初始化 類名
mStuDataList.add(studentData);
}
}
}