带有侧滑菜单的+ 通过retrofit获取数据,通过fresco框架加载图片,使用TabLayout + ViewPage + Fragment + ListView 显示数据

题目要求:

按要求完成下面的各项需求。

使用接口:

//头条

http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41

//娱乐

http://litchiapi.jstv.com/api/GetFeeds?column=7&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41 

//体育

http://litchiapi.jstv.com/api/GetFeeds?column=6&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC4 



 

需求:

1, 橘色部分:  使用ToolBar 实现

2,绿色+ 黑色: 使用TabLayout + ViewPage + Fragment + ListView  实现网络数据的获取,通过retrofit;图片的加载通过fresco框架

                3,使用Design中的NavigationView,实现蓝色部分的侧滑功能

 

注意:直接从网络获取的图片网址不完整,需要在字符串前面补充一下域名。

http://litchiapi.jstv.com



实现代码:

build.gradle  添加依赖

compile 'com.android.support:design:26.0.0-alpha1'
compile 'com.facebook.fresco:fresco:1.5.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'


NewInterface.java
 
	
import android.util.ArrayMap;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.QueryMap;

/**
 * 获取数据的接口
 * Created by Administrator on 2017/11/18.
 */

public interface NewInterface {

    //公共地址 baseUrl ---不同的地址 (@GET(....)) --参数信息(方法的参数列表)
    @GET("api/GetFeeds")
    Call<NewsInfo> getNewInfo(@QueryMap ArrayMap<String,String> map);
}

NewsInfo.java
    利用GsonFormant生成实体类

main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bw.com.bw_day18.MainActivity"
    android:id="@+id/dl_id"
    >


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tool_bar_id"
            />


        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_below="@id/tool_bar_id"
            android:id="@+id/tab_layout_id"
            app:tabIndicatorHeight="4dp"
            app:tabSelectedTextColor="@color/colorAccent" />

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/tab_layout_id"/>


    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/nv_id"
        android:layout_gravity = "left"
        app:headerLayout="@layout/heard_layout"
        app:menu="@menu/navigation_selection"
        />



</android.support.v4.widget.DrawerLayout>

fragment_my.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bw.com.bw_day18.MyFragment">

    <!-- TODO: Update blank fragment layout -->
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv"/>

</FrameLayout>

heard_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="250dp">

    <ImageView
        android:layout_width="176dp"
        android:layout_height="176dp"
        android:layout_centerInParent="true"
        android:id="@+id/iv_id"
        android:src="@mipmap/lf"
        />

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="八维1509B"
        android:layout_centerInParent="true"
        android:textSize="30sp"
        android:textColor="@color/colorAccent"
        android:layout_below="@id/iv_id"/>

</RelativeLayout>

item_lv.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/item_iv"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerVertical="true"
        fresco:placeholderImage="@mipmap/ic_launcher"
        fresco:roundedCornerRadius="10dp"
        fresco:roundTopLeft="true"
        fresco:roundTopRight="true"
        fresco:roundBottomLeft="true"
        fresco:roundBottomRight="true"/>

    <TextView
        android:id="@+id/subject_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="subject"
        android:layout_toRightOf="@id/item_iv"
        android:layout_centerVertical="true"
        android:textSize="20sp"
        android:layout_marginLeft="10dp"
        />




</RelativeLayout>

菜单menu 中

main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/action_set"
        android:title="设置"
        />
</menu>


navigation_selection.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item

        android:id="@+id/action_set"
        android:title="设置"
        android:icon="@android:drawable/ic_menu_set_as"
        />

    <item
        android:id="@+id/action_above"
        android:title="关于"
        android:icon="@android:drawable/ic_menu_add"
        />

    <item
        android:id="@+id/action_clear"
        android:title="清除缓存"
        android:icon="@android:drawable/ic_menu_delete"
        />
</menu>


代码:

MainActivity.java

package bw.com.bw_day18;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.Window;

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

public class MainActivity extends AppCompatActivity {

    private Toolbar mToolBar;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private NavigationView mNavigationView;

    private List<Fragment> fragments;
    private List<String> titles;
    private MyAdapter adapter;
    private DrawerLayout mDl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //取消ActionBar
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        //控件初始化
        initView();

        //显示ToolBar
        mToolBar.setNavigationIcon(R.mipmap.ic_launcher);
        mToolBar.setTitle("我是一个ToolBar");
        mToolBar.inflateMenu(R.menu.main);
        //点击图标, 打开抽屉
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDl.openDrawer(Gravity.LEFT);//打开抽屉
            }
        });
        //每个条目的处理mToolBar.setOnMenuItemClickListener();

        //TabLayout + ViewPager + Fragment
        //初始化数据源
        initData();

        adapter = new MyAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(adapter);

        //把TabLayout和ViewPager 联系到一起
        mTabLayout.setupWithViewPager(mViewPager);

        //设置TabLayout的滑动模式 mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);


        //NavigationView中的获取
        //可以获取头部视图 View headerView  =  mNv.getHeaderView(0);
        //为菜单项设置点击事件  mNavigationView.setNavigationItemSelectedListener();

    }



    private void initView() {
        mDl = (DrawerLayout) findViewById(R.id.dl_id);
        mToolBar = (Toolbar) findViewById(R.id.tool_bar_id);
        mTabLayout = (TabLayout) findViewById(R.id.tab_layout_id);
        mViewPager = (ViewPager) findViewById(R.id.view_pager_id);
        mNavigationView = (NavigationView) findViewById(R.id.nv_id);
    }
    private void initData() {

        fragments = new ArrayList<>();
        for(int i = 0;i<3;i++)
        {
            MyFragment fragment = new MyFragment();

            Bundle bundle = new Bundle();
            bundle.putInt("tabId",i);
            fragment.setArguments(bundle);

            fragments.add(fragment);
        }

        titles = new ArrayList<>();
        titles.add("头条");
        titles.add("娱乐");
        titles.add("体育");

    }

    //自定义的Fragment + ViewPager 的适配器
    class MyAdapter extends FragmentPagerAdapter
    {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

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

        //返回当前显示内容的标题信息
        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }

}


MyApp.java

import android.app.Application;

import com.facebook.drawee.backends.pipeline.Fresco;

/**
 * Created by Administrator on 2017/11/18.
 */

public class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        //初始化Fresco
        Fresco.initialize(this);
    }
}

MyFragment.java

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class MyFragment extends Fragment {

    private ListView mLv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_my,container,false);

        mLv = (ListView) view.findViewById(R.id.lv);

        //获取传入的参数, 决定加载那个地址的内容
        int tabId = getArguments().getInt("tabId");


        //-------Retrofit  加载数据-------
        //导入依赖  -- 接口 (Call<数据返回实体类> 方法)  -- 数据返回实体类
        //声明Retrofit
        //建造者
        Retrofit.Builder builder = new Retrofit.Builder();

        //设置公共的地址
        builder.baseUrl("http://litchiapi.jstv.com/");

        //构建剩余的路径中的参数列表
        ArrayMap<String,String> map = new ArrayMap<>();
        switch (tabId)
        {
            case 0:
                //column=0&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41
                map.put("column","0");
                break;

            case 1:
                //column=7&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41
                map.put("column","7");
                break;

            case 2:
                //column=6&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41
                map.put("column","6");
                break;
        }
        map.put("PageSize","20");
        map.put("pageIndex","1");
        map.put("val","100511D3BE5301280E0992C73A9DEC41");



        //因为要解析json数据, 所以gson解析
        builder.addConverterFactory(GsonConverterFactory.create());

        //得到Retrofit对象
        Retrofit retrofit = builder.build();

        //获得接口中的方法
        //得到接口
        NewInterface filmInter = retrofit.create(NewInterface.class);

        Call<NewsInfo> call = filmInter.getNewInfo(map);

        call.enqueue(new Callback<NewsInfo>() {
            @Override
            public void onResponse(Call<NewsInfo> call, Response<NewsInfo> response) {
                //主线程中
                //得到获取的数据
                NewsInfo newInfo = response.body();

                List<NewsInfo.ParamzBean.FeedsBean> data =  newInfo.getParamz().getFeeds();

                MyListAdapter adapter = new MyListAdapter(data);

                mLv.setAdapter(adapter);


            }

            @Override
            public void onFailure(Call<NewsInfo> call, Throwable t) {

            }
        });


        return view;
    }

    class MyListAdapter extends BaseAdapter
    {

        private List<NewsInfo.ParamzBean.FeedsBean> data;

        private MyListAdapter(List<NewsInfo.ParamzBean.FeedsBean> data)
        {
            this.data = data;
        }
        @Override
        public int getCount() {
            return data.size();
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder;

            if(convertView == null)
            {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_lv,null);

                viewHolder = new ViewHolder();

                viewHolder.iv = (SimpleDraweeView) convertView.findViewById(R.id.item_iv);
                viewHolder.subject = (TextView) convertView.findViewById(R.id.subject_id);

                convertView.setTag(viewHolder);

            }
            else
            {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            //从数据源中, 获取当前显示的条目
             NewsInfo.ParamzBean.FeedsBean feedsBean = data.get(position);

            //利用Fresco 加载图片
            viewHolder.iv.setImageURI(Uri.parse("http://litchiapi.jstv.com"+ feedsBean.getData().getCover()));

            viewHolder.subject.setText(feedsBean.getData().getSubject());

            return convertView;
        }

        class ViewHolder
        {
            SimpleDraweeView iv;
            TextView subject;
        }
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值