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+"");
}
}
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) {
}
});
}
}