我有一个应该在平板电脑和手机上运行的应用。对于平板电脑布局,我创建了这个布局,因此它启动了两个片段:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/search_panel_fragment"
android:name="adam.czibere.fragments.tablet.SearchPanelFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:tag="search_panel" />
<fragment
android:id="@+id/search_list_fragment"
android:name="adam.czibere.fragments.tablet.SearchListFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:tag="search_list" />
</LinearLayout>
到目前为止,这些片段显示了他们的观点。这两个片段的主机活动是SearchActivity。
public class SearchListFragment extends Fragment {
private SearchEventListener seListener;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
seListener = (SearchEventListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement SearchListFragmentListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.kurva_lista, container, false);
SearchActivity sa = (SearchActivity) getActivity();
ListView list = (ListView) v.findViewById(R.id.listView1);
list.setAdapter(sa.mobileAdapter);
list.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
seListener.onListScrolled(view, firstVisibleItem,
visibleItemCount, totalItemCount);
}
});
return v;
}
}
当我单击SearchPanelFragment上的搜索按钮时,它会为适配器创建一个列表并调用notifyDataSetChanged(),但屏幕上看不到任何内容。我检查了,适配器,它有一些值(getgropuCount不返回0)。我的适配器的getView方法根本没有被调用。奇怪的是,如果我在手机上试用它,它可以工作,它会填充列表。 (我在手机上使用另一个片段,但它也是一样的。在手机模式下,屏幕上只能看到一个片段)
这是适配器:
public class MobileSearchListAdapter extends BaseAdapter{
private List<VCDR> items;
VCDR lastElementOnScreen;
private Context context;
public MobileSearchListAdapter( Context c,List<VCDR> objects) {
items=objects;
context=c;
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.search_list_item_viewpager, null);
}
VCDR currentItem=(VCDR) getItem(position);
lastElementOnScreen=currentItem;
TextView date= (TextView) convertView.findViewById(R.id.date);
TextView dur = (TextView) convertView.findViewById(R.id.duration);
TextView caller_name = (TextView) convertView.findViewById(R.id.caller_name);
TextView caller_num = (TextView) convertView.findViewById(R.id.caller_number);
TextView called_num = (TextView) convertView.findViewById(R.id.called_number);
TextView called_name = (TextView) convertView.findViewById(R.id.caller_name);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
date.setText(dateFormat.format(currentItem.getStartTime()) + " " +
timeFormat.format(currentItem.getStartTime()));
dur.setText(timeFormat.format(currentItem.getDuration()));
called_name.setText(currentItem.getDestinationNameD());
caller_name.setText(currentItem.getSourceName());
called_num.setText(currentItem.getDestinationCallerID());
called_num.setText(currentItem.getSourceCallerID());
return convertView;
}
public VCDR getLastVisibleElemnt(){
return lastElementOnScreen;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return items.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}