文章目录
- 1、功能简介
- 2、文件结构
- 3、build.gradle(Module:app)
- 4、activity_main.xml 文件
- 5、recycleview_item.xml
- 6、RecycleViewAdapter 文件
- 7、StudentData 文件
- 8、MainActivity 文件
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);
}
}
}