带有视频和文本的水平回收站视图
Posted
技术标签:
【中文标题】带有视频和文本的水平回收站视图【英文标题】:Horizontal Recycler View with Videos and Text 【发布时间】:2017-10-03 12:29:47 【问题描述】:我有一个带有缩略图和文本的水平回收器视图。单击缩略图图像时,应播放相应的视频。我能够显示图像和文本,现在我无法继续播放视频。任何帮助将不胜感激。
主要活动
public class MainActivity extends AppCompatActivity
private RecyclerView horizontal_recycler_view;
private ArrayList<String> horizontalList;
private HorizontalAdapter horizontalAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
horizontal_recycler_view = (RecyclerView) findViewById(R.id.horizontal_recycler_view);
horizontalList = new ArrayList<>();
horizontalList.add("horizontal 1");
horizontalList.add("horizontal 2");
horizontalList.add("horizontal 3");
horizontalList.add("horizontal 4");
horizontalList.add("horizontal 5");
horizontalAdapter = new HorizontalAdapter(this, horizontalList);
LinearLayoutManager horizontalLayoutManager
= new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
horizontal_recycler_view.setLayoutManager(horizontalLayoutManager);
horizontal_recycler_view.setAdapter(horizontalAdapter);
适配器
public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.MyViewHolder>
private List<String> horizontalList;
Context ctxt;
public HorizontalAdapter(Context context, List<String> horizontalList)
this.ctxt = context;
this.horizontalList = horizontalList;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.horizontal_item_view, parent, false);
return new MyViewHolder(itemView);
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position)
holder.txtView.setText(horizontalList.get(position));
holder.imgView.setImageResource(R.drawable.thumbnail);
holder.txtView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Toast.makeText(ctxt, holder.txtView.getText().toString(), Toast.LENGTH_SHORT).show();
);
@Override
public int getItemCount()
return horizontalList.size();
public class MyViewHolder extends RecyclerView.ViewHolder
public TextView txtView;
public ImageView imgView;
public MyViewHolder(View view)
super(view);
imgView = (ImageView) view.findViewById(R.id.thumbnail);
txtView = (TextView) view.findViewById(R.id.video_title);
【问题讨论】:
你想在另一个活动中播放视频吗?还是在单元格中? 在另一个活动中 获取点击索引视频的绝对路径并传递给下一个活动并使用androidbegin.com/tutorial/…播放 不好意思问我看不懂...能不能说的再清楚点或者举个例子... 【参考方案1】:Main Activity in --->>
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
recyclerView.setAdapter(new Top_ServiceAdapter());
---->Adapter in you horizontal_item_view.xml layout in
linearlayout in image or text with orientation vertical .... so done this problem
【讨论】:
【参考方案2】:您应该首先创建一个包含:“title”、“videoUrl”、“thumbnail_url”的类Item
那么你的水平列表将是一个列表
private List<Item> horizontalList;
然后在您的 viewHolder 上,您获取 videoUrl 并打开新 Activity,其意图是在其捆绑包中包含视频 url。
Intent intent = new Intent(ctxt, VideoActivity.class);
intent.putExtra("VIDEO_URL", item.videoUrl);
startActivity(intent);
然后在您的 VideoActivity 上,您应该实现一个 videoPlayer 库以使用检索到的 videoUrl 显示视频
Intent intent = getIntent();
String videoUrl = intent.getStringExtra("VIDEO_URL");
【讨论】:
以上是关于带有视频和文本的水平回收站视图的主要内容,如果未能解决你的问题,请参考以下文章