@Allen-miss
2017-05-18T06:08:40.000000Z
字数 4236
阅读 509
Recyclerview
RecyclerView是ListView的升级版,不仅可以轻松实现Listview同样的效果,还优化了ListView中的各种不足;
打开build.gradle
compile 'com.android.support:recyclerview-v7:25.3.1'
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:orientation="vertical"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
--------------------------------------------------------------------------------------------
recyelerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="10dp"
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
布局文件其实都跟我们的ListView的文件差不多,重要的是适配器,以及它的拓展性强,下面来看我们的Java代码
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{
private List<Fruit> mFruitList;
static class ViewHolder extends RecyclerView.ViewHolder {
View fruitView;
ImageView fruitImage;
TextView fruitName;
public ViewHolder(View view) {
super(view);
fruitView = view;
fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
fruitName = (TextView) view.findViewById(R.id.fruit_name);
}
}
public FruitAdapter(List<Fruit> fruitList){
mFruitList = fruitList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
final RecyclerView.ViewHolder holder = new ViewHolder(view);
return (ViewHolder) holder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final Fruit fruit = mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
holder.fruitView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
Fruit fruit1 = mFruitList.get(position);
Toast.makeText(v.getContext(),"you click view"+fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
holder.fruitImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
Fruit fruit1 = mFruitList.get(position);
Toast.makeText(v.getContext(),"you click view"+fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mFruitList.size();
}
}
你们可能会说这那里简单,比我们的ListView的代码多多了。但是仔细看,你会发现它的代码逻辑通俗易懂。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruit();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//设置RecyclerView的滑动方向
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter = new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruit() {
Fruit fruit1 = new Fruit("1",R.mipmap.ic_launcher);
Fruit fruit2 = new Fruit("2",R.mipmap.ic_launcher);
Fruit fruit3 = new Fruit("3",R.mipmap.ic_launcher);
Fruit fruit4 = new Fruit("4",R.mipmap.ic_launcher);
Fruit fruit5 = new Fruit("5",R.mipmap.ic_launcher);
Fruit fruit6 = new Fruit("6",R.mipmap.ic_launcher);
Fruit fruit7 = new Fruit("7",R.mipmap.ic_launcher);
Fruit fruit8 = new Fruit("9",R.mipmap.ic_launcher);
fruitList.add(fruit1);
fruitList.add(fruit2);
fruitList.add(fruit3);
fruitList.add(fruit4);
fruitList.add(fruit5);
fruitList.add(fruit6);
fruitList.add(fruit7);
fruitList.add(fruit8);
}
细心的你可能发现
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
跟我们的ListView的中的差别很大
这就是RecyclerView的神奇之处,它能根据我们的需要,来横向滑动或者纵向活动;
还可以设置成GridView的模式,是不是很神奇。
下次再具体讲讲,RecyclerView设置成Gridview
源码下载