Android - 如何在Fragment中添加ListView
Android - How to add ListView in Fragment
我会在 Fragment 中添加 ListView。
这是我的 activitymain.xml:
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.google.android.material.tabs.TabItem
android:id="@+id/tabAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="All"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/tabTravel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Travel"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/tabTech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tech"/>
</com.google.android.material.tabs.TabLayout>
我在 list_item.xml 中开发了自定义布局。
我的Fragment.javaclass:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){// Inflate the layout for this fragment
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_all, container, false); }
当我尝试添加我的 adapter.java class 时,我收到此错误:
“无法解析片段中的方法 findviewbyid”
通过检查你的屏幕截图,你犯了几个错误
更正它们。
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_all, container, false);
// whatever you have written after this, move to the below method.
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
// blah
ArrayList<CouponCategory> couponcategory = whatever you have written
//
ListView couponcategoryListView = view.findViewById(R.id.your_list_id);
// whatever you have written....\
// adapter initialization
// settinh up blah...
}
我会在 Fragment 中添加 ListView。
这是我的 activitymain.xml:
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.google.android.material.tabs.TabItem
android:id="@+id/tabAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="All"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/tabTravel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Travel"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/tabTech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tech"/>
</com.google.android.material.tabs.TabLayout>
我在 list_item.xml 中开发了自定义布局。
我的Fragment.javaclass:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){// Inflate the layout for this fragment
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_all, container, false); }
当我尝试添加我的 adapter.java class 时,我收到此错误: “无法解析片段中的方法 findviewbyid”
通过检查你的屏幕截图,你犯了几个错误 更正它们。
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_all, container, false);
// whatever you have written after this, move to the below method.
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
// blah
ArrayList<CouponCategory> couponcategory = whatever you have written
//
ListView couponcategoryListView = view.findViewById(R.id.your_list_id);
// whatever you have written....\
// adapter initialization
// settinh up blah...
}