实现首页界面

package com.example.login_register;

import static baseinfo.Connectinfo.contexturl;
import static baseinfo.Connectinfo.lunbotuurl;
import static baseinfo.Connectinfo.newstitleurl;
import static baseinfo.Connectinfo.newsurl;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

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

import Lunbotu.LunbotuData;
import NewsTitle.NewsTitleData;
import NewsTitle.NewsTitleDataBean;
import Redianzhuanti.Redian_left;
import Redianzhuanti.Redian_right;
import Xinwen.Const;
import Xinwen.XinwenData;
import Xinwen.XinwenDataBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Home extends Fragment {
    Banner banner;
    ListView listView;
    LinearLayout list_linear;
    OkHttpClient okHttpClient;
    Handler handler;
    ArrayList<String> lunbotulist = new ArrayList<>();
    ArrayList<XinwenDataBean> alldata = new ArrayList<>();
    ArrayList<XinwenDataBean> tabdata = new ArrayList<>();
    XinwenData xinwenData;
    TextView t1, t2, t3, t4, t5, t6;
    TextView[] titlelist = {t1, t2, t3, t4, t5, t6};
    int[] title_id = {R.id.tv1, R.id.tv2, R.id.tv3, R.id.tv4, R.id.tv5, R.id.tv6};
    //专题新闻文本
    LinearLayout left_news, right_news;
    TextView redain_left_info, redain_right_info;
    TextView[] arr_redian_info = {redain_left_info, redain_right_info};
    int[] arr_redain_textid = {R.id.redain_left_info, R.id.redain_right_info};
    ImageView redain_left_img, redain_right_img;
    ImageView[] arr_redian_img = {redain_left_img, redain_right_img};
    int[] arr_redian_imgid = {R.id.redain_left, R.id.redain_right};

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home, container, false);
        banner = view.findViewById(R.id.banner);
        listView = view.findViewById(R.id.list);
        list_linear = view.findViewById(R.id.list_linear);
        handler = new Handler(Looper.getMainLooper());
        //初始化专题文本
        left_news = view.findViewById(R.id.left_layout);
        right_news = view.findViewById(R.id.right_layout);
        for (int i = 0; i < arr_redain_textid.length; i++) {
            arr_redian_info[i] = view.findViewById(arr_redain_textid[i]);
            arr_redian_img[i] = view.findViewById(arr_redian_imgid[i]);
        }
        //初始化导航栏文本
        for (int i = 0; i < titlelist.length; i++) {
            titlelist[i] = view.findViewById(title_id[i]);
        }
        lunbotu();//轮播图
        newslist();//标题下的所有新闻
        return view;
    }

    //轮播图
    private void lunbotu() {

        Request request = new Request.Builder()
                .url(lunbotuurl)
                .build();
        okHttpClient = new OkHttpClient.Builder().build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        LunbotuData lunbotudata = new Gson().fromJson(data, LunbotuData.class);
                        lunbotulist.clear();
                        for (int i = 0; i < lunbotudata.getRows().size(); i++) {
                            lunbotulist.add(contexturl + lunbotudata.getRows().get(i).getAdvImg());
                        }
                        banner.setImageLoader(new ImageLoader() {
                            @Override
                            public void displayImage(Context context, Object path, ImageView imageView) {
                                Glide.with(context)
                                        .load(path)
                                        .into(imageView);
                            }
                        })
                                .setDelayTime(2000)
                                .setImages(lunbotulist)
                                .start();
                    }
                });
            }
        });
    }

    //热点新闻
    private void redianxinwen() {
        //将两个热点新闻放入集合中
        ArrayList<String> redian_img = new ArrayList<>();
        ArrayList<String> redian_title = new ArrayList<>();
        ArrayList<String> redian_info = new ArrayList<>();
        for (int i = 0; i < xinwenData.getRows().size(); i++) {
            if (xinwenData.getRows().get(i).getHot().equals("Y")) {
                redian_title.add(xinwenData.getRows().get(i).getTitle());
                redian_info.add(xinwenData.getRows().get(i).getContent());
                redian_img.add(contexturl + xinwenData.getRows().get(i).getCover());
            }
        }
        Const.redian_img = redian_img;
        Const.redian_info = redian_info;
        Const.redian_title = redian_title;
        //加载主页面专题新闻
        for (int i = 0; i < redian_img.size(); i++) {
            Glide.with(getActivity())
                    .load(redian_img.get(i))
                    .into(arr_redian_img[i]);
            arr_redian_info[i].setText(Html.fromHtml(redian_info.get(i)));
        }
        //左侧专题新闻监听
        left_news.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), Redian_left.class));
            }
        });
        //右侧专题新闻监听
        right_news.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), Redian_right.class));
            }
        });
    }

    //新闻标题
    private void newstitle() {
        Request request = new Request.Builder()
                .url(newstitleurl)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String data = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        NewsTitleData newsTitleData = new Gson().fromJson(data, NewsTitleData.class);
                        for (int i = 0; i < newsTitleData.getData().size(); i++) {
                            titlelist[i].setText(newsTitleData.getData().get(i).getName());
                            if (i == 0) {
                                tabdata.clear();
                                for (int j = 0; j < alldata.size(); j++) {
                                    if (alldata.get(j).getType() != null) {
                                        if (alldata.get(j).getType().equals(String.valueOf(newsTitleData.getData().get(0).getId()))) {
                                            tabdata.add(alldata.get(j));
                                        }
                                    }
                                }
                                PutListview(tabdata);
                            }
                            final int finalI = i;
                            titlelist[i].setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    tabdata.clear();
                                    for (int j = 0; j < alldata.size() - 1; j++) {
                                        if (alldata.get(j).getType() != null) {
                                            if (alldata.get(j).getType().equals(String.valueOf(newsTitleData.getData().get(finalI).getId()))) {
                                                tabdata.add(alldata.get(j));
                                            }
                                        }
                                    }
                                    PutListview(tabdata);
                                    for (int a = 0; a < titlelist.length; a++) {
                                        if (finalI == a) {
                                            titlelist[a].setTextColor(Color.BLUE);
                                        } else {
                                            titlelist[a].setTextColor(Color.GRAY);
                                        }
                                    }
                                }

                            });
                        }
                    }
                });
            }
        });
    }

    //获取新闻导航列表数据
    private void newslist() {
        Request request = new Request.Builder()
                .url(newsurl)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String newsdata = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (response != null && response.isSuccessful()) {
                            xinwenData = new Gson().fromJson(newsdata, XinwenData.class);
                            //将所有数据添加到alltada中
                            for (int i = 0; i < xinwenData.getRows().size(); i++) {
                                alldata.add(xinwenData.getRows().get(i));
                            }
                            Log.i("alldata数据:", String.valueOf(alldata));
                            //第一次进入加载新闻
                            newstitle();//新闻六个标题
                            redianxinwen();
                        }
                    }

                });
            }
        });
    }

    //处理各个列表项
    private void PutListview(ArrayList<XinwenDataBean> tabdata) {
        listView.setAdapter(new Myadapter(tabdata));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Const.xinwenDataBean = tabdata.get(i);
                Intent intent = new Intent(getActivity(), News_List.class);
                startActivity(intent);
            }
        });
        ListHeight(tabdata);
    }

    //新闻列表适配器
    public class Myadapter extends BaseAdapter {
        ArrayList<XinwenDataBean> list;

        public Myadapter(ArrayList<XinwenDataBean> list) {
            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) {
            View view2 = View.inflate(getActivity(), R.layout.news_list, null);
            ImageView iv = view2.findViewById(R.id.iv);
            TextView title = view2.findViewById(R.id.newstitle);
            TextView neirong = view2.findViewById(R.id.neirong);
            TextView updataTime = view2.findViewById(R.id.updataTime);
            TextView commentNum = view2.findViewById(R.id.commentNum);
            Glide.with(getActivity())
                    .load(contexturl + tabdata.get(i).getCover())
                    .into(iv);
            title.setText(Html.fromHtml(tabdata.get(i).getTitle()));
            neirong.setText(Html.fromHtml(tabdata.get(i).getContent()));
            updataTime.setText(tabdata.get(i).getUpdateTime());
            commentNum.setText(String.valueOf(tabdata.get(i).getCommentNum()));
            return view2;
        }
    }

    //处理列表项大小
    private void ListHeight(ArrayList<XinwenDataBean> tabdata) {
        Myadapter myadapter = new Myadapter(tabdata);
        if (myadapter == null) {
            return;
        }
        int listHeight = 0;
        for (int i = 0; i < myadapter.getCount(); i++) {
            View listItem = myadapter.getView(i, null, listView);
            listItem.measure(0, 0);
            listHeight += listItem.getMeasuredHeight();
        }
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(list_linear.getLayoutParams());
        params.height = listHeight + (listView.getDividerHeight() * (myadapter.getCount() - 1)) + 10;
        listView.setLayoutParams(params);
    }
}

xml

<?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">

    <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/banner"
                android:layout_width="match_parent"
                android:layout_height="200dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="专题"
                android:textSize="20dp" />

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

                <LinearLayout
                    android:id="@+id/left_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="10dp"
                    android:layout_weight="1"
                    android:background="@drawable/huisexixian"
                    android:orientation="vertical">

                    <ImageView
                        android:scaleType="fitXY"
                        android:id="@+id/redain_left"
                        android:layout_width="match_parent"
                        android:layout_height="100dp" />

                    <TextView
                        android:singleLine="true"
                        android:id="@+id/redain_left_info"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/right_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="10dp"
                    android:layout_weight="1"
                    android:background="@drawable/huisexixian"
                    android:orientation="vertical">

                    <ImageView
                        android:scaleType="fitXY"
                        android:id="@+id/redain_right"
                        android:layout_width="match_parent"
                        android:layout_height="100dp" />

                    <TextView
                        android:singleLine="true"
                        android:id="@+id/redain_right_info"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </LinearLayout>
            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tv1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="#0647EF"
                    android:text="今日要闻" />

                <TextView
                    android:id="@+id/tv2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="今日要闻" />

                <TextView
                    android:id="@+id/tv3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="今日要闻" />

                <TextView
                    android:id="@+id/tv4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="今日要闻" />

                <TextView
                    android:id="@+id/tv5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="今日要闻" />

                <TextView
                    android:id="@+id/tv6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="今日要闻" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/list_linear"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="10dp"
                android:orientation="vertical">

                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

以上为Java代码

布局界面如下所示

<?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">

    <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/banner"
                android:layout_width="match_parent"
                android:layout_height="200dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="专题"
                android:textSize="20dp" />

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

                <LinearLayout
                    android:id="@+id/left_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="10dp"
                    android:layout_weight="1"
                    android:background="@drawable/huisexixian"
                    android:orientation="vertical">

                    <ImageView
                        android:scaleType="fitXY"
                        android:id="@+id/redain_left"
                        android:layout_width="match_parent"
                        android:layout_height="100dp" />

                    <TextView
                        android:singleLine="true"
                        android:id="@+id/redain_left_info"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/right_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="10dp"
                    android:layout_weight="1"
                    android:background="@drawable/huisexixian"
                    android:orientation="vertical">

                    <ImageView
                        android:scaleType="fitXY"
                        android:id="@+id/redain_right"
                        android:layout_width="match_parent"
                        android:layout_height="100dp" />

                    <TextView
                        android:singleLine="true"
                        android:id="@+id/redain_right_info"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </LinearLayout>
            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tv1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="今日要闻" />

                <TextView
                    android:id="@+id/tv2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="今日要闻" />

                <TextView
                    android:id="@+id/tv3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="今日要闻" />

                <TextView
                    android:id="@+id/tv4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="今日要闻" />

                <TextView
                    android:id="@+id/tv5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="今日要闻" />

                <TextView
                    android:id="@+id/tv6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="今日要闻" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/list_linear"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="10dp"
                android:orientation="vertical">

                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值