仿京东分类页面


//需要实现的页面






、、需要倒的依赖

//ok
implementation 'com.squareup.okhttp3:okhttp:3.9.0'
//gson
implementation 'com.google.code.gson:gson:2.8.4'
//拦截器的依赖
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
//reclerview的依赖
implementation 'com.android.support:recyclerview-v7:27.1.1'
//xRecyclerView的依赖
implementation 'com.jcodecraeer:xrecyclerview:1.3.2'
implementation 'com.android.support:design:27.1.1'
//插件依赖
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation files('libs/universal-image-loader-1.9.5.jar')

、、viewPager+fragment滑动切换布局页面

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:background="#0099ff"
        >
    </android.support.v4.view.ViewPager>


<RadioGroup
    android:id="@+id/radio"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <RadioButton
        android:id="@+id/main_btn_class"
        android:layout_width="50dp"
        android:layout_height="60sp"
        android:background="@drawable/class_selected"
        android:layout_weight="1"
        android:button="@null"
        android:gravity="center"
        />

<View
    android:layout_width="0dp"
    android:layout_height="0.75dp"
    android:background="#000"
    />

    <RadioButton
        android:id="@+id/main_btn_cart"
        android:layout_width="50dp"
        android:layout_height="60sp"
        android:background="@drawable/cart_selected"
        android:layout_weight="1"
        android:button="@null"
        android:gravity="center"
        />

</RadioGroup>
    </LinearLayout>
</LinearLayout>

//fragment的适配器

public class MyFragment extends FragmentPagerAdapter {

    private List<Fragment> list;

    public MyFragment(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.list = list;
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }


}

//MainActivit

public class MainActivity extends AppCompatActivity {

    private List<Fragment> list = new ArrayList<>();
    private RadioGroup radio;
    private ViewPager viewPager;
    private RadioButton btn_class, btn_cart;

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

        initViews();
    }

    private void initViews() {

        radio = findViewById(R.id.radio);
        viewPager = findViewById(R.id.viewpager);
        btn_class = findViewById(R.id.main_btn_class);
        btn_cart = findViewById(R.id.main_btn_cart);
        //创建一个装fragment的容器
        Classesfragment classesfragment = new Classesfragment();
        CartFragment cartFragment = new CartFragment();
        list.add(classesfragment);
        list.add(cartFragment);

        //创建适配器
        MyFragment myFragment = new MyFragment(getSupportFragmentManager(), list);
        viewPager.setAdapter(myFragment);
        //设置默认选中
        radio.check(R.id.main_btn_class);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                switch (position) {
                    case 0:
                        radio.check(R.id.main_btn_class);
                        break;
                    case 1:
                        radio.check(R.id.main_btn_cart);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.main_btn_class:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.main_btn_cart:
                        viewPager.setCurrentItem(1);
                        break;
                }
            }
        });
    }
}


//分类页面布局(左边listview布局,右边recyleerview布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <Button
            android:id="@+id/btn_sao"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/sao_kind" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80dp"
            android:src="@drawable/a_4" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:ems="11" />

        <Button
            android:id="@+id/btn_you"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@drawable/order_msg" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ListView
            android:id="@+id/class_listview"
            android:layout_width="120dp"
            android:layout_height="match_parent"></ListView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

            </android.support.v7.widget.RecyclerView>

        </LinearLayout>
    </LinearLayout>



</LinearLayout>
 
//分类适配器布局 

//先左  左边布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/base_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文字"
         android:layout_gravity="center"
        android:textSize="20dp"
        />

</LinearLayout>

//左边适配

public class MyBaseAdapter extends BaseAdapter {

    private Context context;
    private List<LeftBean.DataBean> list;

    public MyBaseAdapter(Context context, List<LeftBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {

            convertView = View.inflate(context, R.layout.classes_item_base, null);

            holder = new ViewHolder();
            holder.text = convertView.findViewById(R.id.base_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.text.setText(list.get(position).getName());
        return convertView;
    }

    class ViewHolder {
        TextView text;
    }
}

//右边布局页面(右边布局需要用到recylerview套recylerview)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >

    <TextView
        android:id="@+id/item03_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_centerHorizontal="true"
        />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/item03_reView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/item03_tv"
        android:layout_centerHorizontal="true"
        >

    </android.support.v7.widget.RecyclerView>



</RelativeLayout>

//右边的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/base02_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文字"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:textSize="25dp"
        android:layout_marginBottom="20dp"
        />
    <ImageView
        android:id="@+id/base02_img"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@mipmap/ic_launcher"

        />

</LinearLayout>

//右边适配器(大的适配器里面套一个小的适配器)

public class MyAdapter extends RecyclerView.Adapter {

    private static final String TAG = "MyAdapter*****";
    private List<RightBean.DataBean> list;
    private Context context;
    private MyGridAdapter myGridAdapter;
    private RecyclerView recyclerView;


    public MyAdapter(List<RightBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = View.inflate(parent.getContext(), R.layout.classes_item_base002, null);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        Log.d(TAG, "onBindViewHolder: " + list.size());
        Log.d(TAG, "onBindViewHolder: " + list.get(position).getName());
        ((MyViewHolder)holder).textView.setText(list.get(position).getName());
        myGridAdapter = new MyGridAdapter(list);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(myGridAdapter);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView textView;
        public MyViewHolder(View itemView) {
            super(itemView);

            //获取id
            recyclerView = itemView.findViewById(R.id.item03_reView);
            textView = itemView.findViewById(R.id.item03_tv);
        }
    }
}

//这个是右边小的是适配

public class MyGridAdapter extends RecyclerView.Adapter{

    private List<RightBean.DataBean> list;

    public MyGridAdapter(List<RightBean.DataBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = View.inflate(parent.getContext(), R.layout.base003_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        ((ViewHolder)holder).tv.setText(list.get(position).getList().get(position).getName());
         String p=list.get(position).getList().get(position).getIcon();
        ImageLoader.getInstance().displayImage(p, ((ViewHolder)holder).img, MyApp.getOptions());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{

        private final TextView tv;
        private final ImageView img;

        public ViewHolder(View itemView) {
            super(itemView);
            //获取id
            tv = itemView.findViewById(R.id.base02_tv);
            img = itemView.findViewById(R.id.base02_img);

        }
    }

}

//fragment的代码

public class Classesfragment extends BaseFragment<ClassesPresenter> implements ClassesView {


    private ListView listView;
    private MyBaseAdapter adapter;
    private RecyclerView recyclerView;
    private List<RightBean.DataBean.ListBean> list = new ArrayList<>();
    private MyAdapter myAdapter;

    @Override

    protected void initData() {
        presenter.getLeft();

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initViews(View view) {

        //获取id
        listView = view.findViewById(R.id.class_listview);
        //获取id
        recyclerView = view.findViewById(R.id.class_recylerview);
    }

    @Override
    protected int provId() {
        return R.layout.classesfragment;
    }

    @Override
    protected ClassesPresenter provide() {
        return new ClassesPresenter((ClassesView) this);
    }


    @Override
    public void onLeftSuccess(final LeftBean leftBean) {
        List<LeftBean.DataBean> data = leftBean.getData();
        //常见适配器
        adapter = new MyBaseAdapter(getContext(), data);
        listView.setAdapter(adapter);

        presenter.getRight(1);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<LeftBean.DataBean> data1 = leftBean.getData();
                int cid = data1.get(position).getCid();
                presenter.getRight(cid);

            }
        });


    }

    @Override
    public void onLeftFaild(String error) {

    }

    @Override
    public void onRightSuccess(RightBean rightBean) {

        List<RightBean.DataBean> data = rightBean.getData();
        myAdapter = new MyAdapter(data,getContext());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(myAdapter);
    }

    @Override
    public void onRightFaild(String error) {

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值