我正在尝试使用 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) {
}
}
您可以看到我尝试使用片段管理器将片段添加到活动中,但我也尝试通过 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文档并查看所有其他教程让我现在完全困惑。在我知道我已经接近之前开始玩片段,所以我不想把十个教程放在一堆乱七八糟的代码中,只是为了让一个科学怪人片段继续下去。