ListView usage and optimization measures

1. Basic use

Direct code:

list_view_item.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:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/text"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1"
        android:textSize="20sp"/>

</LinearLayout>

activity_list_view.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"
    tools:context="com.shuidi.listviewtest.ListViewActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</LinearLayout>

ListViewActivity.java

public class ListViewActivity extends AppCompatActivity {

    ListView mListView;
    ListAdapter mAdatper;
    List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        mListView = (ListView) findViewById(R.id.listview);
        list = getListData();
        mAdatper = new ListViewAdapter(this,list);
        mListView.setAdapter(mAdatper);
    }

    private List<String> getListData(){
        List<String> list = new ArrayList<>();
        list.add("One");
        list.add("Two");
        list.add("Three");
        list.add("Four");
        list.add("Five");
        list.add("Six");
        list.add("Seven");
        list.add("Eight");
        return list;
    }

    private class ListViewAdapter extends BaseAdapter {

        LayoutInflater inflater;
        List<String> list;


        public ListViewAdapter(Context context,List<String> stringList){
            inflater = LayoutInflater.from(context);
            list = stringList;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public int getCount() {
            return list==null? 0:list.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            View view;
            if (convertView==null) {
                view = inflater.inflate(R.layout.list_view_item,null);
                TextView textView = (TextView)view.findViewById(R.id.text);
                viewHolder = new ViewHolder();
                viewHolder.tx = textView;
                view.setTag(textView);
            } else {
                view = convertView;
                viewHolder = (ViewHolder)view.getTag();
            }
            viewHolder.tx.setText(list.get(position));
            return view;
        }
    }

    private static class ViewHolder{
        public TextView tx;
    }

}

2. Optimization measures

https://www.jianshu.com/p/728e031f1a74
https://www.zhihu.com/question/19703384
http://niorgai.github.io/2014/12/13/ListView%E4%B8%8EBaseAdapter%E4%BC%98%E5%8C%96/

3. Design mode

3.1. Adapter mode

ListView serves as the Client role, interface Adapter as the Target, BaseAdapter as the Adapter, and ListView's data object is Adapter.
The Adapter interface abstracts the interface getItem, getCount, getView required by ListView.
The Adapter interface implemented in the BaseAdapter subclass is actually an operation on data objects, that is, an Adapter dependency.

3.2. Observer mode

When the data of ListView changes, call the notifyDataSetChanged function of Adapter, which in turn calls the notifyChanged function of DataSetObservable, which will call the onChanged method of all observers (AdapterDataSetObserver). This is an observer mode!

Finally, we need to smooth it out. There is an internal class adapterdatasetobserver in AdapterView. When setting the adapter in listview, an adapterdatasetobserver will be built and registered in the adapter. This is an observer. The adapter contains a data set observable DataSetObservable. When the data quantity changes, the developer manually calls the notifyDataSetChanged function in the adapter. In fact, notifyDataSetChanged will call the notifyChanged function of DataSetObservable, which will go through the onchanged function of all observers. In AdapterDataSetObserver's onChanged function, you get a new number of datasets in Adapter, then call ListView's requestLayout() method to rearrange the layout and update the user interface.

4. Reference documents

ListView and design mode:

http://blog.csdn.net/smllys0000/article/details/51064543
http://blog.csdn.net/bboyfeiyu/article/details/44040533
http://blog.csdn.net/bboyfeiyu/article/details/43950185
https://github.com/simple-android-framework-exchange/android_design_patterns_analysis

Tags: Android xml encoding github

Posted on Sun, 03 May 2020 17:20:14 -0400 by bubbasheeko