多條目載入(附帶透明的屬性動畫)

Castertonone發表於2018-05-03
效果圖
一.model包
1.GetNewsListener(資料獲取監聽)
public interface GetNewsListener {
       void getSuccess(String json);
       void getError(String error);
}
2.IModel
public interface IModel {
     void getNewsData(String url, Map<String,String> map,GetNewsListener getNewsListener);
}
3.ModelImpl
public class ModelImpl implements IModel{
    @Override
    public void getNewsData(String url, Map<String, String> map, final GetNewsListener getNewsListener) {
        HttpUtils httpUtils = HttpUtils.getHttpUtils();
        httpUtils.okGet(url);
        httpUtils.setOkLoadListener(new OkLoadListener() {
            @Override
            public void okLoadSuccess(String json) {
                getNewsListener.getSuccess(json);
            }
            @Override
            public void okLoadError(String error) {
                getNewsListener.getError(error);
            }
        });
    }
}

二.presenter包
1.IPresenter
public interface IPresenter {
       void showNewsToView(IModel iModel, IMainView iMainView);
}
2.PresenterImpl
public class PresenterImpl implements IPresenter{
    private static final String TAG = "---------PresenterImpl";
    @Override
    public void showNewsToView(IModel iModel, final IMainView iMainView) {
        Map<String,String> map=new HashMap<>();
        String url = HttpConfig.news_url + "?page=1";
        iModel.getNewsData(url, map, new GetNewsListener() {
            @Override
            public void getSuccess(String json) {
                Log.d(TAG, "成功: "+json);
                Gson gson = new Gson();
                NewsBean newsBean = gson.fromJson(json, NewsBean.class);
              List<NewsBean.DataBeanX.DataBean> list =  newsBean.getData().getData();
                iMainView.showNews(list);
            }
            @Override
            public void getError(String error) {
                Log.d(TAG, "錯誤: "+error);
            }
        });
    }
}

三.view
1.MainActivity
public class MainActivity extends AppCompatActivity implements IMainView{
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initDatas();
    }
    private void initDatas() {
        PresenterImpl presenter = new PresenterImpl();
        presenter.showNewsToView(new ModelImpl(),this);
    }
    private void initViews() {
        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }
    @Override
    public void showNews(final List<NewsBean.DataBeanX.DataBean> list) {
        final MyAdapter myAdapter = new MyAdapter(MainActivity.this,list);
        recyclerView.setAdapter(myAdapter);
        myAdapter.setOnLongItemClick(new MyAdapter.MyLongItemClick() {
            @Override
            public void onItemLongClick(View view, final int postion) {
                //Toast.makeText(MainActivity.this,"長按",Toast.LENGTH_SHORT).show();
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("刪除");
                builder.setMessage("確定刪除嗎");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        list.remove(postion);
                        //重新整理介面卡
                        myAdapter.notifyDataSetChanged();
                        //關閉dialog彈出框
                        dialog.dismiss();
                    }
                });
                //
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });
    }
}
1.5 activity_main(主佈局)
<android.support.constraint.ConstraintLayout 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.lenovo.moni3_20180428.view.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
2.MyAdapter (介面卡)
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
    private static final String TAG = "MyAdapter";
    private final int NEWS1 = 1;
    private final int NEWS2 = 2;
    private Context context;
    private List<NewsBean.DataBeanX.DataBean> list;
    private MyLongItemClick myLongItemClick;
    public MyAdapter(Context context, List<NewsBean.DataBeanX.DataBean> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
          MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
         holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
             @Override
             public boolean onLongClick(View v) {
                 myLongItemClick.onItemLongClick(v,position);
                 return false;
             }
         });
        //開始使用
        int type = getItemViewType(position);
        //根據型別判斷
        switch (type){
            case NEWS1:
                //移倉上面,顯示下面
                LinearLayout shang = holder.getShang();
                LinearLayout xia = holder.getXia();
                xia.setVisibility(View.VISIBLE);
                shang.setVisibility(View.GONE);
                //給下面賦值
                holder.getXia_title().setText(list.get(position).getTitle());
                holder.getXia_title1().setText(list.get(position).getTitle());
                List<String> pics = list.get(position).getPics();
                Glide.with(context).load(HttpConfig.pic_url+pics.get(0)).into(holder.getXia_pic1());
                Glide.with(context).load(HttpConfig.pic_url+pics.get(1)).into(holder.getXia_pic2());
                Glide.with(context).load(HttpConfig.pic_url+pics.get(2)).into(holder.getXia_pic3());
                holder.getXia_pinglun().setText(list.get(position).getComment_amount()+"評論");
                holder.getXia_zan().setText(list.get(position).getViews()+"贊");
                holder.getXia_pic1().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ObjectAnimator alpha = ObjectAnimator.ofFloat(holder.getXia_pic1(), "alpha", 1, 0, 1);
                        alpha.setDuration(2000);
                        alpha.start();
                    }
                });
                holder.getXia_pic2().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ObjectAnimator alpha = ObjectAnimator.ofFloat(holder.getXia_pic2(), "alpha", 1, 0, 1);
                        alpha.setDuration(2000);
                        alpha.start();
                    }
                });
                holder.getXia_pic3().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ObjectAnimator alpha = ObjectAnimator.ofFloat(holder.getXia_pic3(), "alpha", 1, 0, 1);
                        alpha.setDuration(2000);
                        alpha.start();
                    }
                });
                break;
            case NEWS2:
                //顯示上面,隱藏下面
                LinearLayout shang1 = holder.getShang();
                LinearLayout xia2 = holder.getXia();
                xia2.setVisibility(View.GONE);
                shang1.setVisibility(View.VISIBLE);
                //賦值
                holder.getTitle().setText(list.get(position).getTitle());
                holder.getTitle1().setText(list.get(position).getTitle());
                List<String> pics1 = list.get(position).getPics();
                Glide.with(context).load(HttpConfig.pic_url+pics1.get(0)).into(holder.getPic());
                holder.getPinglun().setText(list.get(position).getComment_amount()+"評論");
                holder.getZan().setText(list.get(position).getViews()+"贊");
                holder.getPic().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ObjectAnimator alpha = ObjectAnimator.ofFloat(holder.getPic(), "alpha", 1, 0, 1);
                        alpha.setDuration(2000);
                        alpha.start();
                    }
                });
                break;
        }
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    @Override
    public int getItemViewType(int position) {
        //根據資料裡面圖片的多少,確定顯示什麼的佈局
        NewsBean.DataBeanX.DataBean dataBean = list.get(position);
        int pic_amount = dataBean.getPic_amount();
        if(pic_amount>=3){
            return NEWS1;
        }else{
            return NEWS2;
        }
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView title, title1, xia_title, xia_title1, zan, xia_zan, pinglun, xia_pinglun;
        private ImageView pic, xia_pic1, xia_pic2, xia_pic3;
        private LinearLayout shang,xia;
        public MyViewHolder(View itemView) {
            super(itemView);
            pic= (ImageView) itemView.findViewById(R.id.pic);
            xia_pic1= (ImageView) itemView.findViewById(R.id.xia_pic1);
            xia_pic2= (ImageView) itemView.findViewById(R.id.xia_pic2);
            xia_pic3= (ImageView) itemView.findViewById(R.id.xia_pic3);
            title = (TextView) itemView.findViewById(R.id.title);
            title1 = (TextView) itemView.findViewById(R.id.title1);
            xia_title = (TextView) itemView.findViewById(R.id.xia_title);
            xia_title1 = (TextView) itemView.findViewById(R.id.xia_title2);
            zan = (TextView) itemView.findViewById(R.id.zan);
            xia_zan = (TextView) itemView.findViewById(R.id.xia_zan);
            pinglun = (TextView) itemView.findViewById(R.id.pinglun);
            xia_pinglun = (TextView) itemView.findViewById(R.id.xian_pinglun);
            //上下的佈局
            shang= (LinearLayout) itemView.findViewById(R.id.shang);
            xia= (LinearLayout) itemView.findViewById(R.id.xia);
        }
        public MyViewHolder(View itemView, TextView title, TextView title1, TextView xia_title, TextView xia_title1, TextView zan, TextView xia_zan, TextView pinglun, TextView xia_pinglun, ImageView pic, ImageView xia_pic1, ImageView xia_pic2, ImageView xia_pic3, LinearLayout shang, LinearLayout xia) {
            super(itemView);
            this.title = title;
            this.title1 = title1;
            this.xia_title = xia_title;
            this.xia_title1 = xia_title1;
            this.zan = zan;
            this.xia_zan = xia_zan;
            this.pinglun = pinglun;
            this.xia_pinglun = xia_pinglun;
            this.pic = pic;
            this.xia_pic1 = xia_pic1;
            this.xia_pic2 = xia_pic2;
            this.xia_pic3 = xia_pic3;
            this.shang = shang;
            this.xia = xia;
        }
        public TextView getTitle() {
            return title;
        }
        public void setTitle(TextView title) {
            this.title = title;
        }
        public TextView getTitle1() {
            return title1;
        }
        public void setTitle1(TextView title1) {
            this.title1 = title1;
        }
        public TextView getXia_title() {
            return xia_title;
        }
        public void setXia_title(TextView xia_title) {
            this.xia_title = xia_title;
        }
        public TextView getXia_title1() {
            return xia_title1;
        }
        public void setXia_title1(TextView xia_title1) {
            this.xia_title1 = xia_title1;
        }
        public TextView getZan() {
            return zan;
        }
        public void setZan(TextView zan) {
            this.zan = zan;
        }
        public TextView getXia_zan() {
            return xia_zan;
        }
        public void setXia_zan(TextView xia_zan) {
            this.xia_zan = xia_zan;
        }
        public TextView getPinglun() {
            return pinglun;
        }
        public void setPinglun(TextView pinglun) {
            this.pinglun = pinglun;
        }
        public TextView getXia_pinglun() {
            return xia_pinglun;
        }
        public void setXia_pinglun(TextView xia_pinglun) {
            this.xia_pinglun = xia_pinglun;
        }
        public ImageView getPic() {
            return pic;
        }
        public void setPic(ImageView pic) {
            this.pic = pic;
        }
        public ImageView getXia_pic1() {
            return xia_pic1;
        }
        public void setXia_pic1(ImageView xia_pic1) {
            this.xia_pic1 = xia_pic1;
        }
        public ImageView getXia_pic2() {
            return xia_pic2;
        }
        public void setXia_pic2(ImageView xia_pic2) {
            this.xia_pic2 = xia_pic2;
        }
        public ImageView getXia_pic3() {
            return xia_pic3;
        }
        public void setXia_pic3(ImageView xia_pic3) {
            this.xia_pic3 = xia_pic3;
        }
        public LinearLayout getShang() {
            return shang;
        }
        public void setShang(LinearLayout shang) {
            this.shang = shang;
        }
        public LinearLayout getXia() {
            return xia;
        }
        public void setXia(LinearLayout xia) {
            this.xia = xia;
        }
    }
    public interface MyLongItemClick{
        void onItemLongClick(View view,int postion);
    }
    public void setOnLongItemClick(MyLongItemClick myLongItemClick){
        this.myLongItemClick = myLongItemClick;
    }
}
2.5 item_layout(對應的佈局)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--上半部-->
    <LinearLayout
        android:id="@+id/shang"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">
            <TextView
                android:singleLine="true"
                android:textSize="20sp"
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="標題" />
            <TextView
                android:singleLine="true"
                android:textSize="20sp"
                android:id="@+id/title1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="副標題" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="horizontal">
                <View
                    android:layout_width="0dp"
                    android:layout_height="0.1dp"
                    android:layout_weight="1" />
                <TextView
                    android:id="@+id/pinglun"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:text="評論" />
                <TextView
                    android:id="@+id/zan"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:text="贊" />
            </LinearLayout>
        </LinearLayout>
        <ImageView
            android:id="@+id/pic"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@mipmap/ic_launcher" />
    </LinearLayout>
    <!--下半部-->
    <LinearLayout
        android:id="@+id/xia"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:singleLine="true"
            android:textSize="20sp"
            android:id="@+id/xia_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="標題" />
        <TextView
            android:singleLine="true"
            android:textSize="20sp"
            android:id="@+id/xia_title2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="副標題" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/xia_pic1"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:src="@mipmap/ic_launcher" />
            <ImageView
                android:id="@+id/xia_pic2"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:src="@mipmap/ic_launcher" />
            <ImageView
                android:id="@+id/xia_pic3"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:src="@mipmap/ic_launcher" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="0.1dp"
                android:layout_weight="1" />
            <TextView
                android:id="@+id/xian_pinglun"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="評論" />
            <TextView
                android:id="@+id/xia_zan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="贊" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
由於找的資料介面比較複雜,需要在圖片獲取時把地址重新對一下,便把介面寫了
(工具類)HttpConfig
public class HttpConfig {
    public static String pic_url = "http://365jia.cn/uploads/";
    public static String news_url = "http://365jia.cn/news/api3/365jia/news/headline";
}
依賴
compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.okhttp3:okhttp:3.3.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.android.support:recyclerview-v7:23.1.1' //RecyclerView
許可權
 <uses-permission android:name="android.permission.INTERNET"/>

相關文章