一个简单的新闻页面

这个是用Drawlayout和一个tablayout做的


    
        
        
    

    

后面再来看看具体的实现


import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

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

import bwie.com.sxian2.adapter.TAdapter;
import bwie.com.sxian2.fragment.Fragment02;
import bwie.com.sxian2.fragment.Fragment03;
import bwie.com.sxian2.fragment.Fragment04;
import bwie.com.sxian2.fragment.Fragment05;
import bwie.com.sxian2.fragment.Fragment06;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout draw;
    private ViewPager pager;
    private TabLayout tab;
    private List list_f;
    private List list;
    private ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        draw = findViewById(R.id.draw);
        pager = findViewById(R.id.pager);
        tab = findViewById(R.id.tab);
        draw.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNDEFINED);
        initad();
        inittab();
        pagerData();
    }

    private void pagerData() {
        TAdapter tAdapter = new TAdapter(getSupportFragmentManager(),list_f,list);
        tab.setTabMode(TabLayout.MODE_FIXED);
        tab.setupWithViewPager(pager);
        pager.setAdapter(tAdapter);
    }

    private void inittab() {
        list=new ArrayList<>();
        list_f=new ArrayList<>();
        list.add("首页");
        list.add("附近");
        list.add("发现");
        list.add("订单");
        list.add("我的");
        list_f.add(new Fragment02());
        list_f.add(new Fragment03());
        list_f.add(new Fragment04());
        list_f.add(new Fragment05());
        list_f.add(new Fragment06());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initad() {
        ActionBar actionBar = getSupportActionBar();
        //设置一个默认的照片
        actionBar.setDisplayHomeAsUpEnabled(true);
        toggle = new ActionBarDrawerToggle(this, draw, R.string.open, R.string.close);
        toggle.syncState();
        draw.addDrawerListener(toggle);
    }
}

再来看看适配器里具体写得代码


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.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import bwie.com.sxian2.R;
import bwie.com.sxian2.bean.YShengBean;
import bwie.com.sxian2.imageloader.ImageLoaderConfigs;

public class Mapter extends BaseAdapter {
    private Context context;
    private List list;

    public Mapter(Context context) {
        this.context = context;
        list=new ArrayList();
    }

    public void setData(List is) {
        this.list.clear();
        if (is!=null){
            list.addAll(is);
        }
        notifyDataSetChanged();
    }

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

    @Override
    public YShengBean.DataBean 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 vh;
        if (view==null){
            view=View.inflate(context,R.layout.itme,null);
            vh=new ViewHolder(view);
        }else {
            vh= (ViewHolder) view.getTag();
        }
        vh.bindData(getItem(i));
        return view;
    }
    class ViewHolder{
        ImageView image;
        TextView title,name;

        public ViewHolder(View view) {
            title=view.findViewById(R.id.title);
            name=view.findViewById(R.id.neirong);
            image=view.findViewById(R.id.image);
            view.setTag(this);
        }
        public void bindData(YShengBean.DataBean file){
            title.setText(file.getAuthor_name());
            name.setText(file.getTitle());
            ImageLoader.getInstance().displayImage(file.getThumbnail_pic_s(),image,ImageLoaderConfigs.getDisplayImageOptions(image.getContext()));
        }
    }
}
package bwie.com.sxian2.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.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import bwie.com.sxian2.R;
import bwie.com.sxian2.bean.YShengBean;
import bwie.com.sxian2.imageloader.ImageLoaderConfigs;

public class Mapter extends BaseAdapter {
    private Context context;
    private List list;

    public Mapter(Context context) {
        this.context = context;
        list=new ArrayList();
    }

    public void setData(List is) {
        this.list.clear();
        if (is!=null){
            list.addAll(is);
        }
        notifyDataSetChanged();
    }

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

    @Override
    public YShengBean.DataBean 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 vh;
        if (view==null){
            view=View.inflate(context,R.layout.itme,null);
            vh=new ViewHolder(view);
        }else {
            vh= (ViewHolder) view.getTag();
        }
        vh.bindData(getItem(i));
        return view;
    }
    class ViewHolder{
        ImageView image;
        TextView title,name;

        public ViewHolder(View view) {
            title=view.findViewById(R.id.title);
            name=view.findViewById(R.id.neirong);
            image=view.findViewById(R.id.image);
            view.setTag(this);
        }
        public void bindData(YShengBean.DataBean file){
            title.setText(file.getAuthor_name());
            name.setText(file.getTitle());
            ImageLoader.getInstance().displayImage(file.getThumbnail_pic_s(),image,ImageLoaderConfigs.getDisplayImageOptions(image.getContext()));
        }
    }
}

在来看看Fragment,这里只用一个fragment里有东西,其他都是空的



import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import bwie.com.sxian2.R;
import bwie.com.sxian2.adapter.Mapter;
import bwie.com.sxian2.bean.PagerBean;
import bwie.com.sxian2.bean.YShengBean;
import bwie.com.sxian2.util.Httputil;
import me.maxwin.view.XListView;

public class Fragment02 extends Fragment {
    private final int ONE=1;
    private final int TWO=2;
    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){
                case ONE:
                    List list= (List) msg.obj;
                    vadpter.setData(list);
                    break;
                case TWO:
                    List ysb= (List) msg.obj;
                    mapter.setData(ysb);
                    pagerdizheng();
                    break;

            }
        }
    };
    private int page=1;
    private ViewPager pager;
    private Vadpter vadpter;
    private XListView xlist;
    private Mapter mapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr1, container, false);
        pager = view.findViewById(R.id.f_pager);
        xlist = view.findViewById(R.id.f_xlist);
        mapter = new Mapter(getContext());
        xlist.setPullLoadEnable(true);
        xlist.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                page++;
                Zhuoqu();
            }

            @Override
            public void onLoadMore() {
                page++;
                Zhuoqu();

            }
        });
        xlist.setAdapter(mapter);
        vadpter = new Vadpter(getActivity());
        huoqu();
        Zhuoqu();
        pager.setAdapter(vadpter);
        return view;
    }
    private String uil="http://www.xieast.com/api/news/news.php?page=";
    private void Zhuoqu() {
        final String ull=uil+page;
        new Thread(new Runnable() {
            @Override
            public void run() {
                String request = new Httputil().getHttputil().getRequest(ull);
                YShengBean filmBean = new Gson().fromJson(request, YShengBean.class);
                handler.sendMessage(handler.obtainMessage(TWO,filmBean.getData()));
            }
        }).start();
    }

    private String utl="http://www.xieast.com/api/banner.php";
    private void huoqu() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String request = new Httputil().getHttputil().getRequest(utl);
                PagerBean filmBean = new Gson().fromJson(request, PagerBean.class);
                handler.sendMessage(handler.obtainMessage(ONE,filmBean.getData()));
            }
        }).start();
    }
    private void pagerdizheng() {
        page++;
        xlist.stopRefresh();
        xlist.stopLoadMore();
    }
}

还有一个照片的配置


import android.content.Context;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import bwie.com.sxian2.R;

public class ImageLoaderConfigs {
    public static ImageLoaderConfiguration getImageLoaderConfiguration(Context context){
        ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(context)
                .memoryCacheSizePercentage(13)
                .diskCacheSize(50 * 1024 * 1024)
                .build();
        return build;
    }
    public static DisplayImageOptions getDisplayImageOptions(Context context){
        DisplayImageOptions build = new DisplayImageOptions.Builder()
                ..cacheInMemory(true)//让图片进行内存缓存
                .cacheOnDisk(true)//让图片进行sdcard缓存
                .showImageForEmptyUri(R.mipmap.pic_default)//图片地址有误
                .showImageOnFail(R.mipmap.pic_default)//当图片加载出现错误的时候显示的图片
                .build();
        return build;
    }
}

大致的代码就是这样了

你可能感兴趣的:(一个简单的新闻页面)