ListView的基本使用方法和RecyclerView的基本使用方法
ListView是一种用于列表显示数据内容的控件,它可以通过适配器实现对于数据的列表显示,而RecyclerView是对于ListView优化后的列表数据显示控件。
个人对于List的使用经历多半在新浪微博集成的时候使用,故这里仅仅是记载RecyclerView的使用方法,
而对于ListView的方法可在aliCamera项目中,要想查找可直接查找自己以前写过的源码!
RecyclerView的使用方法:
1.在要使用RecyclerView的项目中,打开open module setting 选择APP的Dependencies选项,选择添加Library Dependency选项,添加RecyclerView的包
2.在对应的Activity中添加对应的代码
具体的代码实现:
package andrew.com.custompaintdemo; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.ViewGroup; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener { private RecyclerView rv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); rv = new RecyclerView(this); //新建一个RecyclerView对象 rv.setLayoutManager(new LinearLayoutManager(this)); //设置这个RecyclerView的布局为线性布局 setContentView(rv); //将RecyclerView作为界面 rv.setAdapter(new RecyclerView.Adapter() { //设置适配器 class ViewHolder extends RecyclerView.ViewHolder{ //这是一个用于设计RecyclerView的界面元素的类 private TextView tv; public ViewHolder(TextView itemView) { super(itemView); tv = itemView; } public TextView getTv() { return tv; } } //以下部分与ListView类似,简单易懂 @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(new TextView(getApplicationContext())); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ViewHolder h = (ViewHolder) holder; h.getTv().setTextColor(getResources().getColor(R.color.colorAccent)); h.getTv().setText("item"+position); } @Override public int getItemCount() { return 100; } }); } }
使用XML资源文件的配置方法:
1.先设置自定义adapter,继承RecylerView.adapter,绑定XML file,设置适配方法
具体代码:
package andrew.com.custompaintdemo; import android.app.Activity; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Created by Andrew on 2016/7/26. */ class MyAdapter extends RecyclerView.Adapter { private Activity mainActivity; public MyAdapter(Activity mainActivity) { this.mainActivity = mainActivity; } class ViewHolder extends RecyclerView.ViewHolder{ private View root; private TextView tv1,tv2; public ViewHolder(View view) { super(view); root = view;
//绑定布局控件的方法 tv1 = (TextView) root.findViewById(R.id.tv1); tv2 = (TextView) root.findViewById(R.id.tv2); } public TextView getTv1() { return tv1; } public TextView getTv2() { return tv2; } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//精华只在这一句!精华只在这一句!精华只在这一句! return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list,null)); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ViewHolder h = (ViewHolder) holder; h.getTv1().setTextColor(mainActivity.getResources().getColor(R.color.colorAccent)); h.getTv1().setText("item" + position); h.getTv2().setTextColor(mainActivity.getResources().getColor(R.color.colorAccent)); h.getTv2().setText("content"); } @Override public int getItemCount() { return 100; } }
XML的配置情况:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv2"/> </LinearLayout>
在Activity中可以通过一些配置来改变界面的显示状况:
rv.setLayoutManager(new GridLayoutManager(this,4)); //表格布局形式
rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,TRUE)) //水平布局形式