gpt4 book ai didi

java - RecyclerView 不显示项目。不调用适配器

转载 作者:搜寻专家 更新时间:2023-11-01 09:40:42 26 4
gpt4 key购买 nike

所以我确定我在某个地方犯了一些微不足道的错误,只是看不到它。但我只是想创建一个基本的 recyclerview 列表。但是,当我运行该应用程序时,没有显示任何内容,我输入了日志语句,发现适配器已创建,但从未对其调用任何内容(如 bindviewholder、createviewholder 或 getItemsize)。我不确定发生了什么。代码如下”

主要 Activity :

public class MainActivity extends GvrActivity {
private ArrayList<SampleItem> videos;
private RecyclerView recyclerView;
private SampleAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

intialization();

}

private void intialization() {
Log.e("tree","mata intialized");
videos = new ArrayList<>();
videos.add(new SampleItem("http://vid1383.photobucket.com/albums/ah303/intellidev/congo_zpsrtmtey4l.mp4", "Adrian", 50, 100));
Log.e("tree","video added");
recyclerView = (RecyclerView) findViewById(R.id.recycle);
adapter = new SampleAdapter(this, videos);
recyclerView.setAdapter(adapter);
Log.e("tree","intialization completed");
}
}

适配器

public class SampleAdapter extends     RecyclerView.Adapter<SampleAdapter.ItemHolder> {
private Context context;
private ArrayList<SampleItem> items;

public SampleAdapter(Context context, ArrayList<SampleItem> videos) {

this.context = context;
this.items = videos;
Log.e("tree","adapter created");
}

@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.video_item, parent, false);
Log.e("tree","onCreateView Done");
return new ItemHolder(itemView);
}

@Override
public void onBindViewHolder(ItemHolder holder, int position) {
Uri uri = Uri.parse(items.get(0).getVideoUrl());
VrVideoView.Options options = new VrVideoView.Options();
options.inputType = VrVideoView.Options.TYPE_STEREO_OVER_UNDER;
try {
holder.view.loadVideo(uri, options);
holder.view.playVideo();
} catch (IOException e) {
Log.e("tree","onBindViewError " +e.getMessage());
}
holder.likes.setText(items.get(position).getNumOfLikes());
holder.shares.setText(items.get(position).getNumOfShares());
holder.profile.setText(items.get(position).getProfileName());
Log.e("tree","onBindViewDone");

}

@Override
public int getItemCount() {
Log.e("tree","items size is "+items.size());
return items.size();
}

class ItemHolder extends RecyclerView.ViewHolder {
private VrVideoView view;
private Button likes;
private Button shares;
private Button profile;

public ItemHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);

view = (VrVideoView) itemView.findViewById(R.id.video_view);
likes = (Button) itemView.findViewById(R.id.likesButton);
shares = (Button) itemView.findViewById(R.id.sharesButton);
profile = (Button) itemView.findViewById(R.id.videoProfileButton);

}

}
}

最佳答案

如果你想使用 RecyclerView,你需要处理以下内容:

  • RecyclerView.Adapter - 处理数据收集并将其绑定(bind)到 View

  • LayoutManager - 帮助定位项目

  • ItemAnimator - 帮助为常见操作设置项目动画例如项目的添加或删除

item animator 是可选的,但 adapter 和 Layoutmanager 是必需的

您忘记添加布局管理器。这两行代码可能解决了你的问题

LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);

关于java - RecyclerView 不显示项目。不调用适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40046450/

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