RecyclerView中ListView GridView 一行显示 多行显示

  导入依赖
compile 'com.android.support:recyclerview-v7:25.3.1'
//效果

//布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwei.administrator.dianshang.activity.FirstActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:onClick="listview"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="listview"
            />
        <Button
            android:onClick="gridview"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="gridview"
            />
        <Button
            android:onClick="yihang"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="一行"
            />
        <Button
            android:onClick="duohang"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="多行"
            />

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

LinearLayout>

===============mainActivity===============
private RecyclerView recyclerView;
private List list;
private SimpleRecycleAdapter simpleRecycleAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    //获取控件
    recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
        //数据源
    list = new ArrayList<>();
    for (int i=0;i<1000;i++){
        list.add("条目"+i);
    }
    //设置布局管理器
    recyclerView.setLayoutManager(new LinearLayoutManager(FirstActivity.this,LinearLayoutManager.VERTICAL,false));

    //设置适配器
    simpleRecycleAdapter = new SimpleRecycleAdapter(FirstActivity.this, list);
    recyclerView.setAdapter(simpleRecycleAdapter);
}
//listview按钮的点击事件
public void listview(View view){
    recyclerView.setLayoutManager(new LinearLayoutManager(FirstActivity.this,LinearLayoutManager.VERTICAL,false));
    simpleRecycleAdapter.notifyDataSetChanged();
}
//gridview按钮的点击事件
public  void gridview(View view){
    recyclerView.setLayoutManager(new GridLayoutManager(FirstActivity.this,3));
    simpleRecycleAdapter.notifyDataSetChanged();
}
 //一行按钮的点击事件
public void yihang(View view){
   recyclerView.setLayoutManager(new LinearLayoutManager(FirstActivity.this,LinearLayoutManager.HORIZONTAL,false));
    simpleRecycleAdapter.notifyDataSetChanged();
}
//多行按钮的点击事件
   public  void duohang(View view){
     recyclerView.setLayoutManager(new GridLayoutManager(FirstActivity.this,3, OrientationHelper.HORIZONTAL,false));
   }
------------------适配器-------------------
public class SimpleRecycleAdapter extends RecyclerView.Adapter {
    Context context;
    List list;

    public SimpleRecycleAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public SimpleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false);
         SimpleHolder simpleHolder=new SimpleHolder(view);
        return simpleHolder;
    }

    @Override
    public void onBindViewHolder(SimpleHolder holder, int position) {
        holder.textView.setText(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
————————————泛型holder——————————————————
public class SimpleHolder extends RecyclerView.ViewHolder {

    public TextView textView;

    public SimpleHolder(View itemView) {
        super(itemView);
        textView = (TextView) itemView.findViewById(android.R.id.text1);

    }
}




你可能感兴趣的:(RecyclerView中ListView GridView 一行显示 多行显示)