我使用SimpleCursorAdapter的子类作为适配器,在支持lib中扩展了ListFragment。所有数据加载部分都可以(通过调试读取),列表只是没有显示,我在Activity上有一个空白页面。这是我的代码:
import android.support.v4.app.FragmentActivity;
public class HostActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_with_fragment);
}
}
布局文件(activity_view_with_fragment.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/lsStopTimetableFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_weight="1"
class="com.package.Fragment.MyFragment">
</fragment>
</LinearLayout>
这是ListFragment代码:
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
public class MyFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
String [] from = new String[] {"col_1", "col_2"};
int [] to = new int [] {R.id.view1, R.id.view2};
//..put value in args
Loader l = getLoaderManager().restartLoader(0, args, this);
if(l != null)
l.forceLoad();
adapter = new CustomCursorAdapter(this.getActivity()
, R.layout.row_entry, null, from,
to, 0);
setListAdapter(adapter);
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
adapter.swapCursor(cursor);
}
public void onLoaderReset(Loader<Cursor> cursorLoader) {
adapter.swapCursor(null);
}
}
在CustomCursorAdapter中,我实现了
getView(int pos,View convertView,ViewGroup parent)
方法。有提示吗?提前谢谢。
答案 0 :(得分:1)
您是否尝试将片段项中的layout_width和layout_height参数从0dp更改为match_parent?如果您尝试仅使用填充整个活动的列表片段进行活动,则无需创建如下布局:
<LinearLayout>
<fragment/>
</LinearLayout>
您可以直接使用片段标记以root身份创建布局,并且只能标记:
<fragment/>