在片段(回收器视图)上调用setAdapter不起作用

时间:2017-01-05 04:11:47

标签: android android-studio android-fragments android-recyclerview android-adapter

我尝试在片段上设置适配器,但没有显示任何内容。

该对象从API检索数据,但不会在屏幕上显示。

我已经把断点放在rvItem.setAdapter(mainAdapter);上但是没有发生,调试直接通过。

public class ItemFragment extends Fragment {

private MainAdapter mainAdapter;
private ItemPresenter itemPresenter;
private GridLayoutManager mLayoutManager;
private List<ObjectAdapter> list = new ArrayList<>();


private String region = "br";


@Bind(R.id.rvItem)
RecyclerView rvItem;

public ItemFragment() {
    // Required empty public constructor

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_item, container, false);
    ButterKnife.bind(this, view);
    itemPresenter = new ItemPresenter();
    mLayoutManager = new GridLayoutManager(getActivity(), 4);
    rvItem.setLayoutManager(mLayoutManager);

    getList();

    return view;

}

private void getList() {
    itemPresenter.loadItemList(region, "all", AppConfigs.api_key, new ItemListListener() {
        @Override
        public void onRequestStarted() {
        }

        @Override
        public void onRequestFinished() {

        }

        @Override
        public void onError(Throwable error) {

        }

        @Override
        public void onItemListLoad(List<ItemDto> itemList) {

            displayItemList(ObjectAdapter.convertItemToObjetct(itemList));


        }
    });

}

public void displayItemList(List<ObjectAdapter> itemList) {
    list = itemList;

    mainAdapter = new MainAdapter(getActivity(), list, new MainAdapter.OnObjectClickListener() {
        @Override
        public void OnObjectClickListener(ObjectAdapter objectAdapter) {
            Toast.makeText(getActivity(), "Object Adapter" + objectAdapter.Id, Toast.LENGTH_SHORT).show();
        }
    });

    rvItem.setAdapter(mainAdapter);
    mainAdapter.notifyDataSetChanged();

}

}

适配器:

public class MainAdapter extends                    
RecyclerView.Adapter<MainAdapter.MainAdapterViewHolder>  {

public List<ObjectAdapter> mObjecterList;
private final OnObjectClickListener listener;
private Context mContext;


public interface OnObjectClickListener {
    void OnObjectClickListener(ObjectAdapter objectAdapter);
}


public MainAdapter(Context context, List<ObjectAdapter> objectAdapterList, OnObjectClickListener listener) {
    this.mObjecterList = objectAdapterList;
    this.listener = listener;
    this.mContext = context;
}

@Override
public MainAdapter.MainAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main_adapter, parent, false);

    return new MainAdapterViewHolder(view);
}

@Override
public void onBindViewHolder(MainAdapter.MainAdapterViewHolder holder, int position) {
    ObjectAdapter ob = mObjecterList.get(position);

    holder.tvNameText.setText(ob.Name);
    holder.bind(mObjecterList.get(position), listener);

    String url = "";
    switch (ob.Type){

        case CHAMPION: url = String.format(AppConfigs.portraitChampion, ob.Portrait);
            break;
        case SPELL: url = String.format(AppConfigs.portraitSpell, ob.Portrait);
            break;
        case ITEM: url = String.format(AppConfigs.portraitItem, ob.Portrait);
            break;
    }

    Picasso.with(mContext).load(url).into(holder.ivRetrato);

}

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

public static class MainAdapterViewHolder extends RecyclerView.ViewHolder {

    public TextView tvNameText;
    public ImageView ivRetrato;

    public MainAdapterViewHolder(View itemView) {
        super(itemView);

        this.tvNameText = (TextView) itemView.findViewById(R.id.tvNameText);
        this.ivRetrato = (ImageView) itemView.findViewById(R.id.ivImagePortrait);
    }

    public void bind(final ObjectAdapter  objectAdapterListItem, final OnObjectClickListener listener) {
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.OnObjectClickListener(objectAdapterListItem);
            }
        });
    }
}
}

1 个答案:

答案 0 :(得分:0)

您需要直接在onCreateView方法设置适配器,并在加载数据时调用适配器的notifyDataSetChanged()