左右RecyclerView,京东分类页面,静态Fragment传值。项目二升大实训必备!不用谢我,我叫雷锋!!!!

原创 2017年01月03日 21:09:18
//是不是觉得很Nb?这是本人3个月项目二的总结,算是机试项目二的总结。本人并没有因为机试末掉这么多月。废话不多说,你跟着我的思路,绝对ok。祝你升入大实训!!
//jar包
compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okio:okio:1.7.0'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
    compile 'com.android.support:recyclerview-v7:23.0.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
//okhttp
package a1501b.bawei.com.mvp.model;

import com.google.gson.Gson;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by Sunyubo on 2016/11/3.
 */

public class OkHttpUtils {
// 二维码 https://github.com/yipianfengye/android-zxingLibrary
    private static OkHttpClient mClient;
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    private OkHttpUtils() {

    }


    public static OkHttpClient getOkHttpClient() {
        if (mClient == null) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            mClient = new OkHttpClient.Builder()
                    .addInterceptor(loggingInterceptor)
                    .build();
        }
        return mClient;
    }

    /**
     * 异步post  参数为对象
     *
     * @param url
     * @param obj
     * @param callback
     */
    public static void post(String url, Object obj, Callback callback) {
        post(url, new Gson().toJson(obj), callback);
    }

    /**
     * 异步post  参数为json
     *
     * @param url
     * @param json
     * @param callback
     */
    public static void post(String url, String json, Callback callback) {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        getOkHttpClient().newCall(request).enqueue(callback);
    }
    /**
     * 异步post 参数为Map
     *
     * @param url
     * @param map
     * @param callback
     */
    public static void post(String url, Map<String, String> map, Callback callback) {
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : map.keySet())
            builder.add(key, map.get(key));

        RequestBody body = builder.build();

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        getOkHttpClient().newCall(request).enqueue(callback);
    }

    /**
     * 异步get
     *
     * @param url
     * @return
     */
    public static void get(String url, Callback callback) {

        Request request = new Request.Builder()
                .url(url)
                .build();
        getOkHttpClient().newCall(request).enqueue(callback);

    }

    /**
     * 同步get
     *
     * @param url
     * @return
     */
    public static String get(String url) {

        Request request = new Request.Builder()
                .url(url)
                .build();
        try {
            Call call = getOkHttpClient().newCall(request);
            Response response = call.execute();

            if (response.isSuccessful()) {
                return response.body().string();
            } else
            {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

//mainactivity的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bawei.sunyubo20161222.MainActivity">
    <fragment
     android:id="@+id/left_fragment"
     android:name="fragment.LeftFragment"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
           android:layout_weight="3" />

         <fragment
             android:name="fragment.Right_fragment"
             android:id="@+id/right_fragment"
           android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
</LinearLayout>
//左边的fragment的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:orientation="vertical"
              android:layout_height="match_parent">

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

</LinearLayout>
//左边recyclerView的item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:orientation="horizontal"
                android:padding="2dp"
              android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >
        <ImageView
            android:layout_width="3dp"
            android:background="#f00"
            android:layout_height="match_parent"
            android:id="@+id/imageView"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/left_textView"
            android:gravity="center"
            android:text="12"
            />

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

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

//右边recyclerView的item1标题
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:orientation="vertical"
              android:layout_height="wrap_content">
    <TextView
        android:text="123"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item1_right_name"
        />
</LinearLayout>
//右边recylerView的item2的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:orientation="vertical"
              android:layout_height="wrap_content">
<ImageView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:id="@+id/imgView2"
    android:src="@mipmap/ic_launcher"
    />
    <TextView
        android:text="123"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        />
</LinearLayout>
//activity不用写东西
package bawei.sunyubo20161222;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
//左边的fragment
package fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
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 com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import adpter.AdpterLeft;
import bawei.mylibrary.OkHttpUtils;
import bawei.sunyubo20161222.MainActivity;
import bawei.sunyubo20161222.R;
import bean.Bean;
import myclick.MyOnitemClickListener;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
 * 1.类的用途
 * 2.@author:Sunyubo
 * 3.@ 2016/12/22.
 */

public class LeftFragment extends Fragment {

    private RecyclerView lv;
    private List<Bean.RsBean> list;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=View.inflate(getActivity(), R.layout.left_fragment,null);
        lv =(RecyclerView) view.findViewById(R.id.listView);
        getData();
        return view;
    }
    private void getData() {
        OkHttpUtils.get("http://mock.eoapi.cn/success/4q69ckcRaBdxhdHySqp2Mnxdju5Z8Yr4", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                        String s=response.body().string();
                        Message message=Message.obtain();
                        message.obj=s;
                       handler.sendMessage(message);

            }
        });
    }
int index=0;
    List<Bean.RsBean.ChildrenBean> listall=new ArrayList<>();
    private FragmentTransaction transaction;
    Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        MainActivity activity= (MainActivity) getActivity();
        FragmentManager  manager= activity.getSupportFragmentManager();
        final Right_fragment right_fragment= (Right_fragment) manager.findFragmentById(R.id.right_fragment);

        final String s= (String) msg.obj;
        final Gson gson=new Gson();
        list = gson.fromJson(s,Bean.class).rs;

        list.get(0).check=true;
        List<Bean.RsBean.ChildrenBean> child=list.get(0).children;
        for(int i=0;i<child.size();i++){
            child.get(i).isHeader=true;
            listall.add(child.get(i));
            listall.addAll(child.get(i).children);
        }
        right_fragment.upList(listall);

        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        lv.setLayoutManager(linearLayoutManager);
        final AdpterLeft adpter=new AdpterLeft(list,getActivity());
        adpter.setOnitemClickListener(new MyOnitemClickListener() {
            @Override
            public void setOnitemClickListener(int position) {
                listall.clear();
                list.get(index).check=false;
                list.get(position).check=true;
                index=position;
                adpter.notifyDataSetChanged();;
                List<Bean.RsBean.ChildrenBean> child=list.get(position).children;
                for(int i=0;i<child.size();i++){
                    child.get(i).isHeader=true;
                    listall.add(child.get(i));
                    listall.addAll(child.get(i).children);
                }

                right_fragment.upList(listall);
            }
        });
        lv.setAdapter(adpter);
    }
};
}
//左边fragment的适配器
package adpter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import bawei.sunyubo20161222.R;
import bean.Bean;
import myclick.MyOnitemClickListener;

/**
 * 1.类的用途
 * 2.@author:Sunyubo
 * 3.@ 2016/12/22.
 */

public class AdpterLeft extends RecyclerView.Adapter<AdpterLeft.Vh> {
    List<Bean.RsBean> list;
    Context context;

    public AdpterLeft(List<Bean.RsBean> list, Context context) {
        this.list = list;
        this.context = context;
    }
    MyOnitemClickListener onitemClickListener;
    public void setOnitemClickListener(MyOnitemClickListener onitemClickListener){
        this.onitemClickListener=onitemClickListener;
    }

    @Override
    public Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Vh(View.inflate(context, R.layout.item_left,null));
    }

    @Override
    public void onBindViewHolder(Vh holder, final int position) {
            if(list.get(position).check){
                holder.i.setVisibility(View.VISIBLE);
                holder.textView.setTextColor(Color.RED);
            }else{
                holder.i.setVisibility(View.GONE);
                holder.textView.setTextColor(Color.BLACK);
            }
            holder.textView.setText(list.get(position).dirName);
        if(onitemClickListener!=null){
            holder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    onitemClickListener.setOnitemClickListener(position);

                }
            });
        }
    }

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

    class Vh extends RecyclerView.ViewHolder{
        TextView textView;
        ImageView i;
        public Vh(View itemView) {
            super(itemView);
            textView= (TextView) itemView.findViewById(R.id.left_textView);
            i=(ImageView)itemView.findViewById(R.id.imageView);
        }
    }
}
//右边的fragment
package fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import adpter.AdpterRight;
import bawei.sunyubo20161222.R;
import bean.Bean;

/**
 * 1.类的用途
 * 2.@author:Sunyubo
 * 3.@ 2016/12/22.
 */

public class Right_fragment extends Fragment {


    private RecyclerView gv;
    private List<Bean.RsBean.ChildrenBean> list=new ArrayList<>();
    private AdpterRight ad;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.right_fragment,null);
        gv = (RecyclerView)view.findViewById(R.id.gridView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return list.get(position).isHeader?3:1;
            }
        });
        gv.setLayoutManager(gridLayoutManager);
        ad = new AdpterRight(getActivity(),list);
        gv.setAdapter(ad);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void upList(List<Bean.RsBean.ChildrenBean> listall) {
        list.clear();
        list.addAll(listall);
      //  Toast.makeText(getActivity(), list.toString(), Toast.LENGTH_SHORT).show();
        //System.out.println(list.toString());
        ad.notifyDataSetChanged();
    }
}
//右边fragment的适配器
package adpter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import bawei.sunyubo20161222.R;
import bean.Bean;
import myclick.MyOnitemClickListener;
/**
 * 1.类的用途
 * 2.@author:Sunyubo
 * 3.@ 2016/12/22.
 */

public class AdpterRight extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    List<Bean.RsBean.ChildrenBean> list;
    public static final int HEADER=1;
    public static final int CONTENT=2;
    public AdpterRight(Context context, List<Bean.RsBean.ChildrenBean> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==HEADER){
            //头
           return new Vh1(LayoutInflater.from(context).inflate(R.layout.item1_right,parent,false));
        }else if(viewType==CONTENT){
            //内容
           return new Vh2(LayoutInflater.from(context).inflate(R.layout.item_right,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof Vh1){
            ((Vh1) holder).t1.setText(list.get(position).dirName);
        }
        if(holder instanceof  Vh2){
            ((Vh2) holder).t2.setText(list.get(position).dirName);
            Glide.with(context).load(list.get(position).imgApp).into(((Vh2) holder).i);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(list.get(position).isHeader){
            return HEADER;
        }else{
            return CONTENT;
        }

    }

    MyOnitemClickListener onitemClickListener;
public void setOnitemClickListener( MyOnitemClickListener onitemClickListener){

    this.onitemClickListener=onitemClickListener;
}

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

    class Vh1 extends RecyclerView.ViewHolder{
        TextView t1;
        public Vh1(View itemView) {
            super(itemView);
            t1=(TextView)itemView.findViewById(R.id.item1_right_name);
        }
    }
    class Vh2 extends RecyclerView.ViewHolder{
    TextView t2;
        ImageView i;
        public Vh2(View itemView) {
            super(itemView);
            t2=(TextView)itemView.findViewById(R.id.name);
            i=(ImageView)itemView.findViewById(R.id.imgView2);
        }
    }
}
//bean
package bean;

import java.io.Serializable;
import java.util.List;

/**
 * 1.类的用途
 * 2.@author:Sunyubo
 * 3.@ 2016/12/22.
 */

public class Bean implements Serializable{


    public String message;
    public int status;
    public List<RsBean> rs;

    public static class RsBean implements Serializable{


        public boolean check;
        public String dirName;
        public int id;
        public List<ChildrenBean> children;


            public static class ChildrenBean implements Serializable{

                public String dirName;
                public int id;
                public String imgApp;
                public boolean isHeader;
                public List<ChildrenBean> children;

                @Override
                public String toString() {
                    return "ChildrenBean{" +
                            "dirName='" + dirName + '\'' +
                            ", id=" + id +
                            ", imgApp='" + imgApp + '\'' +
                            ", isHeader=" + isHeader +
                            ", children=" + children +
                            '}';
                }
            }
    }
}
//如果运行没有炸掉,给兄弟点个赞,我会一直更新csdn。。。。
![这里写图片`这里写代码片`描述](http://img.blog.csdn.net/20170103212152291?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzY1MDgwMjQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

静态页面的传值

Fragment页面之间传值、基本例子演示、生命周期、兼容性

一、Fragment起初: 3.0以后版本推出的Fragment 二、Fragment之间的通讯: 程序要求:创建一个侧边栏效果的fragment布局,左边是新闻分类列表,右面是没一个模块的内容。 ...

在静态页面html中跳转传值

在html中通过"?"传值--------静态传值 在跳转到的页面index2.html中接收----var name=UrlParm.parm("name"); 代码如下: index...

javascript静态页面间的传值和接收

一:JavaScript静态页面值传递之URL篇 能过URL进行传值.把要传递的信息接在URL上. 例子: 参数传出页面Post.htm—>   function Post(...

javascript静态页面传值的三种方法

一:JavaScript静态页面值传递之URL篇  能过URL进行传值.把要传递的信息接在URL上.Post.htm复制内容到剪贴板代码:fun

使用RecyclerView商品左右分类列表

最近自己想写一个购物类App 所以就模仿着网易严选的样式去写  今天刚好也写到分类这一块 看着这样的样式前面自己是想用两个LIstView去完成 后面想了想打算用 RecyclerView写 ,然后在...

高仿京东分类效果(Scroll+Fragment)

实现思路:首先说下布局,整个是一个横向的线性布局,左边是一个ScrollView,右边是一个FrameLayout, 在代码中动态向ScrollView中添加TextView,然后根据TextView...

【项目经验】——ASP.NET页面间传值

最近项目中涉及到很多页面传值的内容,今天,就和大家分享一下最常用的集中ASP.NET页面间传值的几种方法。一、QueryString传值 QueryString是平常传值中用的比较多的一种方式,使用...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)