//TabLayout依赖
implementation 'com.android.support:design:26.+'
//图片加载框架glide
compile 'com.github.bumptech.glide:glide:3.7.0'
// 依赖Fresco compile 'com.facebook.fresco:fresco:0.14.1'
//gif的依赖 compile 'com.facebook.fresco:animated-gif:0.14.1'//
retrofit
依赖
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
//视频播放jiecao依赖
compile 'fm.jiecao:jiecaovideoplayer:2.0'
implementation 'com.android.support:recyclerview-v7:26.+'
图片接口
//https://www.apiopen.top/satinApi?type=3&page=1
//视频接口
https://www.apiopen.top/satinApi?type=4&page=2
常量类 public class Constant { public static final String URL_BASE="https://www.apiopen.top/"; //https://www.apiopen.top/satinApi?type=3&page=1 public static final String URL_SHI="https://www.apiopen.top/"; }
创建接口,拼接字符串 public interface MyServerInterface { @GET("satinApi?type=3&page=1") Call<ResponseBody> getLastString(); @GET("satinApi?type=4&page=2") Call<ResponseBody> getLastshi(); }
fresco的调用 public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }
<!--app:tabMode="scrollable"--> <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/myTab" app:tabGravity="center" app:tabIndicatorColor="@color/colorAccent" app:tabMode="scrollable" app:tabSelectedTextColor="@color/colorPrimaryDark" app:tabTextColor="@color/colorPrimary"></android.support.design.widget.TabLayout> <!--viewPager组件--> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp" android:layout_below="@+id/myTab"></android.support.v4.view.ViewPager>
public class MainActivity extends AppCompatActivity { private TabLayout tabLayout; private ViewPager viewPager; private List<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); tabLayout = findViewById( R.id.myTab ); viewPager = findViewById( R.id.vp ); initMinum(); MyAdapterpager myAdapterpager = new MyAdapterpager( getSupportFragmentManager() ); viewPager.setAdapter( myAdapterpager ); viewPager.setOffscreenPageLimit( list.size() ); tabLayout.setupWithViewPager( viewPager ); } private void initMinum() { list = new ArrayList<>( ); list.add( "推荐" ); list.add( "文学" ); list.add( "图片" ); list.add( "视频" ); list.add( "预留" ); list.add( "历史" ); list.add( "科技" ); } class MyAdapterpager extends FragmentPagerAdapter{ public MyAdapterpager(FragmentManager fm) { super( fm ); } @Override public Fragment getItem(int position) { if (position==3){ Fragment02 fragment02 = new Fragment02(); return fragment02; }else { Fragment01 fragment01 = new Fragment01(); return fragment01; } } @Override public CharSequence getPageTitle(int position) { return list.get( position ); } @Override public int getCount() { return list.size(); } } }
public class Fragment01 extends Fragment { private RecyclerView recyclerView; private MyServerInterface myServerInterface; private Call<ResponseBody> mcall; private Handler handler=new Handler( ){ @Override public void handleMessage(Message msg) { super.handleMessage( msg ); List<ReaBean.DataBean> data= (List<ReaBean.DataBean>) msg.obj; MyAdapter myAdapter = new MyAdapter( data, getActivity() ); recyclerView.setAdapter( myAdapter ); recyclerView.setLayoutManager( new LinearLayoutManager( getActivity(), LinearLayoutManager.VERTICAL, false) ); } }; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate( R.layout.ment01, container, false ); recyclerView = view.findViewById( R.id.recycler ); initData(); return view; } private void initData() { Retrofit retrofit = new Retrofit.Builder().baseUrl( Constant.URL_BASE ).build(); myServerInterface = retrofit.create( MyServerInterface.class ); mcall = myServerInterface.getLastString(); mcall.enqueue( new Callback<ResponseBody>() { private String reuslt; @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { try { reuslt = response.body().string(); Gson gson = new Gson(); ReaBean reaBean = gson.fromJson( reuslt, ReaBean.class ); List<ReaBean.DataBean> data = reaBean.getData(); Message obtain = Message.obtain(); obtain.obj=data; handler.sendMessage( obtain ); } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { } } ); } }
<android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycler"></android.support.v7.widget.RecyclerView>
适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { List<ReaBean.DataBean> list; Context context; public MyAdapter(List<ReaBean.DataBean> list, Context context) { this.list = list; this.context = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = View.inflate( context, R.layout.item01, null ); ViewHolder viewHolder = new ViewHolder( inflate ); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.tv.setText( list.get( position ).getText() ); final String picUrl = list.get(position).getProfile_image(); final Uri uri = Uri.parse(picUrl); holder.img1.setImageURI(uri); } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder{ private final TextView tv; private final SimpleDraweeView img1; private final SimpleDraweeView img2; public ViewHolder(View itemView) { super( itemView ); tv = itemView.findViewById( R.id.tv ); img1 = itemView.findViewById( R.id.my_image_view ); img2 = itemView.findViewById( R.id.my_image ); } } }
<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_image_view" android:layout_width="130dp" android:layout_marginLeft="15dp" android:layout_marginTop="15dp" android:layout_height="130dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" android:layout_marginLeft="15dp" android:layout_marginTop="15dp" android:layout_toRightOf="@id/my_image_view" /> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_image" android:layout_below="@id/my_image_view" android:layout_marginTop="15dp" android:layout_width="500dp" android:layout_height="400dp" />
public class Fragment02 extends Fragment { @Nullable private TextView textView; private RecyclerView recyclerView; private MyServerInterface myServerInterface; private Call<ResponseBody> mcall; private Handler handler=new Handler( ){ @Override public void handleMessage(Message msg) { super.handleMessage( msg ); List<ReaBean02.DataBean> data= ( List<ReaBean02.DataBean>) msg.obj; MyAdaptershi adaptershi = new MyAdaptershi( data, getActivity() ); recyclerView.setAdapter( adaptershi ); recyclerView.setLayoutManager( new LinearLayoutManager( getActivity(), LinearLayoutManager.VERTICAL, false) ); } }; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate( R.layout.ment02, container, false ); initData(); recyclerView = view.findViewById( R.id.recycler ); JCVideoPlayer.releaseAllVideos(); return view; } private void initData() { Retrofit retrofit = new Retrofit.Builder().baseUrl( Constant.URL_SHI).build(); myServerInterface = retrofit.create( MyServerInterface.class ); mcall = myServerInterface.getLastshi(); mcall.enqueue( new Callback<ResponseBody>() { private String reuslt; @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { try { reuslt = response.body().string(); Gson gson = new Gson(); ReaBean02 reaBean02 = gson.fromJson( reuslt, ReaBean02.class ); List<ReaBean02.DataBean> data = reaBean02.getData(); Message obtain = Message.obtain(); obtain.obj=data; handler.sendMessage( obtain ); } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { } } ); } }
带视频的,适配器类 public class MyAdaptershi extends RecyclerView.Adapter<MyAdaptershi.ViewHolder> { List<ReaBean02.DataBean> list; Context context; public MyAdaptershi(List<ReaBean02.DataBean> list, Context context) { this.list = list; this.context = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = View.inflate( context, R.layout.item02, null ); ViewHolder viewHolder = new ViewHolder( inflate ); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.tv.setText( list.get( position ).getText() ); //视频加载 holder.videocontroller1.setUp( list.get( position ).getVideouri(),list.get( position ).getName() ); } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder { private final TextView tv; private final JCVideoPlayer videocontroller1; public ViewHolder(View itemView) { super( itemView ); tv = itemView.findViewById( R.id.tv ); videocontroller1 = itemView.findViewById( R.id.videocontroller1 ); } } }
<TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="15dp" android:layout_toRightOf="@id/my_image_view" /> 视频播放控件 <fm.jiecao.jcvideoplayer_lib.JCVideoPlayer android:id="@+id/videocontroller1" android:layout_width="match_parent" android:layout_below="@id/tv" android:layout_height="200dp" />