home

本文介绍了一个基于安卓的应用中热榜与问答功能的具体实现方式。通过OkHttp发起网络请求获取热榜数据并解析JSON,同时展示了如何利用CommonAdapter进行问答列表的展示。文章深入剖析了UI组件与事件响应的绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.yimiao100.sale.yimiaomanager.view.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yimiao100.sale.yimiaomanager.R;
import com.yimiao100.sale.yimiaomanager.base.BaseFragment;
import com.yimiao100.sale.yimiaomanager.net.AppConst;
import com.yimiao100.sale.yimiaomanager.view.MyListVIew;
import com.yimiao100.sale.yimiaomanager.view.activity.SearchActivity;
import com.yimiao100.sale.yimiaomanager.view.adapter.CommonAdapter;
import com.yimiao100.sale.yimiaomanager.view.adapter.RecyclerAdapter;
import com.yimiao100.sale.yimiaomanager.view.adapter.ViewHolder;
import com.yimiao100.sale.yimiaomanager.view.bean.HotBean;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


/**
 * Created by Michel on 2018/7/23.
 */
public class HomeFragment extends BaseFragment {
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.img_share)
    ImageView imgShare;
    @BindView(R.id.img_home1)
    ImageView imgHome1;
    @BindView(R.id.text_home1)
    TextView textHome1;
    @BindView(R.id.img_pl)
    ImageView imgPl;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.home_hot_Rl1)
    RelativeLayout homeHotRl1;
    @BindView(R.id.img_home2)
    ImageView imgHome2;
    @BindView(R.id.text_home2)
    TextView textHome2;
    @BindView(R.id.img_p2)
    ImageView imgP2;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.home_hot_Rl2)
    RelativeLayout homeHotRl2;
    @BindView(R.id.img_home3)
    ImageView imgHome3;
    @BindView(R.id.text_home3)
    TextView textHome3;
    @BindView(R.id.img_pl3)
    ImageView imgPl3;
    @BindView(R.id.text3)
    TextView text3;
    @BindView(R.id.home_hot_Rl3)
    RelativeLayout homeHotRl3;
    @BindView(R.id.img_home4)
    ImageView imgHome4;
    @BindView(R.id.text_home4)
    TextView textHome4;
    @BindView(R.id.img_pl4)
    ImageView imgPl4;
    @BindView(R.id.text5)
    TextView text5;
    @BindView(R.id.home_hot_Rl4)
    RelativeLayout homeHotRl4;
    Unbinder unbinder;
    @BindView(R.id.listview)
    MyListVIew listview;
    private ArrayList<String> list;
    private LinearLayoutManager layoutManager;
    private RecyclerAdapter recyclerAdapter;
    private HotBean hotBean;

    //热榜图片
    ArrayList<ImageView> ivlist = new ArrayList<>();
    //热榜文字
    ArrayList<TextView> tvlist = new ArrayList<>();
    private AllQusetion allqusetion;

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String str = (String) msg.obj;
            Gson gson = new Gson();
            hotBean = gson.fromJson(str, HotBean.class);
            Log.i("111", "111" + hotBean);
            initRebang(hotBean);

        }
    };
    @SuppressLint("HandlerLeak")
    Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Gson gson = new Gson();
            String stri = (String) msg.obj;
            allqusetion = gson.fromJson(stri, AllQusetion.class);
            //抢答
            initqiangda(allqusetion);
        }
    };

    private void initqiangda(AllQusetion allqusetion) {
        List<AllQusetion.PagingBean.PagedListBean> pagedList = allqusetion.getPaging().getPagedList();
        CommonAdapter commonA = new CommonAdapter<AllQusetion.PagingBean.PagedListBean>(getActivity(), pagedList, R.layout.home_listview) {
            @Override
            public void convert(ViewHolder helper, AllQusetion.PagingBean.PagedListBean item) {

                helper.setText(R.id.text_expand, item.getDescription());
                helper.setText(R.id.text_title, item.getDescription());
                helper.setImageByUrl(R.id.img_cir, item.getQuestionerProfileImageUrl());

            }

        };
        listview.setAdapter(commonA);

    }

    private FormBody requestBody;
    private String data;
    private String data1;


    private void initRebang(HotBean hotBean) {
        List<HotBean.HotListBean> hotList = hotBean.getHotList();
        ivlist.add(imgHome1);
        ivlist.add(imgHome2);
        ivlist.add(imgHome3);
        ivlist.add(imgHome4);

        tvlist.add(textHome1);
        tvlist.add(textHome2);
        tvlist.add(textHome3);
        tvlist.add(textHome4);


        for (int i = 0; i < 4; i++) {
            tvlist.get(i).setText(hotList.get(i).getTitle());

        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_home_two;
    }

    @Override
    protected void initData() {
        //适配器里条目的点击事件
        //  initRecycleClick();
        //适配器布局
        //  initRecycle();
        //热榜
        rebang();
        //抢答
        qiangda();
    }

    //抢答
    private void qiangda() {
        OkHttpClient okHttpClient = new OkHttpClient();
        requestBody = new FormBody.Builder()
                .build();
        Request request = new Request.Builder()
                .url(AppConst.BASE_URL + AppConst.suoyouwentiliebiao)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data1 = response.body().string();
                Log.i("888888888888888888888", "" + data1);
                Message msg1 = Message.obtain();
                msg1.obj = data1;
                handler1.sendMessage(msg1);
                HomeFragment.this.data1 = data1;

            }
        });

    }

    private void rebang() {
        OkHttpClient okHttpClient = new OkHttpClient();
        requestBody = new FormBody.Builder()
                .build();
        Request request = new Request.Builder()
                .url(AppConst.BASE_URL + AppConst.wentirebang)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).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();
                Log.i("888888888888888888888", "" + data);
                Message msg = Message.obtain();
                msg.obj = data;
                handler.sendMessage(msg);
                HomeFragment.this.data = data;

            }
        });
    }

    //适配器操作
    private void initRecycle() {

     /*   list = new ArrayList<String>();
        for (int i = 0; i < 30; i++) {
            list.add("你定吧剧看不懂法就开始帮是借款方的博物馆一竟然分比二笔是就爱看付过IE让肌肤扩多是吃面包军付扩多多多多多多多多多多多多多多多多多多多多多多多多多多多多多多多多多多多多大所不绿发VB火辣本来好处费的," + i);
        }
        //布局管理器
        layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler.setLayoutManager(layoutManager);
        //绑定适配器
        recyclerAdapter = new RecyclerAdapter<String, String, String>(getActivity(), R.layout.home_listview, list, 0, null, 0, null) {

            private ExpandTextView text_expand;

            @Override
            public void convertBody(RecycleHolder holder, String data, int position) {
                int whidth = ScreenUtils.getScreenWidth(getActivity()) - ScreenUtils.dp2px(getActivity(), 16 * 2);
                text_expand = holder.findView(R.id.text_expand);
                text_expand.initWidth(whidth);
                text_expand.setMaxLines(3);
                text_expand.setCloseText(list.get(position));
            }
        };
        recycler.setAdapter(recyclerAdapter);*/

    }

 /*   private void initRecycleClick() {
    }*/


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

   /* @OnClick({R.id.edit_search, R.id.img_share, R.id.img_home1, R.id.text_home1, R.id.img_pl, R.id.text, R.id.home_hot_Rl1, R.id.img_home2, R.id.text_home2, R.id.img_p2, R.id.text1, R.id.home_hot_Rl2, R.id.img_home3, R.id.text_home3, R.id.img_pl3, R.id.text3, R.id.home_hot_Rl3, R.id.img_home4, R.id.text_home4, R.id.img_pl4, R.id.text5, R.id.home_hot_Rl4, R.id.listview})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edit_search:
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.img_share:
                break;
            case R.id.img_home1:
                break;
            case R.id.text_home1:
                break;
            case R.id.img_pl:
                break;
            case R.id.text:
                break;
            case R.id.home_hot_Rl1:
                break;
            case R.id.img_home2:
                break;
            case R.id.text_home2:
                break;
            case R.id.img_p2:
                break;
            case R.id.text1:
                break;
            case R.id.home_hot_Rl2:
                break;
            case R.id.img_home3:
                break;
            case R.id.text_home3:
                break;
            case R.id.img_pl3:
                break;
            case R.id.text3:
                break;
            case R.id.home_hot_Rl3:
                break;
            case R.id.img_home4:
                break;
            case R.id.text_home4:
                break;
            case R.id.img_pl4:
                break;
            case R.id.text5:
                break;
            case R.id.home_hot_Rl4:
                break;
            case R.id.listview:
                break;
        }
    }*/


//    private void dialogShow2() {
//        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//        LayoutInflater inflater = LayoutInflater.from(getActivity());
//        View v = inflater.inflate(R.layout.submit_anser, null);
//
//        Button btn_cancel = (Button) v.findViewById(R.id.dialog_btn_cancel);
//        //builer.setView(v);//这里如果使用builer.setView(v),自定义布局只会覆盖title和button之间的那部分
//        final Dialog dialog = builder.create();
//        dialog.show();
//        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//       //弹出输入框
//        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//
//        dialog.getWindow().setContentView(v);//自定义布局应该在这里添加,要在dialog.show()的后面
//        //dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置
//
//        btn_cancel.setOnClickListener(new View.OnClickListener() {
//
//            @SuppressLint({"WrongConstant", "ShowToast"})
//            @Override
//            public void onClick(View arg0) {
//                dialog.dismiss();
//                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//                LayoutInflater inflater = LayoutInflater.from(getActivity());
//                View v = inflater.inflate(R.layout.cakan_anser, null);
//                final Dialog dialog = builder.create();
//                dialog.show();
//                dialog.getWindow().setContentView(v);
//            }
//        });
//    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值