RecyclerView(4)简单示例

RecyclerView(4)简单示例

1,RecyclerViewFrgmt

 import com.example.adapter.R;

 import android.app.Fragment;
 import android.os.Bundle;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;

 public class RecyclerViewFrgmt extends Fragment {

     RecyclerView mRecyclerView;
     RecyclerAdapter mAdapter;
     String[] dataset;

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
         View v = inflater.inflate(R.layout.frgmt_recycler_view_adapter, container,false);
         mRecyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
         LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
 //        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);//可以设置recyler view 为横向
         // 设置布局管理器
         mRecyclerView.setLayoutManager(layoutManager);  

         dataset = ];
         ; i < dataset.length; i++){
             dataset[i] = "item" + i;
         }
         mAdapter = new RecyclerAdapter(dataset);
         mRecyclerView.setAdapter(mAdapter);
         return v;
     }
 }

2,RecyclerAdapter

 import com.example.adapter.R;
 import android.support.v7.widget.RecyclerView;
 import android.support.v7.widget.RecyclerView.Adapter;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.TextView;

 public class RecyclerAdapter extends Adapter<RecyclerAdapter.ViewHolder> {

     private String[] mDataset;//数据

     //关键部分,每个item的holder
     public static class ViewHolder extends RecyclerView.ViewHolder {//
         public ImageView mIcon;
         public TextView mContent;
         public Button mBtn;
         public ViewHolder(View itemView) {
20             super(itemView);
21             mIcon = (ImageView) itemView.findViewById(R.id.recycler_view_icon);
22             mContent = (TextView) itemView.findViewById(R.id.recycler_view_content);
23             mBtn = (Button) itemView.findViewById(R.id.recycler_view_btn);
         }
     }

     public RecyclerAdapter(String[] dataset) {
         mDataset = dataset;
     }

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

     //把数据和 view holder绑定
     @Override
     public void onBindViewHolder(final ViewHolder holder, final int position) {
         holder.mContent.setText(mDataset[position]);
         holder.mBtn.setText("button" + position);
         holder.mBtn.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 System.out.println("btn");
                 holder.mContent.setText("button" + position);
             }
         });
     }
     //创建一个view holder
     @Override
     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         //1,inflater一个view
         View view = View.inflate(parent.getContext(),R.layout.recycler_view_item, null);
         //2,将view传给holder
         ViewHolder holder = new ViewHolder(view);
         return holder;
     }

 }

3,使用recyclerview的主布局 frgmt_recycler_view_adapter.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >

     <android.support.v7.widget.RecyclerView
         android:id="@+id/recyclerView"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

 </LinearLayout>

4,每个item的布局  recycler_view_item.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="horizontal" >

     <ImageView
         android:id="@+id/recycler_view_icon"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:src="@drawable/ic_child2" />

     <TextView
         android:id="@+id/recycler_view_content"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:text="TextView" />

     <Button
         android:id="@+id/recycler_view_btn"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:text="Button" />

 </LinearLayout>
上一篇:Java中二进制字节与十六进制互转


下一篇:angular4.0中form表单双向数据绑定正确姿势