1. 程式人生 > >QQ頁面+多條目展示

QQ頁面+多條目展示

Fragment

package com.example.kanghuwei_20181204.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.ListView;

import com.example.kanghuwei_20181204.util.NetUtils;
import com.example.kanghuwei_20181204.R;
import com.example.kanghuwei_20181204.adapter.MyBase;
import com.example.kanghuwei_20181204.bean.ListBean;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

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

public class Frag_01 extends Fragment {
    String url="http://api.expoon.com/AppNews/getNewsList/type/1/p/0";
    private List<ListBean.DataBean>list = new ArrayList<>();
    private ListView lv;
    private MyBase adapter;
    private ImageLoader imageLoadernstance;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_01,container,false);

        lv = view.findViewById(R.id.lv);
        imageLoadernstance = ImageLoader.getInstance();
        adapter = new MyBase(list,getActivity(),imageLoadernstance);
        lv.setAdapter(adapter);

        new MAsynTask().execute(url);
        return view;
    }
    private class MAsynTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            String json = NetUtils.getJson(strings[0]);

            return json;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            ListBean listBean = gson.fromJson(s, ListBean.class);
            List<ListBean.DataBean> datalist = listBean.getData();
            list.addAll(datalist);
            adapter.notifyDataSetChanged();
            adapter.notifyDataSetChanged();
        }
    }
}



listview介面卡

package com.example.kanghuwei_20181204.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.kanghuwei_20181204.R;
import com.example.kanghuwei_20181204.bean.ListBean;
import com.example.kanghuwei_20181204.util.IamgeUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class MyBase extends BaseAdapter {
    private List<ListBean.DataBean>list;
    private Context context;
    private ImageLoader imageLoadernstance;

    public MyBase(List<ListBean.DataBean> list, Context context, ImageLoader imageLoadernstance) {
        this.list = list;
        this.context = context;
        this.imageLoadernstance = imageLoadernstance;
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position%2 == 0){
            return 0;
        }else{
            return 1;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType){
            case 0:
                ViewHolder0 holder0;
            if (convertView == null){
                convertView = View.inflate(context, R.layout.lv_item_00,null);
                holder0 = new ViewHolder0();
                holder0.imageView1 = convertView.findViewById(R.id.imageView1);
                holder0.textView1 = convertView.findViewById(R.id.tv1);
                holder0.textView2 = convertView.findViewById(R.id.tv2);
                convertView.setTag(holder0);
            }else{
                holder0 = (ViewHolder0) convertView.getTag();
            }
            holder0.textView1.setText(list.get(position).getNews_id());
            holder0.textView2.setText(list.get(position).getNews_title());
            imageLoadernstance.displayImage(list.get(position).getPic_url(), holder0.imageView1,IamgeUtil.getDisplayImageOption());
            break;
            case 1:
                ViewHolder1 holder1;
                if (convertView == null){
                    convertView = View.inflate(context,R.layout.lv_item_11,null);
                    holder1 = new ViewHolder1();
                    holder1.textView3 = convertView.findViewById(R.id.textView3);
                    holder1.textView4 = convertView.findViewById(R.id.textView4);
                    convertView.setTag(holder1);
                }else{
                    holder1 = (ViewHolder1) convertView.getTag();
                }
                holder1.textView3.setText(list.get(position).getNews_id());
                holder1.textView4.setText(list.get(position).getNews_title());
                break;
        }
        return convertView;
    }
    class ViewHolder0{
        TextView textView1,textView2;
        ImageView imageView1;
    }
    class ViewHolder1{
        TextView textView3;
        TextView textView4;
    }

}

MainActivity.this

package com.example.kanghuwei_20181204;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.example.kanghuwei_20181204.fragment.Frag_01;
import com.example.kanghuwei_20181204.fragment.Frag_02;
import com.example.kanghuwei_20181204.fragment.Frag_03;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radiogroup;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //ctrl+alt+f 提全域性
        radiogroup = findViewById(R.id.radiogroup);
        //新增fragment
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        final Frag_01 frag_01 = new Frag_01();
        final Frag_02 frag_02 = new Frag_02();
        final Frag_03 frag_03 = new Frag_03();

        transaction.add(R.id.fragment1,frag_01);
        transaction.add(R.id.fragment1,frag_02);
        transaction.add(R.id.fragment1,frag_03);
        transaction.show(frag_01).hide(frag_02).hide(frag_03);
        transaction.commit();
        //預設顯示第一個
        radiogroup.check(radiogroup.getChildAt(0).getId());
        //radiogroup切換
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction = manager.beginTransaction();
                switch (checkedId){
                    case R.id.radiobutton1:
                        transaction.show(frag_01).hide(frag_02).hide(frag_03);
                        break;
                    case R.id.radiobutton2:
                        transaction.show(frag_02).hide(frag_01).hide(frag_03);
                        break;
                    case R.id.radiobutton3:
                        transaction.show(frag_03).hide(frag_02).hide(frag_01);
                        break;
                }
                transaction.commit();
            }
        });

    }
}