PINGDao Item(完)

1.Main

package com.example.lenovo.todaynewsmouth;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.lenovo.todaynewsmouth.fragment.CityFragment;
import com.example.lenovo.todaynewsmouth.fragment.HotFragment;
import com.example.lenovo.todaynewsmouth.fragment.ShengFragment;
import com.example.lenovo.todaynewsmouth.fragment.TuiFragment;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView txtTui;
    private TextView txtHot;
    private TextView txtSheng;
    private TextView txtCity;
    private ViewPager vp;
    private List<Fragment> fragmentList;
    private TuiFragment tui;
    private HotFragment hot;
    private ShengFragment sheng;
    private CityFragment city;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtTui = findViewById(R.id.txt_tui);
        txtHot = findViewById(R.id.txt_hot);
        txtSheng = findViewById(R.id.txt_sheng);
        txtCity = findViewById(R.id.txt_city);

        vp = findViewById(R.id.vp);

        fragmentList = new ArrayList<>();
        tui = new TuiFragment();
        hot = new HotFragment();
        sheng = new ShengFragment();
        city = new CityFragment();
        fragmentList.add(tui);
        fragmentList.add(hot);
        fragmentList.add(sheng);
        fragmentList.add(city);

        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });
        txtTui.setOnClickListener(this);
        txtHot.setOnClickListener(this);
        txtSheng.setOnClickListener(this);
        txtCity.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.txt_tui:
                vp.setCurrentItem(0);
                break;

            case R.id.txt_hot:
                vp.setCurrentItem(1);
                break;


            case R.id.txt_sheng:
                vp.setCurrentItem(2);
                break;

            case R.id.txt_city:
                vp.setCurrentItem(3);
                break;

        }
    }
}

 

2. Fragment

package com.example.lenovo.todaynewsmouth.fragment;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.lenovo.todaynewsmouth.ChannelActivity;
import com.example.lenovo.todaynewsmouth.HttpUtils;
import com.example.lenovo.todaynewsmouth.News;
import com.example.lenovo.todaynewsmouth.NewsAdapter;
import com.example.lenovo.todaynewsmouth.R;
import com.google.gson.Gson;

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

/**
 *
 */

public class TuiFragment extends Fragment{

    private TabLayout tbl;
    private ImageView showAll;
    private ListView lvNews;
private static String url = "http://365jia.cn/news/api3/365jia/news/categories/hotnews?page=1";
    private List<News.DataBeanX.DataBean> list;
    private NewsAdapter adapter;

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

        View v = inflater.inflate(R.layout.fragment_tui,container,false);
        tbl = v.findViewById(R.id.tbl);
        showAll = v.findViewById(R.id.show_all);
        lvNews = v.findViewById(R.id.lv_news);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

   //设置TabLayout
        List<String> stringList = new ArrayList<>();
        stringList.add("头条");
        stringList.add("社会");
        stringList.add("国内");
        stringList.add("国际");
        stringList.add("娱乐");
        stringList.add("体育");
        stringList.add("军事");
        stringList.add("科技");
        stringList.add("财经");
        stringList.add("时尚");
        tbl.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (String s : stringList) {
            tbl.addTab(tbl.newTab().setText(s));
        }

        //点击加号showAll
        final ArrayList<String> arrayList = new ArrayList<>();
        arrayList.addAll(stringList);
    showAll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(getActivity(), ChannelActivity.class);
            //下面这三行是把 TabLayout 里的数据  传到我的频道里当数据
            Bundle bundle = new Bundle();
            bundle.putStringArrayList("array",arrayList);
            intent.putExtras(bundle);

            startActivity(intent);

        }
    });




        list = new ArrayList<>();
        adapter = new NewsAdapter(getActivity(),list);
        lvNews.setAdapter(adapter);

        new AsyncTask<String,Integer,String>(){

            @Override
            protected String doInBackground(String... strings) {
                return HttpUtils.getHttpUrlConnection(strings[0]);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                News news = gson.fromJson(s, News.class);
                list.clear();
                list.addAll(news.getData().getData());
                adapter.notifyDataSetChanged();

            }
        }.execute(url);

    }
}

 // Layout

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <android.support.design.widget.TabLayout
            android:id="@+id/tbl"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="50dp"></android.support.design.widget.TabLayout>
        <ImageView
            android:id="@+id/show_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/feedback_add"/>
    </LinearLayout>

    <ListView
        android:id="@+id/lv_news"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

</LinearLayout>

 

 

3.ChannelActivity

package com.example.lenovo.todaynewsmouth;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.example.lenovo.todaynewsmouth.adapter.MyGvAdapter;
import com.example.lenovo.todaynewsmouth.adapter.OtherAdapter;

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

public class ChannelActivity extends AppCompatActivity implements View.OnClickListener{

    private ArrayList<String> array;
    private Button btnfinish;
    private TextView txtEdit;
    private GridView gvMyChannel;
    private GridView gvotherChannel;
    private List<String> recommond;

    //当前是否是编辑状态
    private boolean isEdited =false;
    private MyGvAdapter myGvAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_channel);

        btnfinish = findViewById(R.id.btn_finish);
        txtEdit = findViewById(R.id.txtEdit);
        gvMyChannel = findViewById(R.id.gvMyChannel);
        gvotherChannel = findViewById(R.id.gvotherChannel);


        //接收值
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        array = bundle.getStringArrayList("array");

        //给 我的 频道添加适配器
        myGvAdapter = new MyGvAdapter(this,array);
       gvMyChannel.setAdapter(myGvAdapter);


       //给推荐频道写数据
        recommond = new ArrayList<>();
        recommond.add("特卖");
        recommond.add("直播");
        recommond.add("房产");
        recommond.add("历史");
        recommond.add("旅游");

        //给 推荐频道添加适配器
        final OtherAdapter otherAdapter = new OtherAdapter(this,recommond);
        gvotherChannel.setAdapter(otherAdapter);



        //初始化接口实例方法(从我的频道 点击删除按钮  添加到推荐频道里)
        MyGvAdapter.OnDeleteItemClick listener = new MyGvAdapter.OnDeleteItemClick() {
            @Override
            public void onDeleteClick(int position) {

                recommond.add(array.get(position));
                otherAdapter.notifyDataSetChanged();

                array.remove(position);
                myGvAdapter.notifyDataSetChanged();

            }
        };


        //调用初始化接口实例的方法
        myGvAdapter.setOnDeleteItemClick(listener);

        //从推荐频道里 添加 到 我的频道
        gvotherChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //先添加 后删除
                array.add(recommond.get(i));
                myGvAdapter.notifyDataSetChanged();

                recommond.remove(i);
                otherAdapter.notifyDataSetChanged();


            }
        });


        //点击事件
        txtEdit.setOnClickListener(this);
        btnfinish.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.txtEdit:
                isEdited = !isEdited;
                if (isEdited){
                    txtEdit.setText("完成");
                }else {
                    txtEdit.setText("编辑");
                }
                // 调用我的频道里的setEdited 方法 设置它是true/false
                myGvAdapter.setEdited(isEdited);
                break;

            case R.id.btn_finish:
                finish();
                break;
        }
    }
}

  // Layout

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

    <Button
        android:id="@+id/btn_finish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="完成"/>

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

        <TextView
            android:text="我的频道"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/txtEdit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:textSize="20sp"
            android:textColor="#000000"
            android:text="编辑"/>

    </RelativeLayout>

    <GridView
        android:id="@+id/gvMyChannel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="4"></GridView>

    <TextView
        android:text="推荐频道"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <GridView
        android:id="@+id/gvotherChannel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="4"></GridView>

</LinearLayout>

 

 

3.1 MyGvAdapter

package com.example.lenovo.todaynewsmouth.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.example.lenovo.todaynewsmouth.R;

import java.util.ArrayList;

/**
 * Created by lenovo on 2018/9/21.
 */

public class MyGvAdapter extends BaseAdapter{


    //1.接口回调  提供一个接口
  public interface OnDeleteItemClick{
      void onDeleteClick(int position);
    }

  //2.提供一个接口的实例
    private OnDeleteItemClick listener;

   //3.提供一个初始化实例的方法
    public void  setOnDeleteItemClick(OnDeleteItemClick listener){
        this.listener = listener;
    }

     //上下文
    private Context context;
    private ArrayList<String> list;

    public MyGvAdapter(Context context, ArrayList<String> list) {
        this.context = context;
        this.list = list;
    }


    //4.
    private boolean isEdited = false;
    public void  setEdited(boolean edited){
        isEdited = edited;
        notifyDataSetChanged();
    }


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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;

        if (view == null){
            holder = new ViewHolder();
          view = View.inflate(context, R.layout.item_mygv,null);
            holder.txtChannel = view.findViewById(R.id.txt_channel);
            holder.del = view.findViewById(R.id.del);

            view.setTag(holder);

        }else {
           holder = (ViewHolder) view.getTag();
        }
        holder.txtChannel.setText(list.get(i));

        //2.5 判断是否 显示/隐藏 删除图片
        if(isEdited){
            holder.del.setVisibility(View.VISIBLE);
        }else {
            holder.del.setVisibility(View.GONE);
        }

        //点击删除
        holder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //接口回调
                listener.onDeleteClick(i);
            }
        });


        return view;
    }

    class ViewHolder{

        private TextView txtChannel;
        private ImageView del;

    }

}

//LayOut

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/txt_channel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:text="删除"
        android:textColor="#000000"/>
    <ImageView
        android:id="@+id/del"
        android:layout_width="18dp"
        android:layout_height="18dp"
        android:src="@drawable/delete_icon_night"/>

</LinearLayout>

 

 

 

3.2 OtherFragment

package com.example.lenovo.todaynewsmouth.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.lenovo.todaynewsmouth.R;

import java.util.List;

/**
 *
 */

public class OtherAdapter extends BaseAdapter{

    private Context context;
    private List<String> list;
    //bnoolean类型
    private boolean isEdited = false;


    public OtherAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    //
    public void  setEdited(boolean edited){
        isEdited = edited;
        notifyDataSetChanged();
    }

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

    @Override
    public Object 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 holder = null;
        if(view == null){

            holder = new ViewHolder();
            view = View.inflate(context, R.layout.item_other,null);
            holder.txtChannel = view.findViewById(R.id.txt_channel);

              view.setTag(holder);
        }else {
          holder = (ViewHolder) view.getTag();
        }

        holder.txtChannel.setText(list.get(i));

        return view;
    }


    class ViewHolder{
        private TextView txtChannel;
    }
}

//LayOut

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/del"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:src="@drawable/detail_subscribe_night" />

    <TextView
        android:id="@+id/txt_channel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:text="  删除"
        android:textColor="#000000"/>

</LinearLayout>

 

 

 

// BaseApplication

package com.example.lenovo.todaynewsmouth;

import android.app.Application;
import android.graphics.Bitmap;
import android.os.Environment;

import com.nostra13.universalimageloader.cache.disc.DiskCache;
import com.nostra13.universalimageloader.cache.disc.impl.ext.LruDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.File;
import java.io.IOException;

/**
 * 
 */

public class BaseApplication extends Application{

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

        File cacheFifle = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File rootSD = Environment.getExternalStorageDirectory();
            cacheFifle = new File(rootSD, "imgs");
            if (!cacheFifle.exists()) {
                cacheFifle.mkdirs();
            }
        }


        DiskCache diskCache = null;
        try {
            diskCache = new LruDiskCache(cacheFifle, new Md5FileNameGenerator(), 50 * 1024 * 1024);
        } catch (IOException e) {
            e.printStackTrace();
        }

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();



        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
                .diskCache(diskCache)
                .defaultDisplayImageOptions(options)
                .build();

        ImageLoader.getInstance().init(configuration);
    }
}

 

 

 

//   NewsAdapter

package com.example.lenovo.todaynewsmouth;

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.List;

/**
 *
 */

public class NewsAdapter extends BaseAdapter{

    private Context context;
    private List<News.DataBeanX.DataBean> list;

    public NewsAdapter(Context context, List<News.DataBeanX.DataBean> list) {
        this.context = context;
        this.list = list;
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        News.DataBeanX.DataBean bean = list.get(position);

        if (bean.getPics().size()==0){
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder1 holder1 = null;
        ViewHolder2 holder2 = null;
        int type = getItemViewType(i);

        switch (type){
            case 1:
                if (view == null){
                    holder1 = new ViewHolder1();
                    view = View.inflate(context,R.layout.item_1,null);
                    holder1.txtTitle = view.findViewById(R.id.txt_title);
                    holder1.imgLogo = view.findViewById(R.id.img_logo);
                    view.setTag(holder1);
                }else {
               holder1 = (ViewHolder1) view.getTag();
                }

                ImageLoader.getInstance().displayImage(list.get(i).getPics().get(0),  holder1.imgLogo);
                holder1.txtTitle.setText(list.get(i).getTitle());
                break;

            case 0:

                if (view == null){
                    holder2 = new ViewHolder2();
                    view = View.inflate(context,R.layout.item_0,null);
                    holder2.txtTitle = view.findViewById(R.id.txt_title);
                    view.setTag(holder2);
                }else {
                    holder2 = (ViewHolder2) view.getTag();
                }

                holder2.txtTitle.setText(list.get(i).getTitle());
                break;
        }



        return view;
    }


    class ViewHolder1{
        private TextView txtTitle;
        private ImageView imgLogo;

    }

    class ViewHolder2{
        private TextView txtTitle;
    }
}

工具类

package com.example.lenovo.monimouth02;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 
 */

public class HttpUtils {

    public static String getHttpUrlConnextion(String urlString){
        String result = "";

        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setUseCaches(false);
            connection.setConnectTimeout(3000);
            connection.setRequestMethod("GET");
            connection.connect();


            int code = connection.getResponseCode();
            if (code == 200){
                InputStream is = connection.getInputStream();
                result = getStringInputStream(is);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

            return result;
    }

    public static String getStringInputStream(InputStream is) {

        String result = "";

        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len = -1;
            byte [] buffer = new byte[1024];
            while ((len=is.read(buffer,0,buffer.length)) !=-1){
              baos.write(buffer,0,len);
                baos.flush();
            }
            result = baos.toString();
            baos.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
           return result;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值