下面的工作和测试代码:
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class SpecialList extends ListActivity{
TextView toptext;
String[] items={"Atlanta", "Boston", "Chicago", "Dallas"};
private CityListAdapter mListOfCities;
boolean mAtlantaListExpanded;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mAtlantaListExpanded = false;
mListOfCities = new CityListAdapter();
setListAdapter(mListOfCities);
for (int n=0; n < items.length; n++)
{
mListOfCities.add(items[n]);
}
}
public void onListItemClick(ListView parent, View v, int position, long id)
{
Intent intent = new Intent();
if (parent.getItemAtPosition(position)=="Atlanta")
{
mAtlantaListExpanded = !mAtlantaListExpanded;
mListOfCities.clear();
for (int n=0; n < items.length; n++)
{
mListOfCities.add(items[n]);
}
Log.i("SpecialList", "Atlanta");
}
else if (parent.getItemAtPosition(position)=="Boston")
{
Log.i("SpecialList", "Boston");
}
else if (parent.getItemAtPosition(position)=="Chicago")
{
Log.i("SpecialList", "Chicago");
}
else if (parent.getItemAtPosition(position)=="Dallas")
{
Log.i("SpecialList", "Dallas");
}
}
class CityListAdapter extends ArrayAdapter<String> {
CityListAdapter() {
super(SpecialList.this, R.layout.one_city_row);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = null;
LayoutInflater inflater = getLayoutInflater();
if ((position == 0) && (!mAtlantaListExpanded))
{
row = inflater.inflate(R.layout.expandable_city_row, parent, false);
TextView city = (TextView) row.findViewById(R.id.Expandable_TextView01);
city.setText(items[position]);
TextView cityZone1 = (TextView) row.findViewById(R.id.Zone_TextView01);
cityZone1.setText("Zone 1");
TextView cityZone2 = (TextView) row.findViewById(R.id.Zone_TextView02);
cityZone2.setText("Zone 2");
TextView cityZone3 = (TextView) row.findViewById(R.id.Zone_TextView03);
cityZone3.setText("Zone 3");
cityZone1.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("SpecialList", "Zone 1");
}
}
);
cityZone2.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("SpecialList", "Zone 2");
}
}
);
cityZone3.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("SpecialList", "Zone 3");
}
}
);
}
else
{
row = inflater.inflate(R.layout.one_city_row, parent, false);
TextView city = (TextView) row.findViewById(R.id.City_TextView01);
city.setText(items[position]);
}
return(row);
}
}
}
单击“Atlanta”会重新绘制列表,在仅显示“Atlanta”或扩展列表之间切换。R.layout.one_city_row.xml
是一个 TextView 的简单布局,R.layout.Expandable_city_row.xml
有额外的子选项作为 TextView 元素,可以从附加的代码片段中得出结论。
使用这种方法,您可以根据您的需要完全自定义列表。在我放置的地方Log.i()
,你应该表达你的意图。