RecyclerView之UI与数据绑定

package activity.cyq.com.learnrsv;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by DL_007 on 2015/7/19.
 */
class MyAdapter extends RecyclerView.Adapter {

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView txTitle;
        private TextView txContent;

        public ViewHolder(View root) {
            super(root);
            txTitle = (TextView) root.findViewById(R.id.title);
            txContent = (TextView) root.findViewById(R.id.content);
        }

        public TextView getTxTitle() {
            return txTitle;
        }

        public TextView getTxContent() {
            return txContent;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.listcell, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;
        Artical ac = data[position];
        vh.getTxTitle().setText(ac.title);
        vh.getTxContent().setText(ac.content);

    }

    @Override
    public int getItemCount() {
        return data.length;
    }

    private Artical[] data = new Artical[]{
            new Artical("标题一", "文章内容"),
            new Artical("标题一", "文章内容"),
            new Artical("标题一", "文章内容"),
            new Artical("标题一", "文章内容")};
}

 

posted @ 2015-07-20 00:48  木头同学  阅读(1549)  评论(1编辑  收藏  举报