开发者社区> 问答> 正文

Android RecyclerView不在片段上显示图像或文本

我正在尝试按照在线教程在片段上构建RecyclerView,然后将图像和文本加载到卡中。我收到一个错误:

com.example.gena E / RecyclerView:未连接适配器;跳过布局

下面的代码是片段

package com.example.gena;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

public class ApprenticeFrag extends Fragment {

    private RecyclerView recyclerView;
    private ApprenticeAdapter adapter;
    private List<ApprenticeData> mApprenticeList;
    private ApprenticeData mApprenticeData;

    public ApprenticeFrag() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.apprentice_frag, container, false);

        recyclerView = rootView.findViewById(R.id.recyclerView);

        mApprenticeList = new ArrayList<>();
        mApprenticeData = new ApprenticeData("john doe1", getString(R.string.appr1_descr), R.drawable.appr1);
        mApprenticeList.add(mApprenticeData);
        mApprenticeData = new ApprenticeData("john doe2", getString(R.string.appr2_descr), R.drawable.appr2);
        mApprenticeList.add(mApprenticeData);
        mApprenticeData = new ApprenticeData("john doe3", getString(R.string.appr3_descr), R.drawable.appr3);
        mApprenticeList.add(mApprenticeData);
        mApprenticeData = new ApprenticeData("john doe4", getString(R.string.appr4_descr), R.drawable.appr4);
        mApprenticeList.add(mApprenticeData);
        mApprenticeData = new ApprenticeData("john doe5", getString(R.string.appr5_descr), R.drawable.appr5);
        mApprenticeList.add(mApprenticeData);
        mApprenticeData = new ApprenticeData("john doe6", getString(R.string.appr6_descr), R.drawable.appr6);
        mApprenticeList.add(mApprenticeData);
        mApprenticeData = new ApprenticeData("john doe7", getString(R.string.appr7_descr), R.drawable.appr7);
        mApprenticeList.add(mApprenticeData);
        mApprenticeData = new ApprenticeData("john doe8", getString(R.string.appr8_descr), R.drawable.appr8);
        mApprenticeList.add(mApprenticeData);
        mApprenticeData = new ApprenticeData("john doe9", getString(R.string.appr9_descr), R.drawable.appr9);
        mApprenticeList.add(mApprenticeData);
        mApprenticeData = new ApprenticeData("john doe10", getString(R.string.appr10_descr), R.drawable.appr10);
        mApprenticeList.add(mApprenticeData);
        mApprenticeData = new ApprenticeData("john doe11", getString(R.string.appr11_descr), R.drawable.appr11);
        mApprenticeList.add(mApprenticeData);

        adapter = new ApprenticeAdapter(getActivity(), mApprenticeList);
        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(mLayoutManager);

        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);



        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.apprentice_frag, container, false);
    }
}

另外我正在使用的适配器如下:

package com.example.gena;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;


public class ApprenticeAdapter extends RecyclerView.Adapter<ApprenticeViewHolder> {

    private Context mContext;
    private List<ApprenticeData> mApprenticeList;

    ApprenticeAdapter(Context mContext, List<ApprenticeData> mApprenticeList) {
        this.mContext = mContext;
        this.mApprenticeList = mApprenticeList;
    }


    @NonNull
    @Override
    public ApprenticeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_row, parent, false);
        return new ApprenticeViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(final ApprenticeViewHolder holder, int position) {
        holder.mImage.setImageResource(mApprenticeList.get(position).getApprenticePicture());
        holder.mTitle.setText(mApprenticeList.get(position).getApprenticeName());

    }

    @Override
    public int getItemCount() {
        return 0;
    }

}

class ApprenticeViewHolder extends RecyclerView.ViewHolder {

    CardView mCardView = itemView.findViewById(R.id.cardview);
    ImageView mImage;
    TextView mTitle;

    ApprenticeViewHolder(View itemView) {
        super(itemView);

        mImage = itemView.findViewById(R.id.ivImage);
        mTitle = itemView.findViewById(R.id.tvTitle);
    }
}

任何帮助将不胜感激。谢谢

问题来源:Stack Overflow

展开
收起
montos 2020-03-26 21:12:24 515 0
1 条回答
写回答
取消 提交回答
  • 您面临的问题是由于适配器校准中的原因:

    @Override
    public int getItemCount() {
        return 0;
    }
    

    将以上行更改为

    @Override
    public int getItemCount() {
        return mApprenticeList.size();
    }
    

    它将解决您的问题。

    另外,你需要在你更新的最后一行onCreateView的方法

    return inflater.inflate(R.layout.apprentice_frag, container, false);
    

    return rootView;
    

    回答来源:Stack Overflow

    2020-03-26 21:12:58
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载