gpt4 book ai didi

android - 在 Fragment(回收器 View )上调用 setAdapter 不起作用

转载 作者:行者123 更新时间:2023-12-02 08:19:30 26 4
gpt4 key购买 nike

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

该对象从 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);
}
});
}
}
}

最佳答案

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

关于android - 在 Fragment(回收器 View )上调用 setAdapter 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41477188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com