所以我确定我在某处犯了一些微不足道的错误而且看不到它。但我只是想创建一个基本的Recyclerview列表。但是,当我运行应用程序时,没有显示任何内容,我输入了日志语句,发现适配器已创建但未调用任何内容(如bindviewholder,createviewholder或getItemsize)。我不确定发生了什么事。代码低于“
MainActivity:
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);
}
}
}
答案 0 :(得分:2)
如果您想使用RecyclerView
,则需要使用以下内容:
RecyclerView.Adapter
- 处理数据收集并将其绑定到
视图
LayoutManager
- 帮助定位项目
ItemAnimator
- 帮助制作常用操作项目的动画
例如添加或删除项目
item animator是可选的,但是必须使用适配器和Layoutmanager
你忘了添加布局管理器。这两行代码可能解决了你的问题
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);
答案 1 :(得分:0)
试试这个答案
https://stackoverflow.com/a/35802948/2144418
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
答案 2 :(得分:0)
添加
recyclerView.setLayoutManager(new LinearLayoutManager(context));
在回收者查看初始化之后。如果要使用项目网格,也可以设置GridLayoutManager。