在recyclerView

时间:2018-01-04 13:21:28

标签: android android-recyclerview textview imageview

我想在recyclerview中一起显示html文本和图像,但我猜不了。当ı习惯了一个按钮它的工作原理。但是ı尝试使用recyclerview它不起作用。有人帮我吗?同时我在android上太新手了。 谢谢大家。

    public MyViewHolder(View itemView) {
        super(itemView);

        itemView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View position) {
           int mposition = getLayoutPosition();
              callActivities(mposition);
           }

            private void callActivities(int mposition) {
               if (mposition == 0) {
                   image.setImageResource(R.drawable.bb);
                   String htmlAsString = getString(R.string.bb);
                   Spanned htmlAsSpanned = Html.fromHtml(htmlAsString);
                   vh= (TextView) findViewById(R.id.vh);
                   vh.setText(htmlAsSpanned);
                   ((TextView)findViewById(R.id.vh)).setVisibility(View.VISIBLE);  ((TextView)findViewById(R.id.baslik)).setVisibility(View.GONE);
               } else if (mposition == 1){
                   image.setImageResource(R.drawable.db);
                   String htmlAsString = getString(R.string.db);
                   Spanned htmlAsSpanned = Html.fromHtml(htmlAsString);
                   vh= (TextView) findViewById(R.id.vh);
                   vh.setText(htmlAsSpanned);
                   ((TextView)findViewById(R.id.vh)).setVisibility(View.VISIBLE);  ((TextView)findViewById(R.id.baslik)).setVisibility(View.GONE);


               } else if (mposition == 2){
                   image.setImageResource(R.drawable.cb);
                   String htmlAsString = getString(R.string.cb);
                   Spanned htmlAsSpanned = Html.fromHtml(htmlAsString);
                   vh= (TextView) findViewById(R.id.vh);
                   vh.setText(htmlAsSpanned);
                   ((TextView)findViewById(R.id.vh)).setVisibility(View.VISIBLE);  ((TextView)findViewById(R.id.baslik)).setVisibility(View.GONE);
               }      ((TextView)findViewById(R.id.vh)).setVisibility(View.VISIBLE);  ((TextView)findViewById(R.id.baslik)).setVisibility(View.GONE);
               } 
            }
        });

}

}

1 个答案:

答案 0 :(得分:0)

让我看看我是否做对了..我正在使用的这个例子应该对你有所帮助

public class YourAdapter extends RecyclerView.Adapter<YourAdapter.MyViewHolder>{

    class MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView textview;

        private MyViewHolder(View view){
            super(view);
            //here you will be able to retrieve the views inside each item of the recyclerview
            imageView = view.findViewById(R.id.imageView);
            textView = view.findViewById(R.id.textView);
        }
    }

    public YourAdapter(){//constructor}

    @Override
    public int getItemCount(){
        return 0;
    }

    @Override
    public YourAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view= LayoutInflater.from(parent.getContext())
            .inflate(R.layout.layour_for_rv_item, parent, false);

        return new YourAdapter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(YourAdapter.MyViewHolder holder, position){
        // here you link your data 
        // for instance set the text to the textView
    }
}