找不到RecyclerView?
1.打开Project Structure(ctrl + alt + shift + s)
2.如图找到v7点击加号
3.选择第一项Library dependence
4.在新窗口中的搜索框中搜索RecyclerView
RecyclerView的基本使用
android.support.v7.widget.RecyclerView
android.support.v7.widget.RecyclerView.Adapter
RecyclerView封装了ViewHolder,可以直接通过传RecyclerView内容布局给onCreateViewHolder方法创建ViewHolder,然后可以在一个继承于RecyclerView.ViewHolder的类中获取布局中的控件,RecyclerView内容的更新在方法onBindViewHolder中实现,RecyclerView的item数量在方法getItemCount中返回。实现一个基本的RecyclerView只需要3个方法和一个ViewHolder类。
以下为demo:
MainActivity.class
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private MyAdapter adapter;
private boolean flag=false;
private String[] myDataset=new String[15];
private final static String TAG=MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recy);
int i=0;
while(i<15){
myDataset[i]=""+i;
++i;
}
RecyclerView.LayoutManager layoutManager;
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter=new MyAdapter(myDataset);
upData();
}
private void upData(){
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.setRefreshList(new MyAdapter.RefreshList() {
@Override
public void refresh() {
myDataset[adapter.positions]="change";
flag=true;
upData();
}
});
myDataset[8]="init";
if(flag){
Log.i(TAG,"flag==true");
//以下可实现只刷新第position条item
//adapter.notifyItemChanged(adapter.positions);
adapter.notifyDataSetChanged();
flag=false;
}else{
recyclerView.setAdapter(adapter);
}
}
});
}
}
MyAdapter.class:
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private String[] myDataset;
private RefreshList refreshList;
public int positions,offeset;
public MyAdapter(String[] myDataset){
this.myDataset=myDataset;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position){
holder.textView.setText(myDataset[position]);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.textView.setText("点击了");
positions=position;
refreshList.refresh();
}
});
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_layout,parent,false);
ViewHolder vh=new ViewHolder(v);
return vh;
}
@Override
public int getItemCount() {
return myDataset.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
private Button button;
private TextView textView;
private ViewHolder(View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.textView);
button=itemView.findViewById(R.id.butt);
}
}
public interface RefreshList{
void refresh();
}
public void setRefreshList(RefreshList refreshlist){
this.refreshList=refreshlist;
}
}
activity_main.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/recy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"/>
</LinearLayout>
recyclerview_layout.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">
<TextView
android:id="@+id/textView"
android:layout_width="150dp"
android:layout_height="30dp" />
<Button
android:id="@+id/butt"
android:layout_width="80dp"
android:layout_height="50dp"
android:text="确定"/>
</LinearLayout>