导入的包
compile 'com.facebook.fresco:animated-base-support:0.12.0' compile 'com.facebook.fresco:animated-gif:0.12.0' compile 'com.facebook.fresco:animated-webp:0.12.0' compile 'com.facebook.fresco:webpsupport:0.12.0' compile 'com.facebook.fresco:webpsupport:0.12.0' compile 'com.facebook.fresco:fresco:0.12.0' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support:design:26.0.0-alpha1' compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.squareup.retrofit2:retrofit:2.2.0' compile 'com.squareup.retrofit2:converter-gson:2.2.0' testCompile 'junit:junit:4.12'
布局
<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" android:id="@+id/drawerLayout" tools:context="com.example.dell_pc.day11_18moni.MainActivity"> <android.support.design.widget.NavigationView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="left" app:headerLayout="@layout/heard_view" android:id="@+id/heardView" app:menu="@menu/main" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="70dp" android:background="@color/colorPrimary"></android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout android:id="@+id/table" android:layout_width="match_parent" android:layout_height="50dp" app:tabIndicatorColor="@color/colorAccent" app:tabIndicatorHeight="5dp" app:tabTextColor="#000000" app:tabSelectedTextColor="@color/colorPrimary" ></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v4.view.ViewPager> </LinearLayout> </android.support.v4.widget.DrawerLayout>
activity
public class MainActivity extends AppCompatActivity{ private TabLayout table; private ViewPager viewPager; private Toolbar toolbar; private MyAdapter adapter; private List<String> titles = new ArrayList<>();//一系列标题 private List<Fragment> data = new ArrayList<>();//一系列标题 private DrawerLayout drawerLayout; private NavigationView heardView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView(); } private void initView() { drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); heardView = (NavigationView) findViewById(R.id.heardView); table = (TabLayout) findViewById(R.id.table); viewPager = (ViewPager) findViewById(R.id.viewPager); toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setNavigationIcon(R.mipmap.ic_launcher); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (drawerLayout.isEnabled()) { drawerLayout.openDrawer(Gravity.LEFT); } } }); toolbar.setTitle("荔枝新闻"); toolbar.inflateMenu(R.menu.main); titles.add("头条"); titles.add("娱乐"); titles.add("体育"); data.add(new BlankFragment_1()); data.add(new BlankFragment_2()); data.add(new BlankFragment_3()); adapter = new MyAdapter(getSupportFragmentManager(), data); viewPager.setAdapter(adapter); table.setupWithViewPager(viewPager); table.setTabMode(TabLayout.MODE_SCROLLABLE); } class MyAdapter extends FragmentPagerAdapter { private List<Fragment> data; public MyAdapter(FragmentManager fm, List<Fragment> data) { super(fm); this.data = data; } @Override public Fragment getItem(int position) { return data.get(position); } @Override public int getCount() { return data.size(); } @Override public CharSequence getPageTitle(int position) { return titles.get(position); } }
接口
public interface TenInterface_1 { // http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41 @GET("GetFeeds?column=0&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41") Call<Move> getList(); }
myAPP
public class MyApp extends Application{ @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }
fresco获取图片
Uri uri = Uri.parse("http://litchiapi.jstv.com"+data_1.getCover()); mylayout_1.imageView_1.setImageURI(uri); mylayout_1.textView_1.setText(data_1.getSubject());
网络矿建
Retrofit.Builder builder = new Retrofit.Builder(); //设置公共地址 builder.baseUrl("http://litchiapi.jstv.com/api/"); //设置解析 builder.addConverterFactory(GsonConverterFactory.create()); //得到Retrofit Retrofit retrofit = builder.build(); //从Retrofit的到接口 TenInterface_1 tenInterface_1 = retrofit.create(TenInterface_1.class); //得到Call对象 Call<Move> call_1 = tenInterface_1.getList(); //传入接口回掉,获取头条数据 call_1.enqueue(new Callback<Move>() { @Override public void onResponse(Call<Move> call, Response<Move> response) { //从response中获取数据 Move move = response.body(); Move.ParamzBean paramz = move.getParamz(); List<Move.ParamzBean.FeedsBean> feeds = paramz.getFeeds(); for (int i = 0; i < feeds.size(); i++) { Move.ParamzBean.FeedsBean feedsBean = feeds.get(i); Move.ParamzBean.FeedsBean.DataBean data = feedsBean.getData(); //图片 String cover = data.getCover(); //头部 String subject = data.getSubject(); list.add(new Data_1(cover,subject)); MyAdapte_1 myAdapte_1 = new MyAdapte_1(getActivity(),list); listView_1.setAdapter(myAdapte_1); } } @Override public void onFailure(Call<Move> call, Throwable t) { } });