RecyclerView实现布局的切换

public class Main2Activity extends AppCompatActivity implements MyView, View.OnClickListener {

private RecyclerView recycler;
private List<Data.DataBean> mdata = new ArrayList<>();
private MyAdapter myAdapter;
private String keywords = "手机";
private int page = 1;
private int sort = 0;
private String Murl = "http://www.zhaoapi.cn/product/searchProducts";
private MyPresenter myPresenter;
private TextView qiehuan;
private TextView zonghe;
private TextView xiaoliang;
private TextView jiage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    initView();
    //布局管理器
    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
    recycler.setLayoutManager(gridLayoutManager);

    //初始化适配器
    myAdapter = new MyAdapter(this, mdata);
    recycler.setAdapter(myAdapter);

    myPresenter = new MyPresenter(this);
    myPresenter.getData(Murl, "手机", "1", 0);
}


private void initView() {
    recycler = (RecyclerView) findViewById(R.id.recycler);
    qiehuan = (TextView) findViewById(R.id.qiehuan);
    qiehuan.setOnClickListener(this);
    zonghe = (TextView) findViewById(R.id.zonghe);
    zonghe.setOnClickListener(this);
    xiaoliang = (TextView) findViewById(R.id.xiaoliang);
    xiaoliang.setOnClickListener(this);
    jiage = (TextView) findViewById(R.id.jiage);
    jiage.setOnClickListener(this);
}

@Override
public void Success(T data) {
    Data data1 = (Data) data;
    mdata.addAll(data1.getData());
    myAdapter.setMdata(mdata);


}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {

     if (page==0){
           page=1;
         qiehuan.setBackground(getResources().getDrawable(R.drawable.grid));
         GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
         recycler.setLayoutManager(gridLayoutManager);
         myAdapter = new MyAdapter(this, mdata);
         recycler.setAdapter(myAdapter);

         myPresenter = new MyPresenter(this);
         myPresenter.getData(Murl, "手机", "1", 0);
     }
     else{
         page=0;
         qiehuan.setBackground(getResources().getDrawable(R.drawable.line));
         LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
         recycler.setLayoutManager(linearLayoutManager);
         myAdapter = new MyAdapter(this, mdata);
         recycler.setAdapter(myAdapter);

         myPresenter = new MyPresenter(this);
         myPresenter.getData(Murl, "手机", "1", 0);
     }
}

}

xml文件

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="horizontal">
    <TextView
        android:layout_marginTop="10dp"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="《"/>
    <EditText
        android:layout_marginTop="10dp"
        android:gravity="center_vertical"
        android:layout_marginLeft="30dp"
        android:layout_width="460dp"
        android:layout_height="wrap_content"
        android:hint="输入商品名字"/>
    <TextView
        android:id="@+id/qiehuan"
        android:layout_width="46dp"
        android:layout_height="46dp"
        android:background="@drawable/line"/>
</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#ccc"
    ></View>
<LinearLayout
    android:layout_marginTop="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginBottom="8dp">
    <TextView
        android:id="@+id/zonghe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20dp"
        android:gravity="center_horizontal"
        android:text="综合"/>
    <TextView
        android:id="@+id/xiaoliang"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20dp"
        android:gravity="center_horizontal"
        android:text="销量"/>
    <TextView
        android:id="@+id/jiage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20dp"
        android:gravity="center_horizontal"
        android:text="价格"/>
</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#ccc"
    android:layout_marginBottom="5dp"
    ></View>

<android.support.v7.widget.RecyclerView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:id="@+id/recycler"></android.support.v7.widget.RecyclerView>

猜你喜欢

转载自blog.csdn.net/weixin_43629061/article/details/85122580
今日推荐