RecycleView的使用



1.添加依赖:  

compile 'com.android.support:recyclerview-v7:25.1.1'


2.适配器:

 

public class TestRecyAdapter  extends RecyclerView.Adapter<TestRecyAdapter.MyHolder>{

    List<Test> list;
    Context context;

    public TestRecyAdapter(List<Test> list,Context context){
        this.list = list;
        this.context = context;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        MyHolder vh = new MyHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        holder.tv1.setText(list.get(position).getName());
        holder.tv2.setText(list.get(position).getAge());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    
    public static class MyHolder extends RecyclerView.ViewHolder{
        TextView tv1,tv2;
        public MyHolder(View itemView) {
            super(itemView);
            tv1 = (TextView) itemView.findViewById(R.id.textView2);
            tv2 = (TextView) itemView.findViewById(R.id.textView3);
        }
    }
}


main.xml

 

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


activity调用

public class TestActivity extends AppCompatActivity {
    List<Test> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_phone);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        TestRecyAdapter adapter = new TestRecyAdapter(list,this);
        recyclerView.setAdapter(adapter);

    }
}

设置布局方向:

  

横向布局

如果想要一个横向的List只要设置LinearLayoutManager如下就行,注意要声明mLayoutManager的类型是LinearLayoutManager而不是父类LayoutManager:

 
 
  1. mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
Grid布局

如果想要一个Grid布局的列表,只要声明LayoutManager为GridLayoutManager即可:

 
 
  1. mLayoutManager = new GridLayoutManager(context,columNum);
  2. mRecyclerView.setLayoutManager(mLayoutManager);

注意,在Grid布局中也可以设置列表的Orientation属性,来实现横向和纵向的Grid布局。

瀑布流布局

瀑布流就使用StaggeredGridLayoutManager吧,具体方法与上面类似,就不做介绍啦。


manifest注册

<activity android:name="com.mxdnp.adapter.TestActivity"
    android:theme="@style/AppTheme"/>


adapter中还可以添加点击事件、长按事件

@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_zj,parent,false);
    MyHolder vh = new MyHolder(view);
    vh.tv1.setOnClickListener(new View.OnClickListener() { //点击事件
        @Override
        public void onClick(View view) {
           
        }
    });
    vh.tv2.setOnLongClickListener(new View.OnLongClickListener() { //长按事件
        @Override
        public boolean onLongClick(View view) {
            return false;
        }
    });
    return vh;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值