RecyclerView在Fragment Activity中

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

Recycler View on Fragment Activity

问题

我在代码中让50条数据显示出来但是我把它放在一个片段Fragment中的可循环视图RecyclerView因为我有一个下拉菜单但是当我添加调用所有内容的代码时我遇到了这两行错误

    package com.example.tallerof.ui.gallery;

    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.annotation.NonNull;
    import androidx.fragment.app.Fragment;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    import com.example.tallerof.R;
    import com.example.tallerof.adapters.UsuariosAdapter;
    
    import java.util.ArrayList;
    
    public class GalleryFragment extends Fragment {
    
        private GalleryViewModel galleryViewModel;
    
        ArrayList<String> ListadeDatos;
        RecyclerView recyclerView;
    
        public View onCreateView(@NonNull LayoutInflater inflater,
                                 ViewGroup container, Bundle savedInstanceState) {
    
            recyclerView = (RecyclerView) findViewById(R.id.Recycler1); /* 错误 #1 无法解析方法 findViewById 在 GalleryFragment 中 */
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false)); /* 错误 #2 需要类型: context 提供: GalleryFragment */
    
            ListadeDatos = new ArrayList<String>();
    
            for (int i = 0; i <= 50; i++) {
                ListadeDatos.add("Dato # " + i + "");
            }
    
            UsuariosAdapter adapter = new UsuariosAdapter(ListadeDatos);
            recyclerView.setAdapter(adapter);
    
            return null;
        }
    }

注意:由于您要求只返回翻译的代码部分,因此我已经删除了您原始代码中的错误注释。如果需要错误注释,请在您的代码中添加。

英文:

I am making 50 data appear in my code, but I am putting it in a recycler view in a fragment because I have a dropdown menu, but when I put the code to call everything I get these two lines of error:

package com.example.tallerof.ui.gallery;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tallerof.R;
import com.example.tallerof.adapters.UsuariosAdapter;

import java.util.ArrayList;

public class GalleryFragment extends Fragment {

    private GalleryViewModel galleryViewModel;

    ArrayList&lt;String&gt; ListadeDatos;
    RecyclerView recyclerView;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

    recyclerView=(RecyclerView) findViewById(R.id.Recycler1); /* Error #1 Cannot resolve method findViewById in GalleryFragment*/
    recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); /* Error #2 required type: context provide: Gallery Fragment*/

    ListadeDatos=new ArrayList&lt;String&gt;();

    for(int i=0; i&lt;=50; i++){
        ListadeDatos.add(&quot;Dato # &quot;+i+&quot;&quot;);

    }

        UsuariosAdapter adapter= new UsuariosAdapter(ListadeDatos);
        recyclerView.setAdapter(adapter);

        return null;
    }


}

答案1

得分: 0

你必须这样做。

recyclerView = getActivity().findViewById(R.id.Recycler1);

谢谢。

英文:

you must to do this.

recyclerView = getActivity().findViewbyId(R.id.Recycler1);

Regards.

huangapple
  • 本文由 发表于 2020年6月29日 03:04:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/62627047.html
匿名

发表评论

匿名网友

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

确定