使用RecyclerView展示資料(可切換列表模式)

Castertonone發表於2018-05-03
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"/>

相關文章