如何將下面的代碼轉換爲片段。我無法將其轉換爲片段而不是「ListActivity」。我有刷卡選項卡,我想在下面的代碼中的其中一個選項卡上,但唯一的問題是它必須分割。從ListActivity到片段
public class PinnedSectionListActivity extends ListActivity implements OnClickListener {
private static final int[] COLORS = new int[] {
R.color.green_light, R.color.orange_light,
R.color.blue_light, R.color.red_light };
private class MyPinnedSectionListAdapter extends ArrayAdapter<Item> implements PinnedSectionListAdapter {
public MyPinnedSectionListAdapter(Context context, int resource, int textViewResourceId, List<Item> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTextColor(Color.DKGRAY);
view.setTag("" + position);
if (getItem(position).type == Item.SECTION) {
//view.setOnClickListener(PinnedSectionListActivity.this);
view.setBackgroundColor(parent.getResources().getColor(COLORS[position % COLORS.length]));
}
return view;
}
@Override public int getViewTypeCount() {
return 2;
}
@Override public int getItemViewType(int position) {
return getItem(position).type;
}
@Override public boolean isItemViewTypePinned(int viewType) {
return viewType == Item.SECTION;
}
}
private static class Item {
public static final int ITEM = 0;
public static final int SECTION = 1;
public final int type;
public final String text;
public Item(int type, String text) {
this.type = type;
this.text = text;
}
@Override public String toString() {
return text;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPinnedSectionListAdapter adapter = new MyPinnedSectionListAdapter(
this, android.R.layout.simple_list_item_1, android.R.id.text1, prepareItems());
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this, "Item: " + position, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private static ArrayList<Item> prepareItems() {
ArrayList<Item> result = new ArrayList<Item>();
for (int i = 0; i < 30; i++) {
result.add(new Item(Item.SECTION, "Section " + i));
for (int j=0; j<4; j++) {
result.add(new Item(Item.ITEM, "Item " + j));
}
}
return result;
}
@Override
public void onClick(View v) {
Toast.makeText(this, "Item: " + v.getTag(), Toast.LENGTH_SHORT).show();
}
}
MY試穿onCreateView方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_main, container, false); MyPinnedSectionListAdapter adapter = new MyPinnedSectionListAdapter(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, prepareItems()); setListAdapter(adapter);
return v;
}
private void setListAdapter(MyPinnedSectionListAdapter adapter) {
}
你的''PinnedSectionListActivity''onCreate'代碼在哪裏? –
對不起,我現在已添加到原始問題?我需要幫助將onCreate方法轉換爲Fragment。 – Mihir