带有视频和文字的水平回收站视图

时间:2017-10-03 12:29:48

标签: android android-recyclerview android-videoview

我有一个带有缩略图图像和文本的水平循环器视图。在单击缩略图图像时,应播放相应的视频。我能够显示图像和文字,现在我不得不继续播放视频。非常感谢任何帮助。

主要活动

 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);

    }
}

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您应首先创建一个包含以下内容的类项目:&#34; title&#34;,&#34; videoUrl&#34;,&#34; thumbnail_url&#34;

然后您的水平列表将是

的列表
private List<Item> horizontalList;

然后在你的viewHolder上,你得到videoUrl并打开新活动,其意图是在其包中包含视频网址。

    Intent intent = new Intent(ctxt, VideoActivity.class);
    intent.putExtra("VIDEO_URL", item.videoUrl);
    startActivity(intent);

然后在你的VideoActivity上你应该使用检索到的videoUrl实现一个videoPlayer库来显示视频

Intent intent = getIntent();
String videoUrl = intent.getStringExtra("VIDEO_URL");