<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.youth.banner.Banner
android:id="@+id/home_banner"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_margin="14dp"
app:image_scale_type="fit_xy" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/home_recy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="14dp"
android:layout_marginRight="14dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="14dp"
android:layout_marginBottom="7dp"
android:text="专题" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/huise" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/home_l"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginRight="7dp"
android:layout_weight="1"
android:background="@drawable/news"
android:orientation="vertical">
<ImageView
android:id="@+id/home_l_img"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="fitXY" />
<TextView
android:id="@+id/home_l_txv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_marginBottom="7dp"
android:ellipsize="end"
android:maxLines="2"
android:text="123" />
</LinearLayout>
<LinearLayout
android:id="@+id/home_r"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:layout_marginRight="14dp"
android:layout_weight="1"
android:background="@drawable/news"
android:orientation="vertical">
<ImageView
android:id="@+id/home_r_img"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="fitXY" />
<TextView
android:id="@+id/home_r_txv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_marginBottom="7dp"
android:ellipsize="end"
android:maxLines="2"
android:text="123" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="14dp"
android:background="@color/huise" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="14dp"
android:orientation="horizontal">
<TextView
android:id="@+id/home_l1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
<TextView
android:id="@+id/home_l2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
<TextView
android:id="@+id/home_l3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
<TextView
android:id="@+id/home_l4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
<TextView
android:id="@+id/home_l5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
<TextView
android:id="@+id/home_l6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
</LinearLayout>
<LinearLayout
android:id="@+id/home_linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="14dp"
android:layout_marginBottom="5dp"
android:orientation="vertical">
<ListView
android:id="@+id/home_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.test1218.Const;
import com.example.test1218.MainActivity;
import com.example.test1218.news.NewsInfo;
import park.Park;
import com.example.test1218.R;
import com.example.test1218.banner.GBData;
import com.example.test1218.fengzhuang.Height;
import com.example.test1218.fengzhuang.MySerAdapter;
import com.example.test1218.fengzhuang.Okhehe;
import com.example.test1218.news.NewsBean;
import com.example.test1218.news.NewsData;
import com.example.test1218.news.NfData;
import com.example.test1218.service.SerBean;
import com.example.test1218.service.SerData;
import com.example.test1218.waimaiFragment.waimai;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class HomeFragment extends Fragment {
View view;
Banner banner;
Okhehe hehe;
String qianzhui;
List<String> img = new ArrayList<>();
List<NewsBean> l1 = new ArrayList<>(), l2 = new ArrayList<>(), l3 = new ArrayList<>();
List<SerBean> s1 = new ArrayList<>(), s2 = new ArrayList<>();
TextView t1, t2, t3, t4, t5, t6;
TextView ts[] = new TextView[]{t1, t2, t3, t4, t5, t6};
int tsId[] = new int[]{R.id.home_l1, R.id.home_l2, R.id.home_l3, R.id.home_l4, R.id.home_l5, R.id.home_l6};
ListView listView;
LinearLayout linearLayout;
LinearLayout l, r;
TextView l_t, r_t;
ImageView l_i, r_i;
RecyclerView recy;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.home_fragment, container, false);
banner = view.findViewById(R.id.home_banner);
l = view.findViewById(R.id.home_l);
r = view.findViewById(R.id.home_r);
l_t = view.findViewById(R.id.home_l_txv);
r_t = view.findViewById(R.id.home_r_txv);
l_i = view.findViewById(R.id.home_l_img);
r_i = view.findViewById(R.id.home_r_img);
recy = view.findViewById(R.id.home_recy);
hehe = Okhehe.getInstance();
qianzhui = getActivity().getSharedPreferences("Myshujv", Context.MODE_PRIVATE)
.getString("qianzhui", "");
String url = qianzhui + "/prod-api/api/rotation/list?type=2";
hehe.GetString(url, new Okhehe.FuncString() {
@Override
public void onResponse(String js) {
GBData gb = new Gson().fromJson(js, GBData.class);
img.clear();
for (int i = 0; i < gb.getRows().size(); i++) {
img.add(qianzhui + gb.getRows().get(i).getAdvImg());
}
banner.setImages(img)
.setImageLoader(new MyImgLoader())
.start();
}
});
listView = view.findViewById(R.id.home_list);
linearLayout = view.findViewById(R.id.home_linear);
PutMyNews();
PutMySer();
return view;
}
private void PutMySer() {
String url = qianzhui + "/prod-api/api/service/list";
hehe.GetString(url, new Okhehe.FuncString() {
@Override
public void onResponse(String js) {
s1.clear();
SerData sd = new Gson().fromJson(js, SerData.class);
for (int i = 0; i < sd.getRows().size(); i++) {
s1.add(sd.getRows().get(i));
}
Comparator<SerBean> comparator = new Comparator<SerBean>() {
@Override
public int compare(SerBean serBean, SerBean t1) {
if (serBean.getId() != t1.getId()) {
return t1.getId() - serBean.getId();
} else {
return t1.getId() - serBean.getId();
}
}
};
Collections.sort(s1, comparator);
s2.clear();
for (int i = 0; i < 7; i++) {
s2.add(s1.get(i));
}
SerBean sb = new SerBean();
sb.setServiceName("更多");
s2.add(sb);
recy.setAdapter(new MySerAdapter(getActivity(), s2, qianzhui));
GridLayoutManager gd = new GridLayoutManager(getActivity(), 4);
recy.setLayoutManager(gd);
recy.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View v7 = recy.getLayoutManager().findViewByPosition(7);
ImageView imageView = v7.findViewById(R.id.ser_item_img);
imageView.setBackgroundResource(R.drawable.yuan_lanse);
v7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((MainActivity) getActivity()).select(1);
}
});
View v6 = recy.getLayoutManager().findViewByPosition(6);
v6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getActivity(), Park.class));
}
});
View v4 = recy.getLayoutManager().findViewByPosition(4);
v4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getActivity(), waimai.class));
}
});
recy.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
});
}
public class MyImgLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object o, ImageView imageView) {
Glide.with(context).load(o).into(imageView);
}
}
private void PutMyNews() {
String url = qianzhui + "/prod-api/press/press/list";
hehe.GetString(url, new Okhehe.FuncString() {
@Override
public void onResponse(String js) {
NewsData nd = new Gson().fromJson(js, NewsData.class);
l1.clear();
for (int i = 0; i < nd.getRows().size(); i++) {
l1.add(nd.getRows().get(i));
}
PutMyNF();
PutMyTwoNews(l1);
}
});
}
private void PutMyTwoNews(final List<NewsBean> newsBeans) {
l3.clear();
for (int i = 0; i < newsBeans.size(); i++) {
if (newsBeans.get(i).getHot().equals("Y")) {
l3.add(newsBeans.get(i));
}
}
Glide.with(getActivity()).load(qianzhui + newsBeans.get(0).getCover()).into(l_i);
Glide.with(getActivity()).load(qianzhui + newsBeans.get(1).getCover()).into(r_i);
l_t.setText(Html.fromHtml(newsBeans.get(0).getContent()));
r_t.setText(Html.fromHtml(newsBeans.get(1).getContent()));
l.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Const.newsBean = newsBeans.get(0);
startActivity(new Intent(getActivity(), NewsInfo.class));
}
});
r.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Const.newsBean = newsBeans.get(1);
startActivity(new Intent(getActivity(), NewsInfo.class));
}
});
}
private void PutMyNF() {
String url = qianzhui + "/prod-api/press/category/list";
hehe.GetString(url, new Okhehe.FuncString() {
@Override
public void onResponse(String js) {
final NfData nf = new Gson().fromJson(js, NfData.class);
for (int i = 0; i < ts.length; i++) {
ts[i] = view.findViewById(tsId[i]);
ts[i].setText(nf.getData().get(i).getName());
if (i == 0) {
l2.clear();
for (int s = 0; s < l1.size(); s++) {
if (l1.get(s).getType() != null) {
if (l1.get(s).getType().equals(String.valueOf(nf.getData().get(0).getId()))) {
l2.add(l1.get(s));
}
}
}
PutMyNewView(l2);
}
final int c = i;
ts[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
l2.clear();
for (int s = 0; s < l1.size(); s++) {
if (l1.get(s).getType() != null) {
if (l1.get(s).getType().equals(String.valueOf(nf.getData().get(c).getId()))) {
l2.add(l1.get(s));
}
}
}
PutMyNewView(l2);
}
});
}
}
});
}
private void PutMyNewView(final List<NewsBean> newsBeans) {
MyListAdapter adapter = new MyListAdapter(newsBeans);
listView.setAdapter(adapter);
Height ht = new Height();
ht.height(linearLayout, listView, adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Const.newsBean = newsBeans.get(i);
startActivity(new Intent(getActivity(), NewsInfo.class));
}
});
}
private class MyListAdapter extends BaseAdapter {
List<NewsBean> newsBeans;
public MyListAdapter(List<NewsBean> newsBeans) {
this.newsBeans = newsBeans;
}
@Override
public int getCount() {
return newsBeans.size();
}
@Override
public Object getItem(int i) {
return newsBeans.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v1 = View.inflate(getActivity(), R.layout.list_style, null);
ImageView imageView = v1.findViewById(R.id.news_img);
TextView title = v1.findViewById(R.id.news_title);
TextView nr = v1.findViewById(R.id.news_nr);
TextView time = v1.findViewById(R.id.news_time);
TextView pl = v1.findViewById(R.id.news_pl);
Glide.with(getActivity()).load(qianzhui + newsBeans.get(i).getCover()).into(imageView);
title.setText(newsBeans.get(i).getTitle());
nr.setText(Html.fromHtml(newsBeans.get(i).getContent()));
time.setText(newsBeans.get(i).getPublishDate());
pl.setText(String.valueOf(newsBeans.get(i).getCommentNum()));
return v1;
}
}
}