package com.bwie.jingdong.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import com.bwie.jingdong.R;
import com.bwie.jingdong.activity.SuoSouActivity;
import com.bwie.jingdong.adapter.ZuoFenLeiAdapter;
import com.bwie.jingdong.bean.FenLeiBean;
import com.bwie.jingdong.util.ApiUtil;
import com.bwie.jingdong.util.CommonUtils;
import com.bwie.jingdong.util.OkHttp3Util;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by lenovo on 2017/12/27.
*/
public class FragmenFenLei extends Fragment {
private ImageView sousuo;
private ListView lv;
private FrameLayout frame1;
private FrameLayout frame2;
private ZuoFenLeiAdapter zuoFenLeiAdapter;
private static int totalHeight = 0;//ListView高度
public static int mPostion;
private ZiFenLeiFragment ziFenLeiFragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fenlei, container, false);
sousuo = view.findViewById(R.id.sousuo);
lv = view.findViewById(R.id.lv);
// frame1 = view.findViewById(R.id.frame1);
frame2 = view.findViewById(R.id.frame2);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
sousuo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), SuoSouActivity.class);
startActivity(intent);
}
});
zuo();
}
private void zuo() {
OkHttp3Util.doGet(ApiUtil.FENLEIURL, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
final String json=response.body().string();
CommonUtils.runOnUIThread(new Runnable() {
@Override
public void run() {
FenLeiBean fenLeiBean = new Gson().fromJson(json, FenLeiBean.class);
List<FenLeiBean.DataBean> data = fenLeiBean.getData();
zuoFenLeiAdapter = new ZuoFenLeiAdapter(getActivity(), data);
lv.setAdapter(zuoFenLeiAdapter);
}
});
}
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mPostion=i;
//点击listViewitem回滚居中
totalHeight = lv.getMeasuredHeight()-120;
lv.smoothScrollToPositionFromTop(mPostion,totalHeight/2,50);
zuoFenLeiAdapter.notifyDataSetChanged();
ziFenLeiFragment = new ZiFenLeiFragment();
ziFenLeiFragment.getCid(mPostion);
// System.out.println("==cid=="+mPostion);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frame2, ziFenLeiFragment).commit();
}
});
}
}
package com.bwie.jingdong.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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.bwie.jingdong.R;
import com.bwie.jingdong.adapter.YuoFenLeiAdapter;
import com.bwie.jingdong.bean.ZiFenLeiBean;
import com.bwie.jingdong.util.ApiUtil;
import com.bwie.jingdong.util.CommonUtils;
import com.bwie.jingdong.util.OkHttp3Util;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by lenovo on 2017/12/27.
*/
public class ZiFenLeiFragment extends Fragment {
private RecyclerView rv;
private int cid;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_zifenlei, container, false);
rv = view.findViewById(R.id.rv);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Intent intent = getActivity().getIntent();
rv.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
OkHttp3Util.doGet(ApiUtil.ZIFENLEIURL +"?"+ cid, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
final String json = response.body().string();
CommonUtils.runOnUIThread(new Runnable() {
@Override
public void run() {
ZiFenLeiBean ziFenLeiBean = new Gson().fromJson(json, ZiFenLeiBean.class);
List<ZiFenLeiBean.DataBean> data = ziFenLeiBean.getData();
YuoFenLeiAdapter yuoFenLeiAdapter = new YuoFenLeiAdapter(getActivity(), data);
rv.setAdapter(yuoFenLeiAdapter);
}
});
}
}
});
}
public void getCid(int postion){
cid = postion;
}
}
package com.bwie.jingdong.adapter;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.bwie.jingdong.R;
import com.bwie.jingdong.bean.FenLeiBean;
import com.bwie.jingdong.fragment.FragmenFenLei;
import java.util.List;
/**
* Created by lenovo on 2017/12/27.
*/
public class ZuoFenLeiAdapter extends BaseAdapter{
Context context;
List<FenLeiBean.DataBean> list;
public ZuoFenLeiAdapter(Context context, List<FenLeiBean.DataBean> list) {
this.context=context;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view==null){
view=View.inflate(context, R.layout.zuo_fenlei_layout,null);
holder=new ViewHolder();
holder.textView=view.findViewById(R.id.text_name);
view.setTag(holder);
}else {
holder= (ViewHolder) view.getTag();
}
holder.textView.setText(list.get(i).getName());
if (i== FragmenFenLei.mPostion){
holder.textView.setTextColor(Color.RED);
holder.textView.setBackgroundColor(Color.parseColor("#F3F5F7"));
}else {
holder.textView.setTextColor(Color.BLACK);
holder.textView.setBackgroundColor(Color.parseColor("#ffffff"));
}
return view;
}
class ViewHolder{
TextView textView;
}
}
package com.bwie.jingdong.adapter;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.bwie.jingdong.R;
import com.bwie.jingdong.bean.ZiFenLeiBean;
import com.bwie.jingdong.inter.ItemClickListener;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lenovo on 2017/12/27.
*/
public class YuoFenLeiAdapter extends RecyclerView.Adapter<YuoFenLeiAdapter.ViewHolder>{
Context context;
List<ZiFenLeiBean.DataBean> list1=new ArrayList<>();
public YuoFenLeiAdapter(Context context, List<ZiFenLeiBean.DataBean> list1) {
this.context=context;
this.list1=list1;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=View.inflate(context, R.layout.you_fenlei_layout,null);
ViewHolder holder=new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.tv_zifenlei.setText(list1.get(position).getName());
List<ZiFenLeiBean.DataBean.ListBean> list = this.list1.get(position).getList();
if (list!=null){
holder.rv_zifenlei_item.setLayoutManager(new GridLayoutManager(context,4));
ZiZiFenLeiAdapter zizifenleiadapter=new ZiZiFenLeiAdapter(context,list);
holder.rv_zifenlei_item.setAdapter(zizifenleiadapter);
//条目的点击事件
zizifenleiadapter.setItemClickListener(new ItemClickListener() {
@Override
public void setItemClickListener(int position) {
Toast.makeText(context, position + " click", Toast.LENGTH_SHORT).show();
}
@Override
public void setItemLongClickListener(int position) {
}
});
}
}
@Override
public int getItemCount() {
return list1.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private final RecyclerView rv_zifenlei_item;
private final TextView tv_zifenlei;
public ViewHolder(View itemView) {
super(itemView);
tv_zifenlei = itemView.findViewById(R.id.tv_zifenlei);
rv_zifenlei_item = itemView.findViewById(R.id.rv_zifenlei_item);
}
}
}
package com.bwie.jingdong.adapter;
import android.content.Context;
import android.content.Intent;
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 com.bwie.jingdong.R;
import com.bwie.jingdong.activity.LieBiaoActivity2;
import com.bwie.jingdong.bean.ZiFenLeiBean;
import com.bwie.jingdong.inter.ItemClickListener;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lenovo on 2017/12/27.
*/
public class ZiZiFenLeiAdapter extends RecyclerView.Adapter<ZiZiFenLeiAdapter.ViewHolder> {
Context context;
List<ZiFenLeiBean.DataBean.ListBean> list_zizifenlei=new ArrayList<>();
private ItemClickListener itemClickListener;
public ZiZiFenLeiAdapter(Context context, List<ZiFenLeiBean.DataBean.ListBean> list_zizifenlei) {
this.context = context;
this.list_zizifenlei = list_zizifenlei;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.zizifenlei,parent,false);
ViewHolder holder=new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position)
{
Glide.with(context).load(list_zizifenlei.get(position).getIcon()).into(holder.iv_zizifenlei);
System.out.println("===iv_zizifenlei=="+list_zizifenlei.get(position).getIcon());
holder.tv_zizifenlei.setText(list_zizifenlei.get(position).getName());
System.out.println("===tv_zizifenlei=="+list_zizifenlei.get(position).getName());
// 如果设置了回调,则设置点击事件
if (itemClickListener!= null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//int pos = holder.getLayoutPosition();//条目的下标
//mOnItemClickLitener.onItemClick(v, pos);
int pscid = list_zizifenlei.get(position).getPscid();
System.out.println("===pscid=="+pscid);
Intent intent=new Intent(context, LieBiaoActivity2.class);
intent.putExtra("pscid",pscid);
context.startActivity(intent);
}
});
}
}
@Override
public int getItemCount() {
return list_zizifenlei.size();
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener=itemClickListener;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
private final TextView tv_zizifenlei;
private final ImageView iv_zizifenlei;
public ViewHolder(View itemView) {
super(itemView);
iv_zizifenlei = itemView.findViewById(R.id.iv_zizifenlei);
tv_zizifenlei = itemView.findViewById(R.id.tv_zizifenlei);
}
}
}
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/saoyisao"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_margin="5dp"
android:layout_height="match_parent"
android:src="@drawable/saoyisao"/>
<ImageView
android:id="@+id/sousuo"
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="match_parent"
android:src="@drawable/sousuo"/>
<ImageView
android:id="@+id/xiaoxi"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_margin="5dp"
android:layout_height="match_parent"
android:src="@drawable/xiaoxi"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv"
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_height="match_parent"
android:scrollbars="none">
</ListView>
<View
android:layout_width="10dp"
android:layout_height="match_parent"
android:background="#F3F5F7" />
<FrameLayout
android:id="@+id/frame2"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_name"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sssss"/>
</android.support.constraint.ConstraintLayout>
<?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">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="fitXY"
android:src="@drawable/dongtu"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:background="#F3F5F7"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</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"
android:orientation="vertical">
<TextView
android:id="@+id/tv_zifenlei"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aaa"
android:layout_margin="10dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_zifenlei_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"></android.support.v7.widget.RecyclerView>
</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="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center">
<ImageView
android:id="@+id/iv_zizifenlei"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/tv_zizifenlei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:text="dfgfd"/>
</LinearLayout>