使用Recyclerview展示Ui上面

作者: Android开发架构 | 来源:发表于2019-01-30 16:57 被阅读14次

    依赖:

    implementation 'com.hjm:BottomTabBar:1.1.1'
    implementation 'com.recker.flybanner:flybanner:1.3'
    implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.2'
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation'com.squareup.okhttp3:okhttp:3.7.0'
    implementation'com.squareup.okio:okio:1.12.0'
    implementation'com.android.support:design:28.0.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    

    xml布局文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_height="match_parent">
        <com.gy.week2.TopBar
            android:id="@+id/topbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:leftButttonBackgrount="@drawable/sel_11"
            app:rightButttonBackgrount="@drawable/sel_22"
            app:titleText="1608c"
            app:titleTextSize="30dp"
            app:titleTextColor="@color/colorPrimaryDark"></com.gy.week2.TopBar>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recy1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">
            </android.support.v7.widget.RecyclerView>
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recy2"
                android:layout_width="0dp"
                android:layout_weight="3"
                android:layout_height="match_parent">
            </android.support.v7.widget.RecyclerView>
        </LinearLayout>
    </LinearLayout>
    

    我这里面是两个Adapter.java代码:

    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.bumptech.glide.Glide;
    import com.gy.week2.R;
    import com.gy.week2.bean.GyBean;
    import com.gy.week2.bean.LgyBean;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Shoopdapter extends RecyclerView.Adapter<Shoopdapter.ViewHolder> {
       private  TextView t1_you;
       private  ImageView img_you;
       Context context;
       GyBean dataBean;
       ArrayList<GyBean.DataBean.ListBean> list1;
       ArrayList<GyBean.DataBean.ListBean> list2;
       ArrayList<GyBean.DataBean.ListBean> list3;
       public Shoopdapter(Context context,ArrayList<GyBean.DataBean.ListBean> list1) {
           this.context=context;
           this.list1=list1;
       }
       @NonNull
       @Override
       //创建view视图
       public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    
          // if (i==TYPRONE){
               //找布局
               View inflate = View.inflate(context, R.layout.you_iteam, null);
               //初始化viewholder
               ViewHolder viewHolder = new ViewHolder(inflate);
               return viewHolder;
       }
       @Override
       //绑定数据
       public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
           if (viewHolder instanceof ViewHolder){
               t1_you.setText(list1.get(i).getName());
               Glide.with(context).load(list1.get(i).getIcon()).into(img_you);
           }
       }
       @Override
       //返回条目数量
       public int getItemCount() {
           return list1.size();
       }
    
       class ViewHolder extends RecyclerView.ViewHolder {
           TextView tee1,tee2,tee3;
           ImageView imgee,imgee2,imgee3;
    
           public ViewHolder(@NonNull View itemView) {
               super(itemView);
               t1_you = itemView.findViewById(R.id.t1_you);
               img_you = itemView.findViewById(R.id.img_you);
           }
       }
    }
    
    
    
    
    
    
    
    
    
    //第二个:
    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.gy.week2.R;
    import com.gy.week2.bean.GyBean;
    import com.gy.week2.bean.LgyBean;
    
    import java.util.ArrayList;
    
    public class FenAdapter extends RecyclerView.Adapter<FenAdapter.ViewHolder>{
       private  TextView t1_zuo;
       Context context;
       ArrayList<LgyBean.DataBean> list;
       RecyiteamClick recyiteamClick;
       public FenAdapter(Context context,ArrayList<LgyBean.DataBean> list) {
           this.context=context;
           this.list=list;
       }
    
       @NonNull
       @Override
       //创建view视图
       public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
           //找布局
           View inflate = View.inflate(context, R.layout.zuo_iteam, null);
           //初始化viewholder
           ViewHolder viewHolder = new ViewHolder(inflate);
    
           return viewHolder;
       }
    
       @Override
       //绑定数据
       public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
           t1_zuo.setText(list.get(i).getName());
           t1_zuo.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   recyiteamClick.setClick(i);
               }
           });
       }
    
       //定义接口
       public interface RecyiteamClick{
           void setClick(int position);
       }
       public void SetRecyiteamClick(RecyiteamClick recyiteamClick){
           this.recyiteamClick=recyiteamClick;
       }
    
       @Override
       //返回条目数量
       public int getItemCount() {
           return list.size();
       }
    
       class ViewHolder extends RecyclerView.ViewHolder {
           TextView tee1;
           public ViewHolder(View itemView) {
               super(itemView);
               t1_zuo = itemView.findViewById(R.id.t1_zuo);
           }
       }
    }
    

    我这里是在fragment.java里面写的:

    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.support.v7.widget.DividerItemDecoration;
    import android.support.v7.widget.GridLayoutManager;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.gy.week2.R;
    import com.gy.week2.TopBar;
    import com.gy.week2.adapter.FenAdapter;
    import com.gy.week2.adapter.Shoopdapter;
    import com.gy.week2.bean.GyBean;
    import com.gy.week2.bean.LgyBean;
    import com.gy.week2.home.presenter.HomePresenterimpl;
    import com.gy.week2.home.view.IHomeView;
    import com.gy.week2.shoop.presenter.ShoopPresenterimpl;
    import com.gy.week2.shoop.view.IShoopView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ShouYeFragment extends Fragment implements IHomeView,IShoopView {
    
       private View view;
       private LinearLayoutManager linearLayoutManager;
       private GridLayoutManager gridLayoutManager;
       private RecyclerView recy1;
       private RecyclerView recy2;
       private Button leftButton;
       private Button rightButton;
       private TopBar topbar;
       private FenAdapter fenAdapter;
       private HomePresenterimpl homePresenterimpl;
       private ShoopPresenterimpl shoopPresenterimpl;
       private Shoopdapter shoopdapter;
    
       @Nullable
       @Override
       public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
           view = inflater.inflate(R.layout.shouyefragment,container,false);
           init();
           return view;
       }
       void init(){
           recy1 = view.findViewById(R.id.recy1);
           recy2 = view.findViewById(R.id.recy2);
           topbar = view.findViewById(R.id.topbar);
           leftButton = view.findViewById(R.id.leftButton);
           rightButton = view.findViewById(R.id.rightButton);
           topbar.Setonclickinterface(new TopBar.Setonclick() {
               @Override
               public void SetLeftButtonclick() {
                   getActivity().finish();
               }
    
               @Override
               public void SetRightButtonclick() {
                   Toast.makeText(getContext(),"你好",Toast.LENGTH_LONG).show();
               }
           });
           //实例化presenter
           homePresenterimpl = new HomePresenterimpl(this);
           homePresenterimpl.getPreData("1");
           //实例化presenter
           shoopPresenterimpl = new ShoopPresenterimpl(this);
           shoopPresenterimpl.getShoopPreData();
           //初始化布局管理器
           //线性布局
           linearLayoutManager = new LinearLayoutManager(getContext());
           //网格布局
           gridLayoutManager = new GridLayoutManager(getContext(),3);
           //将布局添加到里面
           recy1.setLayoutManager(linearLayoutManager);
           //添加分割线
           recy1.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
           //将布局添加到里面
           recy2.setLayoutManager(gridLayoutManager);
           //添加分割线
           recy2.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
       }
    
       @Override
       public void getMvData(LgyBean lgyBean) {
           final List<LgyBean.DataBean> data = lgyBean.getData();
           fenAdapter = new FenAdapter(getContext(), (ArrayList<LgyBean.DataBean>) data);
           fenAdapter.SetRecyiteamClick(new FenAdapter.RecyiteamClick() {
               @Override
               public void setClick(int position) {
                   /*Toast.makeText(getContext(),""+data.get(position).getCid(),Toast.LENGTH_LONG).show();*/
                   homePresenterimpl.getPreData(data.get(position).getCid());
                   shoopdapter.notifyDataSetChanged();
    
               }
           });
           recy1.setAdapter(fenAdapter);
    
    
       }
    
       @Override
       public void getMVdata(GyBean gyBean) {
           List<GyBean.DataBean.ListBean> list = gyBean.getData().get(0).getList();
           shoopdapter = new Shoopdapter(getContext(), (ArrayList<GyBean.DataBean.ListBean>) list);
           recy2.setAdapter(shoopdapter);
    
       }
    
       //解绑
       @Override
       public void onDestroy() {
           super.onDestroy();
           if (homePresenterimpl==null){
               homePresenterimpl.destory();
           }
       }
    }
    

    免费获取安卓开发架构的资料(包括Fultter、高级UI、性能优化、架构师课程、 NDK、Kotlin、混合式开发(ReactNative+Weex)和一线互联网公司关于android面试的题目汇总可以加:936332305 / 链接:点击链接加入【安卓开发架构】:https://jq.qq.com/?_wv=1027&k=515xp64

    image

    相关文章

      网友评论

        本文标题:使用Recyclerview展示Ui上面

        本文链接:https://www.haomeiwen.com/subject/sjycsqtx.html