使用RecyclerView展示数据(可切换列表模式)

本文介绍如何使用RecyclerView在Android应用中展示数据,并详细讲解如何实现列表模式的切换功能。通过引入com.android.support:recyclerview-v7:25.2.0库,开发者可以创建高效、动态的数据列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.线性布局
2.网格布局
3.瀑布流布局

一.http
1.HttpConfig

public class HttpConfig { public static String goodslist_url = "https://www.zhaoapi.cn/product/getCatagory"; }
二.model
1.GoodsListener
public interface GoodsListener {
    void getSuccess(String json);
    void getError(String error);
}
2.IModel
public interface IModel {

    void getGoodsListData(String url,GoodsListener goodsListener);

}
3.ModelImpl
public class ModelImpl implements IModel{ @Override public void getGoodsListData(String url, final GoodsListener goodsListener) { HttpUtils httpUtils = HttpUtils.getHttpUtils(); httpUtils.okGet(url); httpUtils.setOkLoadListener(new OkLoadListener() { @Override public void okLoadSuccess(String json) { goodsListener.getSuccess(json); } @Override public void okLoadError(String error) { goodsListener.getError(error); } }); } }
三.presenter
1.IPresenter
public interface IPresenter {

     void showGoodsListToView(IModel iModel, IMainView iMainView);
}
2.PresenterImpl
public class PresenterImpl implements IPresenter{
    private static final String TAG = "PresenterImpl----";

    @Override
    public void showGoodsListToView(IModel iModel, final IMainView iMainView) {
        iModel.getGoodsListData(HttpConfig.goodslist_url, new GoodsListener() {
            @Override
            public void getSuccess(String json) {
                Log.d(TAG, "getSuccess: "+json);
                Gson gson =new Gson();
                UserBean userBean = gson.fromJson(json, UserBean.class);
                List<UserBean.DataBean> data = userBean.getData();
                iMainView.showGoodsList(data);
            }

            @Override
            public void getError(String error) {
                Log.d(TAG, "getError: "+error);
            }
        });
    }
}
四.view
1.IMainView

public interface IMainView {
    void showGoodsList(List<UserBean.DataBean> list);
}
2.MainActivity
public class MainActivity extends AppCompatActivity implements IMainView,View.OnClickListener{

    private Button grid_button,list_button,pubu_button;
    private PresenterImpl presenter;
    private RecyclerView recyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    private void initData(){
        presenter = new PresenterImpl();
        presenter.showGoodsListToView(new ModelImpl(),this);
    }

    private void initView() {

        grid_button = (Button) findViewById(R.id.grid_button);
        list_button= (Button) findViewById(R.id.list_button);
        pubu_button= (Button) findViewById(R.id.pubu_button);
       recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayout = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(linearLayout);
        pubu_button.setOnClickListener(this);
        grid_button.setOnClickListener(this);
        list_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.grid_button:
                GridLayoutManager gridLayoutManager = new GridLayoutManager(this,5);
                recyclerView.setLayoutManager(gridLayoutManager);
                initData();
                break;
            case R.id.list_button:
                LinearLayoutManager linearLayout = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
                recyclerView.setLayoutManager(linearLayout);
                initData();
                break;
            case R.id.pubu_button:
                StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(10,StaggeredGridLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(staggeredGridLayoutManager);
                initData();
                break;
        }
    }

    @Override
    public void showGoodsList(List<UserBean.DataBean> list) {
        MyAdapter myAdapter = new MyAdapter(MainActivity.this,list);
        recyclerView.setAdapter(myAdapter);
    }
}
2.5  activity_main
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="线性布局" android:id="@+id/list_button"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="表格布局" android:id="@+id/grid_button"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="瀑布流布局" android:id="@+id/pubu_button"/> </LinearLayout> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/recyclerView"></android.support.v7.widget.RecyclerView> </LinearLayout>

3.MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{ private Context context; private List<UserBean.DataBean> list; public MyAdapter(Context context, List<UserBean.DataBean> list) { this.context = context; this.list = list; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false); MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.getTextView().setText(list.get(position).getName()); } @Override public int getItemCount() { return list.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ private TextView textView; public MyViewHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.name); } public MyViewHolder(View itemView, TextView textView) { super(itemView); this.textView = textView; } public TextView getTextView() { return textView; } public void setTextView(TextView textView) { this.textView = textView; } } }
3.5 item_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:gravity="center"
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="数据一"/>

</LinearLayout>

   五.依赖与权限1.1

1.

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

compile 'com.google.code.gson:gson:2.6.2'

compile 'com.squareup.okhttp3:okhttp:3.3.0'

compile 'com.github.bumptech.glide:glide:3.7.0'
2.

<uses-permission android:name="android.permission.INTERNET"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值