2014-04-23 32 views
0

如何將下面的代碼轉換爲片段。我無法將其轉換爲片段而不是「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) { 
} 
+0

你的''PinnedSectionListActivity''onCreate'代碼在哪裏? –

+0

對不起,我現在已添加到原始問題?我需要幫助將onCreate方法轉換爲Fragment。 – Mihir

回答

0

片段不是活動的,但可以在一個FragmentActivity被包含。

構建一個Fragment,在FragmentActivity中實例化。 然後使用Fragment.onCreateView()方法膨脹佈局。

+0

對不起,但我是一個新的Android世界。我實際上需要幫助onCreateView。請看看原來的問題,我已經添加了我的嘗試onCreateView。我沒有得到任何列表作爲輸出。 – Mihir

0
public class PinnedSectionListActivity extends ListFragment implements OnClickListener 
{ 
    /*rest of the code*/ 
} 
+0

我如何通過擴展ListFragment來實現片段。我有點卡在onCreate方法。我該如何轉換? – Mihir

+0

ListFragment是一個片段,包含您需要編寫的ListView代碼。你應該使用onCreateView()而不是onCreate()。請參閱http://developer.android.com/guide/components/fragments.html以瞭解片段生命週期。如果您發現有用的答案,請接受。提前致謝。 – user3388324

+0

再次感謝您的快速響應,我試過但沒有得到任何輸出。這裏是我的代碼:\t @覆蓋 \t公共查看onCreateView(LayoutInflater吹氣,ViewGroup中容器, \t捆綁savedInstanceState){ \t \t \t視圖V = inflater.inflate(R.layout.activity_main,容器,FALSE); \t MyPinnedSectionListAdapter adapter = new MyPinnedSectionListAdapter(getActivity(),android.R.layout.simple_list_item_1,android.R.id.text1,prepareItems()); \t setListAdapter(adapter); \t \t \t \t return v; \t} private void setListAdapter(MyPinnedSectionListAdapter adapter){ \t} – Mihir