RecyclerView添加头尾布局/xml解析

RecyclerView添加头尾布局/xml解析

    • pull解析
    • Sax解析

SAX解析
对文档进行顺序扫描,当扫描到4中情况(开始文档,结束文档,开始标签,结束标签)的时候回通知DefaultHandler中的方法,一直重复这个方法直到结束
优点:解析速度快,占用内存少
缺点:解析一个xml文件对应一个handler类,比较麻烦
特点:流式解析,解析是同步的督导哪里就是解析到哪里
用法
自定义类继承DefaultHandler 重写方法 startElement endElement characters
获得工厂 SAXParserFactory factory = SAXParserFactory.newInstance();
获得解析器SAXParser parser = factory.newSAXParser();
设置解析文件和解析方式parser.parse(文件,handler对象);
pull解析
解析方式类似sax当元素开始调用parse.nextText()从文档中取值

pull解析

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    List<Book> list = new ArrayList<>();
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = factory.newPullParser();

        InputStream open = getAssets().open("book.xml");
        parser.setInput(open,"utf-8");

        int type = parser.getEventType();
        Book book = null;
        while (type!=XmlPullParser.END_DOCUMENT){
            switch (type){
                case XmlPullParser.START_TAG:
                    if ("book".equals(parser.getName())){
                        book = new Book();
                        String attributeValue = parser.getAttributeValue(0);
                        book.setId(attributeValue);
                    } else if ("name".equals(parser.getName())) {
                        book.setName(parser.nextText());
                    }else if ("author".equals(parser.getName())){
                        book.setAuthor(parser.nextText());
                    }else if ("year".equals(parser.getName())){
                        book.setYear(parser.nextText());
                    }else if ("price".equals(parser.getName())){
                        book.setPrice(parser.nextText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("book".equals(parser.getName())){
                        list.add(book);
                    }
                    break;
            }
            type=parser.next();
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.e("####",list+"");
}
}

Sax解析

public class Main2Activity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    SAXParserFactory factory = SAXParserFactory.newInstance();
    try {
        SAXParser parser = factory.newSAXParser();
        InputStream open = getAssets().open("book.xml");
        MyHandler myHandler = new MyHandler();
        parser.parse(open,myHandler);

        List<Book> list = myHandler.getList();
        Log.e("####sax",list+"");


    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
class MyHandler extends DefaultHandler{
    List<Book> list = new ArrayList<>();
    String tagname;
    Book book;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        if (qName.equals("book")){
            book= new Book();
            String value = attributes.getValue(0);
            book.setId(value);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
         if (qName.equals("name")){
            book.setName(tagname);
        }else if (qName.equals("author")){
            book.setAuthor(tagname);
        }else if (qName.equals("year")){
            book.setYear(tagname);
        } else if (qName.equals("price")) {
            book.setPrice(tagname);
        }else if (qName.equals("book")){
            list.add(book);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        tagname=new String(ch,start,length);
    }

    public List<Book> getList() {
        return list;
    }
}

}

我们可以通过XRecylerView来添加头尾布局代码如下


public class MainActivity extends AppCompatActivity {
    XRecyclerView recyclerView;
    MyAdapter myAdapter;

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=findViewById(R.id.recycler);


        initData();

    }
    @RequiresApi(api = Build.VERSION_CODES.P)
    public void initData(){
        recyclerView=findViewById(R.id.recycler);

        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        ArrayList<String> strings=new ArrayList<>();
        for(int i = 0 ; i<=50;i++){
            strings.add("第"+i+"个:王鹏");

        }
        myAdapter=new MyAdapter(strings,this);
        recyclerView.setAdapter(myAdapter);
        View view= LayoutInflater.from(this).inflate(R.layout.head,null,false);
        View view1= LayoutInflater.from(this).inflate(R.layout.foot,null,false);
        recyclerView.addHeaderView(view);
        recyclerView.setFootView(view1, new CustomFooterViewCallBack() {
            @Override
            public void onLoadingMore(View yourFooterView) {

            }

            @Override
            public void onLoadMoreComplete(View yourFooterView) {

            }

            @Override
            public void onSetNoMore(View yourFooterView, boolean noMore) {

            }
        });

    }


}

你可能感兴趣的:(Android)