我想使用ActionBarSherlock來顯示不同的listfragments取決於當前正在查看哪個選項卡。到目前爲止,儘管我甚至無法過去嘗試顯示我的片段。我可以看到它正在實例化並創建,但它在視圖中根本不顯示。使用ActionBarSherlock無法看到我的片段
這裏的活動,我有一個實現SherlockFragmentActivity:
public class MyListView extends SherlockFragmentActivity implements ActionBar.TabListener{
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(com.actionbarsherlock.R.style.Theme_Sherlock); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_navigation);
MyListFrag listFrag = (MyListFrag) getSupportFragmentManager()
.findFragmentById(R.id.fragment1);
listFrag.update(ViewActivity.livePoints);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment1, listFrag);
fragmentTransaction.commit();
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 1; i <= 3; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText("Tab " + i);
tab.setTabListener(this);
getSupportActionBar().addTab(tab);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
}
}
你可以看到我試着用fragmentmanager的片段添加到活動,但我也想通過XML進行添加這也不適合我。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dip">
<fragment
android:name="core.MyListFrag"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent"
/>
</LinearLayout>
最後,這裏是我正在使用的listfragment類。我想要做的是使用我通過擴展baseadapter創建的自定義適配器來填充列表,但我不確定是否正確執行了這可能是問題的一部分。
public class MyListFrag extends SherlockListFragment{
MarkItemAdapter mia;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mia = new MarkItemAdapter(this.getActivity(), MyViewActivity.livePoints);
setListAdapter(mia);
Toast.makeText(getSherlockActivity(), "frag started"+MyViewActivity.livePoints.size(), Toast.LENGTH_LONG).show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_frag_layout, container, false);
}
public void updateUrl(List<Points> newList) {
if(mia!=null){
mia.points.clear();
mia.points = newList;
mia.notifyDataSetChanged();
}else{
mia.points = newList;
mia.notifyDataSetChanged();
}
}
class MarkItemAdapter extends BaseAdapter{
List<Points> points = null;
Context cntx = null;
LayoutInflater myInflater;
int itemPosition =0;
ViewHolder holder;
public MarkItemAdapter(Context cntx,List<Points> points){
this.points = points;
this.cntx = cntx;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return marks.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
itemPosition = position;
if (convertView==null) {
convertView = myInflater.inflate(R.layout.point_row_item, null);
holder = new ViewHolder();
holder.description = (TextView) convertView.findViewById(R.id.point_description);
holder.distance = (TextView) convertView.findViewById(R.id.point_distance);
holder.point_image = (ImageView) convertView.findViewById(R.id.point_image);
holder.view_point = (ImageButton) convertView.findViewById(R.id.view_mark);
holder.rate_point = (RatingBar) convertView.findViewById(R.id.rate_point);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.description.setText(points.get(itemPosition).title);
holder.distance.setText(points.get(itemPosition).getDistanceString());
holder.view_point.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
//do stuff
}
});
//add logic for rating bar
return convertView;
}
}
private class MyViewHolder{
ImageView mark_image;
TextView description;
TextView distance;
ImageButton view_mark;
RatingBar rate_mark;
}
}
我可以添加其他的XML佈局,是否可以幫助任何人來幫助我,但我離開了出來,因爲我敢肯定,這是我的Java這就是問題所在,而不是佈局。首次使用碎片時,碎片絕對不是非常簡單。我不確定是否必須使用onAttach()
或其他內容,並查看Android文檔,並看到所有其他教程目前讓我完全困惑。在我知道我離得很近之前,從碎片中玩弄碎片,所以我不想把十個教程放在一堆代碼中,只是爲了得到一個科學怪人片段。