If you think the Android project RZAndroidBaseUtils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.raizlabs.baseutils.examples.viewgroupadapter;
/*fromwww.java2s.com*/import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import com.raizlabs.baseutils.examples.R;
import com.raizlabs.widget.adapters.ViewGroupAdapter;
import com.raizlabs.widget.adapters.ViewGroupAdapter.ItemClickedListener;
/**
* Simple example of a {@link ViewGroupAdapter} that displays incrementing
* strings.
* @author Dylan James
*/publicclass ViewGroupAdapterExampleActivity extends Activity {
privateint currentCount = 0;
private ViewGroupAdapter<String> adapter;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewgroupadapter);
// Grab the view to bind the adapter to
ViewGroup adapterGroup = (ViewGroup) findViewById(R.id.viewGroupAdapter_adapterLayout);
// Create and bind an adapter which displays views representing strings
adapter = new ViewGroupAdapter<String>(adapterGroup) {
@Override
protected View createView(String item, LayoutInflater inflater, ViewGroup root) {
// Inflate a new view
TextView text = (TextView) inflater.inflate(
R.layout.simple_text_item_layout, root, false);
// Simply set the text in a label
text.setText(item);
return text;
}
};
// When an item is clicked in the adapter, remove it.
adapter.setItemClickedListener(new ItemClickedListener<String>() {
@Override
publicvoid onItemClicked(ViewGroupAdapter<String> adapter,
String item, int index) {
// Use removeAt to guarantee we remove the right index
// adapter.remove will remove the first occurrence of the object
adapter.removeAt(index);
}
});
// Handle the add 1 button
findViewById(R.id.add1Button).setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
adapter.add(getNextString());
}
});
// Handle the add 5 button
findViewById(R.id.add5Button).setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
List<String> strings = new LinkedList<String>();
for (int i = 0; i < 5; ++i) {
strings.add(getNextString());
}
adapter.add(strings);
}
});
// Handle the clear button
findViewById(R.id.clearButton).setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
adapter.clear();
}
});
}
/**
* @return The next string to display
*/private String getNextString() {
return Integer.toString(++currentCount);
}
}