在我的主要活动中,我有一个带有导航选项卡和操作项的 ActionBar。我的一个操作项调用了一个 ListFragment,它显示了一个带有图像和文本视图的自定义列表视图。我正在使用 ActionBarSherlock 来实现兼容性。这是我调用新 ListFragment 的代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, ERGProActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.menuitem_search:
onSearchRequested();
return true;
case R.id.menuitem_info:
// Create new fragment and transaction
SherlockListFragment aboutListFragment = new AboutListFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
ft.replace(R.id.root, aboutListFragment);
ft.addToBackStack(null);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
// Commit the transaction
ft.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
这是我的 ListFragment 代码:
public class AboutListFragment extends SherlockListFragment {
private ListView listView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().setContentView(R.layout.aboutlist);
CustomMenu menu_data[] = new CustomMenu[]
{ new CustomMenu(R.drawable.info, "About"),
new CustomMenu(R.drawable.legal, "Disclaimer"),};
CustomMenuAdapter adapter = new CustomMenuAdapter(getActivity(),
R.layout.listview_item_row, menu_data);
listView = (ListView) getActivity().findViewById(R.id.listView1);
listView.setAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0:
// about
Log.i("ABOUT"," - About Selected");
case 1:
Log.i("ABOUT"," - Disclaimer Selected");
// disclaimer
}
}
}
这是我的 CustomAdapter 代码:
public class CustomMenuAdapter extends ArrayAdapter<CustomMenu> {
Context context;
int layoutResourceId;
CustomMenu data[] = null;
public CustomMenuAdapter(Context context, int layoutResourceId, CustomMenu[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CustomMenuHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CustomMenuHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (CustomMenuHolder)row.getTag();
}
CustomMenu menu = data[position];
holder.txtTitle.setText(menu.title);
holder.imgIcon.setImageResource(menu.icon);
return row;
}
static class CustomMenuHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
任何意见或建议将不胜感激!先谢谢了。
谢谢各位的意见!这是最终的工作代码:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().setContentView(R.layout.aboutlist);
CustomMenu menu_data[] = new CustomMenu[]
{ new CustomMenu(R.drawable.info, "About"),
new CustomMenu(R.drawable.legal, "Disclaimer"),};
CustomMenuAdapter adapter = new CustomMenuAdapter(getActivity(),
R.layout.listview_item_row, menu_data);
listView = (ListView) getActivity().findViewById(R.id.listView1);
// listView.setOnItemSelectedListener(this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
Log.i("ABOUT"," - About Selected");
break;
case 1:
Log.i("ABOUT"," - Disclaimer Selected");
break;
default:
// do nothing
Log.i("ABOUT"," - default");
break;
}
}
});
}