ListView dynamically loads data templates (using xml layout)

Source: Internet
Author: User

Following the previous ListView dynamic loading of data templates (using code layout), I want to use the xml layout file to implement it, because the layout file must be standardized in the xml file, the principle is the same as that in the previous article.

Other_listview.xml,Note: The id method defined by ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

The layout file other_listview_item.xml of each Item in the ListView. You can replace the image used by the ImageView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/avira_antyvir"/>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:text="@string/hello"
/>
</LinearLayout>

The layout file other_listview_footer_more.xml is the bottom View of the ListView. When you click the Button, the progress bar is displayed.

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Button
Android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "more"
/>
<LinearLayout
Android: orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center"
Android: id = "@ + id/linearlayout">
<ProgressBar
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Retrieving..."/>
</LinearLayout>
</LinearLayout>

Class file OtherListView. java

package com.focus.loading;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class OtherListView extends ListActivity implements OnScrollListener {

ListView list;
int scrollState;
int count = 40;
int lastItem;
int visibleItemCount;

Button footerButton;
LinearLayout footerProgressBarLayout;
View view;
ListAdapter listAdapter = new ListAdapter();

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

LayoutInflater inflater = LayoutInflater.from(this);
view = inflater.inflate(R.layout.other_listview_footer_more, null);
footerButton = (Button) view.findViewById(R.id.button);
footerProgressBarLayout = (LinearLayout) view
.findViewById(R.id.linearlayout);
footerProgressBarLayout.setVisibility(View.GONE);
footerButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (lastItem == listAdapter.count
&& scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
footerButton.setVisibility(View.GONE);
footerProgressBarLayout.setVisibility(View.VISIBLE);
if (listAdapter.count <= count) {
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
listAdapter.count += 10;
listAdapter.notifyDataSetChanged();
// list.setSelection(lastItem);
footerButton.setVisibility(View.VISIBLE);
footerProgressBarLayout
.setVisibility(View.GONE);
}
}, 2000);
}

}
}
});

list = getListView();
list.addFooterView(view);
list.setAdapter(listAdapter);
list.setOnScrollListener(this);
}

class ListAdapter extends BaseAdapter {

int count = 10;

@Override
public int getCount() {
return count;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(OtherListView.this);
View view = inflater.inflate(R.layout.other_listview_item, null);
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText("Hello World " + position);
return view;
}

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.visibleItemCount = visibleItemCount;
lastItem = firstVisibleItem + visibleItemCount - 1;
System.out.println(listAdapter.count);
if (listAdapter.count >= count) {
list.removeFooterView(view);
}
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.scrollState = scrollState;

}
}

Because of the same principle as before, so did not write comments, do not understand first look at the previous article http://www.cnblogs.com/and_he/archive/2011/05/30/2063230.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.