ListView在开发的过程中,算是常客了。
ListView用于向用户展现滚动数据,以及Item的点击操作。使用ListView,基本上包括:数据,Adapter,Item布局三个点。
adapter:
- ArrayAdapter:简单易用的Adapter,通常用于数组或list集合的数据源。
- simpleAdapter:有很好的扩充性,可以自定义出各种效果。
- simpleCursorAdapter:主要用于从数据库获取数据,。
- BaseAdapter:抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性。
优化一:convertView是否为null的判断,优化布局。
View view; if(convertView == null){ view = LayoutInfalter.from(getContext()).inflate(resourceID,null) } else{ view = convertView }
当convertview不为空时,加载Listview缓存的布局。
优化二:ViewHolder :不难发现的是,当布局加载完成后,每次都会重现加载item布局控件,因此创建一个ViewHolder内部类,并将控件实例保存在该内部类中。第一次加载时,调用view的setTag方法,将ViewHodler对象存储在view中。再次加载时,调用view的getTag方法,取出ViewHolder对象。
//getView核心代码 ViewHolder viewHolder; if(convertView == null){ viewHolder = new ViewHolder(); viewHolder.fruitImage = (ImageView) view.findViewByID(R.id.fruit_image); view.setTage(viewHolder);//讲ViewHolder存储在View中 }else{ view = convertView; viewHolder = ViewHolder view.getTag();//重获取viewHolder } viewHolder.fruitImage.setImageResource(fruit.getIMageID); //内部类 class ViewHolder{ ImageView fruitImage; }
通过上面的优化,ListView在运行过程中效果会有很大的提升。
其实,ListView用到了RecycleBin机制:
- listview的许多view呈现在Ui上,这样的View对我们来说是可见的,可以称为OnScreen的view(也为ActionView)。
- view被上滚移除屏幕,这样的view称为offScreenView(也称为ScrapView)。
- 然后ScrapView会被listview删除,而RecycleView会将这部分保存。
- 而listview底部需要显示的view会从RecycleBin里面取出一个ScrapView。
RecycleBin用于存储从屏幕滑出去的item,当该item需要进入屏幕时,listview就会从recycleBin里取出。
下面通过一个聊天界面介绍RecyclerView的基本使用。
1> 导入RecyclerView库
在build.gradle中添加“compile ‘com.android.support:recyclerview-v7:25.2.0‘”
2> 创建主界面xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.test.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="Hello World!" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/edit_text" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="Edit something here" android:layout_weight="1"/> <Button android:id="@+id/button_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send"/> </LinearLayout> </LinearLayout>
3> 创建msg_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFF" android:orientation="vertical" tools:context="com.example.test.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="Hello World!" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/edit_text" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="Edit something here" android:layout_weight="1"/> <Button android:id="@+id/button_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send"/> </LinearLayout> </LinearLayout>
4> 消息类
public class Msg { public static final int TYPE_SENT = 1; public static final int TYPE_REVICE = 0; public String content; public int type; public Msg(int type ,String content) { this.content = content; this.type = type; } public void setContent(String content) { this.content = content; } public void setType(int type) { this.type = type; } public String getContent() { return content; } public int getType() { return type; } }
5> adapter
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder>{ public List<Msg> mMsgList; public MsgAdapter(List<Msg> msglist){ this.mMsgList = msglist; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item ,parent,false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { Msg msg = mMsgList.get(position); if (msg.getType() == Msg.TYPE_REVICE){ holder.leftlayout.setVisibility(View.VISIBLE); holder.rightlayout.setVisibility(View.GONE); holder.lefttext.setText(msg.getContent()); }else if (msg.getType() == Msg.TYPE_SENT){ holder.leftlayout.setVisibility(View.GONE); holder.rightlayout.setVisibility(View.VISIBLE); holder.righttext.setText(msg.getContent()); } } @Override public int getItemCount() { return mMsgList.size(); } static class ViewHolder extends RecyclerView.ViewHolder { LinearLayout leftlayout; LinearLayout rightlayout; TextView lefttext; TextView righttext; public ViewHolder(View itemView) { super(itemView); leftlayout = (LinearLayout) itemView.findViewById(R.id.left_layout); rightlayout = (LinearLayout) itemView.findViewById(R.id.right_layout); lefttext = (TextView) itemView.findViewById(R.id.left_msg); righttext = (TextView) itemView.findViewById(R.id.right_msg); } } }
6> MainActivity
public class MainActivity extends AppCompatActivity { public List<Msg> msglist = new ArrayList<>(); private EditText mEditText; private Button Send; private RecyclerView mRecyclerView; private MsgAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); LinearLayoutManager layoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(layoutManager); adapter = new MsgAdapter(msglist); Log.d("azheng",adapter.mMsgList.get(0).getContent()); mRecyclerView.setAdapter(adapter); } private void init() { mEditText = (EditText) findViewById(R.id.edit_text); Send = (Button) findViewById(R.id.button_send); mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview); Msg msg1 = new Msg(0, "hello"); msglist.add(msg1); Msg msg2 = new Msg(1, "hi"); msglist.add(msg2); Msg msg3 = new Msg(0, "Nice to meet you!"); msglist.add(msg3); } }
由于界面太丑,就不贴图了。
时间: 2024-10-05 09:07:50