我开发了一个“国家管理”应用程序。我在我的应用程序上使用ListView。我的Listview已准备就绪,可以正常工作。我想在我的应用程序中添加“国家/地区”部分标题。拜托,有人帮我解决这个问题。 这是我的代码。
MainActivity.java
public class MainActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<PageData> data = new ArrayList<PageData>();
// Africa (Here should add country Header)
data.add(new PageData(R.drawable.ic_launcher, "Algeria"));
data.add(new PageData(R.drawable.ic_launcher, "Egypt"));
data.add(new PageData(R.drawable.ic_launcher, "Ethiopia"));
data.add(new PageData(R.drawable.ic_launcher, "Ghana"));
// Asia (Here should add country Header)
data.add(new PageData(R.drawable.ic_launcher, "Bangladesh"));
data.add(new PageData(R.drawable.ic_launcher, "India"));
data.add(new PageData(R.drawable.ic_launcher, "Pakistan"));
data.add(new PageData(R.drawable.ic_launcher, "Japan"));
// Europe (Here should add country Header)
data.add(new PageData(R.drawable.ic_launcher, "Belgium"));
data.add(new PageData(R.drawable.ic_launcher, "Finland"));
data.add(new PageData(R.drawable.ic_launcher, "Italy"));
data.add(new PageData(R.drawable.ic_launcher, "Poland"));
// America (Here should add country Header)
data.add(new PageData(R.drawable.ic_launcher, "Canada "));
data.add(new PageData(R.drawable.ic_launcher, "USA"));
data.add(new PageData(R.drawable.ic_launcher, "Mexico"));
data.add(new PageData(R.drawable.ic_launcher, "Bermuda"));
// Australia (Here should add country Header)
data.add(new PageData(R.drawable.ic_launcher, "Australia"));
data.add(new PageData(R.drawable.ic_launcher, "Fiji"));
data.add(new PageData(R.drawable.ic_launcher, "New Zealand"));
data.add(new PageData(R.drawable.ic_launcher, "Papua New Guinea"));
final ListView lv = (ListView) findViewById(R.id.list_view);
final ListViewAdapter lva = new ListViewAdapter(this, R.layout.listview_item, data);
lv.setAdapter(lva);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
PageData pd = data.get(position);
Toast.makeText(MainActivity.this.getBaseContext(), pd.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical" />
</LinearLayout>
listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_view"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="0dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/item_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp" />
</LinearLayout>
ListViewAdapter.java
public class ListViewAdapter extends ArrayAdapter<PageData> {
protected List<PageData> data;
protected LayoutInflater inflater;
public ListViewAdapter(Activity activity, int textViewResourceId, List<PageData> objects){
super(activity, textViewResourceId, objects);
data= objects;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.listview_item, parent, false);
PageData pd = data.get(position);
ImageView iv = (ImageView) row.findViewById(R.id.image_view);
iv.setImageResource(pd.getPicture());
TextView tv = (TextView)row.findViewById(R.id.item_label);
tv.setText(Html.fromHtml(pd.getTitle()));
return row;
}
}
PageData.java
public class PageData implements Parcelable {
private int picture;
private String title;
public PageData() {
}
public PageData(Parcel in) {
picture = in.readInt();
title = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(picture);
dest.writeString(title);
}
public PageData(int picture, String title) {
this.picture = picture;
this.title = title;
}
public int getPicture() {
return picture;
}
public String getTitle() {
return title;
}
}