//activity_main.xml
<RelativeLayout
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="com.example.haowang.lanmu.MainActivity">
<HorizontalScrollView
android:id="@+id/hs"
android:layout_width="300dp"
android:scrollbars="none"
android:layout_height="40dp">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</HorizontalScrollView>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/hs"></android.support.v4.view.ViewPager>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/viewpager"
android:layout_toEndOf="@+id/hs"
android:layout_toRightOf="@+id/hs"
android:text="频道管理" />
</RelativeLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private HorizontalScrollView hs;
private LinearLayout linearLayout;
private ViewPager viewPager;
List<TextView> list;
private ArrayList<ChannelBean> channelBeens;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
list =new ArrayList<>();
//当前添加栏目的数据
channelBeens = new ArrayList<>();
channelBeens.add(new ChannelBean("头条",true));
channelBeens.add(new ChannelBean("娱乐",true));
channelBeens.add(new ChannelBean("科技",true));
channelBeens.add(new ChannelBean("信息",true));
channelBeens.add(new ChannelBean("八卦",true));
channelBeens.add(new ChannelBean("北京",false));
channelBeens.add(new ChannelBean("上海",false));
channelBeens.add(new ChannelBean("天津",false));
channelBeens.add(new ChannelBean("重庆",false));
channelBeens.add(new ChannelBean("大大燕网",false));
for (int i = 0; i < channelBeens.size(); i++){
TextView textView = new TextView(this);
textView.setText(channelBeens.get(i).getName()+"");
textView.setTextSize(20);
textView.setId(i+1000);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id = view.getId();
viewPager.setCurrentItem(id-1000);
}
});
//刚进程序第一个默认红色,其他为黑色
if(i ==0){
textView.setTextColor(Color.RED);
}else {
textView.setTextColor(Color.BLACK);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(50,10,50,10);
//添加到布局中
linearLayout.addView(textView,layoutParams);
//添加到集合
list.add(textView);
}
//为viewpager设置适配器
viewPager.setAdapter(new Myadapter(getSupportFragmentManager()));
//viewpager的监听
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//滑动改变颜色
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//list中存的是 textview
for (int i = 0; i < list.size(); i++) {
if(position==i){
list.get(i).setTextColor(Color.RED);
}else{
list.get(i).setTextColor(Color.BLACK);
}
}
//获取当前的textview
TextView textView = list.get(position);
//width是每次滑动的距离
int width = textView.getWidth()+10;
//让scrollView滑动 滑动距离是textview之间的间距
hs.scrollTo(width*position,0);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//将数据传入频道管理
ChannelActivity.startChannelActivity(MainActivity.this,channelBeens);
}
});
}
//获取资源ID
private void initView() {
hs = (HorizontalScrollView) findViewById(R.id.hs);
linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
viewPager = (ViewPager) findViewById(R.id.viewpager);
button = (Button) findViewById(R.id.button);
}
class Myadapter extends FragmentPagerAdapter {
public Myadapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Log.i("TAG",position+"");
return Fragment1.getinstance(list.get(position).getText().toString());
}
@Override
public int getCount() {
return list.size();
}
}
}
//SQlite
public class SqliteHelper extends SQLiteOpenHelper {
public SqliteHelper(Context context) {
super(context, "News.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table news(id integer primary key autoincrement," +
"title text" +
"content text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
//Newsdao
ublic class NewsDao {
private SQLiteDatabase database;
private SqliteHelper helper;
public NewsDao(Context context){
helper = new SqliteHelper(context);
database = helper.getReadableDatabase();
}
public void add(String title,String content){
ContentValues values = new ContentValues();
values.put("title",title);
values.put("content",content);
database.insert("news",null,values);
}
}
//Fragmentone
ublic class Fragment1 extends Fragment {
private PullToRefreshListView listView;
private Banner banner;
private List<String> image_list;
private String path = "http://120.76.205.241:8000/news/toutiao?pageToken=0&catid=news_society&contentType=";
private String key = "&apikey=sAAsHtdPErktuQosSUUG0AvpAsDPOMfUtMhQPz3mXnoVwe0S6DNEFIcfyhOpe7O9";
private int page = 1;
private List<HasNextBean.DataBean> lists = new ArrayList<>();
private MyBase adapter;
private NewsDao dao;
public static Fragment getinstance(String title){
//创建Fragment1
Fragment1 fragment1 = new Fragment1();
//创建bundle
Bundle bundle = new Bundle();
//添加值
bundle.putString("title",title);
//把值添加到Arguments中
fragment1.setArguments(bundle);
return fragment1;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentone,container,false);
listView = view.findViewById(R.id.listview);
banner = view.findViewById(R.id.banner);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dao = new NewsDao(getActivity());
//得到值 当展示科技的时候 在进行显示控件
Bundle bundle = getArguments();
if(bundle.get("title").equals("科技")){
lunbo();
banner.setVisibility(View.VISIBLE);
listView.setVisibility(View.VISIBLE);
pull();
}
}
//轮播
//Viewpager
public void lunbo(){
Log.i("WWWWW","2222222");
HttpUtils.getdata("http://www.wanandroid.com/tools/mockapi/6523/article_channel_list", getActivity(), new InterfaceUtils() {
@Override
public void getJson(String json) {
Log.i("WWWWW",json);
//请求数据
Gson gson = new Gson();
ChannelsBean channelsBean = gson.fromJson(json, ChannelsBean.class);
List<ChannelsBean.DataBean.FocusListBean> focus_list = channelsBean.getData().getFocus_list();
//存放网络地址的集合
image_list = new ArrayList<String>();
//遍历集合存储
for (int i = 0;i < focus_list.size();i++){
image_list.add(focus_list.get(i).getImage_url());
Log.i("WWWWW",focus_list.get(i).getImage_url()+"");
}
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
//设置图片加载器
banner.setImageLoader(new MyLoader());
//设置图片集合
banner.setImages(image_list);
//设置banner动画效果
banner.setBannerAnimation(Transformer.DepthPage);
//设置自动轮播,默认为true
banner.isAutoPlay(true);
//设置轮播时间
banner.setDelayTime(1500);
//设置指示器位置(当banner模式中有指示器时)
banner.setIndicatorGravity(BannerConfig.CENTER);
//banner设置方法全部调用完毕时最后调用
banner.start();
}
});
}
//上拉下拉
public void pull(){
String url = path+page+key;
HttpUtils.getdata(url, getActivity(), new InterfaceUtils() {
@Override
public void getJson(String json) {
//请求分页的数据
Gson gson = new Gson();
HasNextBean hasNextBean = gson.fromJson(json, HasNextBean.class);
List<HasNextBean.DataBean> data = hasNextBean.getData();
if(page == 1){
lists.clear();
}
lists.addAll(data);
//将list集合的数据存入数据库进行缓存
for (int i = 0;lists.size()>0;i++){
String title = lists.get(i).getTitle();
String content = lists.get(i).getContent();
dao.add(title,content);
}
adapter.notifyDataSetChanged();
}
});
}
//设置PullToRefreshListView
public void initView(){
//设置刷新模式 ,both代表支持上拉和下拉,pull_from_end代表上拉,pull_from_start代表下拉
listView.setMode(PullToRefreshBase.Mode.BOTH);
/*这里通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的状态的区别,第一个true 代表下来状态 ,第二个true 代表上拉的状态
如果想区分上拉和下拉状态的不同,可以分别设置*/
ILoadingLayout startLabels = listView.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉刷新");
startLabels.setRefreshingLabel("正在拉");
startLabels.setReleaseLabel("放开刷新");
ILoadingLayout endLabels = listView.getLoadingLayoutProxy(false, true);
endLabels.setPullLabel("上拉刷新");
endLabels.setRefreshingLabel("正在载入...");
endLabels.setReleaseLabel("放开刷新...");
listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
page++;
pull();//请求数据
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
pull();
}
});
adapter = new MyBase(lists,getActivity());
listView.setAdapter(adapter);
}
class MyLoader extends ImageLoader{
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load(path).into(imageView);
}
}
}
//适配器
public class MyBase extends BaseAdapter {
private List<HasNextBean.DataBean> lists;
private Context context;
public MyBase(List<HasNextBean.DataBean> lists, Context context) {
this.lists = lists;
this.context = context;
}
private final static int ONE = 0;
private final static int TWO = 1;
private final static int THREE = 2;
@Override
public int getCount() {
return lists.size();
}
//样式的总数
@Override
public int getViewTypeCount() {
return 3;
}
//样式的选中
@Override
public int getItemViewType(int position) {
if(lists.get(position).getImageUrls().size() == 3){
return ONE;
}else if(lists.get(position).getImageUrls().size() == 5){
return TWO;
}else if (lists.get(position).getImageUrls().size() == 6){
return THREE;
}else{
return ONE;
}
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(getItemViewType(i)==ONE){
oneholder holder;
if(view==null){
view=View.inflate(context,R.layout.mybaseone,null);
holder =new oneholder();
holder.text1 = view.findViewById(R.id.text1);
holder.image1= view.findViewById(R.id.image1);
view.setTag(holder);
}else{
holder = (oneholder) view.getTag();
}
holder.text1.setText(list.get(i).getTitle());
ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),holder.image1, MyApplication.getoptions());
return view;
}else if(getItemViewType(i)==TWO){
twoholder holder;
if(view==null){
view=View.inflate(context,R.layout.mybasetwo,null);
holder =new twoholder();
holder.text2 = view.findViewById(R.id.text2);
holder.image1= view.findViewById(R.id.image1);
holder.image2= view.findViewById(R.id.image2);
view.setTag(holder);
}else{
holder = (twoholder) view.getTag();
}
holder.text2.setText(list.get(i).getTitle());
ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),holder.image1, MyApplication.getoptions());
ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s02(),holder.image2,MyApplication.getoptions());
return view;
}else if(getItemViewType(i)==THREE)
threeholder holder;
if(view==null){
view=View.inflate(context,R.layout.mybasethree,null);
holder =new threeholder();
holder.text3 = view.findViewById(R.id.text3);
holder.image1= view.findViewById(R.id.image1);
holder.image2= view.findViewById(R.id.image2);
holder.image3= view.findViewById(R.id.image3);
view.setTag(holder);
}else{
holder = (threeholder) view.getTag();
}
holder.text3.setText(list.get(i).getTitle());
ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),holder.image1, MyApplication.getoptions());
ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s02(),holder.image2,MyApplication.getoptions());
ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s03(),holder.image3,MyApplication.getoptions());
return view;
}
return null;
}
class oneholder{
TextView text1;
ImageView image1;
}
class twoholder{
TextView text2;
ImageView image1;
ImageView image2;
}
class threeholder{
TextView text3;
ImageView image1;
ImageView image2;
ImageView image3;
}
}
//fragmentone.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
>
<com.youth.banner.Banner
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:visibility="gone"
></com.youth.banner.Banner>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:visibility="gone"
ptr:ptrDrawable="@drawable/default_ptr_flip"
ptr:ptrAnimationStyle="flip"
ptr:ptrHeaderBackground="#383838"
ptr:ptrHeaderTextColor="#FFFFFF"
></com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>