Android – 如何在Fragment中添加ListView

huangapple go评论101阅读模式
英文:

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="全部"/>
    <com.google.android.material.tabs.TabItem
        android:id="@+id/tabTravel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="旅行"/>
    <com.google.android.material.tabs.TabItem
        android:id="@+id/tabTech"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="科技"/>

</com.google.android.material.tabs.TabLayout>

我有list_item.xml,在这里我已经开发了自定义布局。

我的Fragment.java类:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    return inflater.inflate(R.layout.fragment_all, container, false);
}

当我尝试添加我的adapter.java类时,我收到这个错误:“无法在Fragment中解析findviewbyid方法”。

英文:

I would add ListView in Fragment.

This is my activitymain.xml:

&lt;com.google.android.material.tabs.TabLayout
    android:id=&quot;@+id/tablayout&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot; &gt;

    &lt;com.google.android.material.tabs.TabItem
        android:id=&quot;@+id/tabAll&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;All&quot;/&gt;
    &lt;com.google.android.material.tabs.TabItem
        android:id=&quot;@+id/tabTravel&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Travel&quot;/&gt;
    &lt;com.google.android.material.tabs.TabItem
        android:id=&quot;@+id/tabTech&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Tech&quot;/&gt;

&lt;/com.google.android.material.tabs.TabLayout&gt;

I have the list_item.xml where I have developed the custom layout.

My Fragment.java class:

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);    }

When I try to add my adapter.java class I receive this error:
"cannot resolve method findviewbyid in fragment"

答案1

得分: 1

根据您的截图,您犯了几个错误
请进行更正。

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_all, container, false); 

      // 将此段后面的内容移到下面的方法中。
    }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    
    
       // blah 
        ArrayList<CouponCategory> couponcategory = 您之前写的内容
    
       //
       ListView couponcategoryListView = view.findViewById(R.id.your_list_id);
    
       // 您之前的内容...
       // 适配器初始化
       // 设置 blah...
    
        }
英文:

By checking your screen-shot, you made several mistakes
Correct them.

@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&lt;CouponCategory&gt; couponcategory = whatever you have written

   //
   ListView couponcategoryListView = view.findViewById(R.id.your_list_id);

   // whatever you have written....\
   // adapter initialization
   // settinh up blah...

    }

huangapple
  • 本文由 发表于 2020年7月23日 21:20:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63055286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定