屬性動畫跳轉展示資料(可切換可搜尋)

Castertonone發表於2018-05-03
一.http
1.HttpConfig
public class HttpConfig {
    public static String goodslist_url = "http://120.27.23.105/product/searchProducts";
}

二.model
1.GetGoodsListener (監聽介面)
public interface GetGoodsListener {
    void getSuccess(String json);
    void getError(String error);
}
2.IModel
public interface IModel {
    //獲取商品列表資料
    void getGoodsListData(String url, Map<String,String> map,GetGoodsListener getGoodsListener);
}
3.ModelImpl
public class ModelImpl implements IModel {
    @Override
    public void getGoodsListData(String url, Map<String, String> map, final GetGoodsListener getGoodsListener) {
        HttpUtils httpUtils = HttpUtils.getHttpUtils();
        httpUtils.okPost(url, map);
        httpUtils.setOkLoadListener(new OkLoadListener() {
            @Override
            public void okLoadSuccess(String json) {
                getGoodsListener.getSuccess(json);
            }
            @Override
            public void okLoadError(String error) {
                getGoodsListener.getError(error);
            }
        });
    }
}

三.presenter
1.IPresenter
public interface IPresenter {
    void showGoodsListToView(IModel iModel, IMainView iMainView);
}
2.PresenterImpl
public class PresenterImpl implements IPresenter {
    private static final String TAG = "PresenterImpl---";
    @Override
    public void showGoodsListToView(IModel iModel, final IMainView iMainView) {
        Map<String,String> map = new HashMap<>();
        map.put("keywords",iMainView.getContent());
        map.put("page","1");
        iModel.getGoodsListData(HttpConfig.goodslist_url, map, new GetGoodsListener() {
            @Override
            public void getSuccess(String json) {
                Log.d(TAG, "資料----: "+json);
                Gson gson = new Gson();
                GoodsListBean goodsListBean = gson.fromJson(json, GoodsListBean.class);
                List<GoodsListBean.DataBean> list = goodsListBean.getData();
                //放入view
                iMainView.showGoodsList(list);
            }
            @Override
            public void getError(String error) {
                Log.d(TAG, "getError: "+error);
            }
        });
    }
}

四.view
1.IMainView
public interface IMainView {
    //顯示商品也
    void showGoodsList(List<GoodsListBean.DataBean> list);
    //獲取輸入框內容
    String getContent();
}
2.SplashActivity (屬性動畫跳轉介面)
public class SplashActivity extends AppCompatActivity {
    private int width;
    private int height;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash2);
        //獲取螢幕寬高
        WindowManager wm = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        width = wm.getDefaultDisplay().getWidth();
        height = wm.getDefaultDisplay().getHeight();
        //初始化頁面
        initViews();
    }
    private void initViews() {
        ImageView imageView=findViewById(R.id.splash_pic);
        //屬性動畫
        ObjectAnimator translationY = ObjectAnimator.ofFloat(imageView, "translationY", 0, height / 2);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 2, 1);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 2, 1);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 0, 1);
        ObjectAnimator rotation = ObjectAnimator.ofFloat(imageView, "rotation", 360);
        //新增到動畫集合
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(translationY,scaleX,scaleY,alpha,rotation);
        //設定
        animatorSet.setDuration(3000);
        animatorSet.start();
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }
            @Override
            public void onAnimationEnd(Animator animation) {
                //動畫結束的時候,跳轉頁面
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
            }
            @Override
            public void onAnimationCancel(Animator animation) {
            }
            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
    }
}
2.5  activity_splash2(動畫對應的佈局)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/splash_pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@mipmap/ic_launcher" />
</LinearLayout>
3.MainActivity (主方法)
public class MainActivity extends AppCompatActivity implements IMainView, View.OnClickListener {
    private static final String TAG = "MainActivity-----";
    private RecyclerView recyclerView;
    private MySearchView mySearchView;
    private TextView sousuo;
    private PresenterImpl presenter;
    private boolean flag = true;
    private ImageView change;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化介面
        initViews();
        //初始化資料
        initDatas();
    }
    private void initDatas() {
        presenter = new PresenterImpl();
        presenter.showGoodsListToView(new ModelImpl(), this);
    }
    private void initViews() {
        recyclerView = findViewById(R.id.recyclerView);
        mySearchView = findViewById(R.id.mysearch);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        sousuo = findViewById(R.id.sousuo);
        sousuo.setOnClickListener(this);
        change = findViewById(R.id.change);
        change.setOnClickListener(this);
    }
    //顯示商品列表
    @Override
    public void showGoodsList(List<GoodsListBean.DataBean> list) {
        Log.d(TAG, "showGoodsList: " + list);
        MyAdapter myAdapter = new MyAdapter(MainActivity.this, list);
        recyclerView.setAdapter(myAdapter);
    }
    @Override
    public String getContent() {
        String content = mySearchView.getContent();
        if (TextUtils.isEmpty(content)) {
            content = "筆記本";
        }
        return content;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sousuo:
                presenter.showGoodsListToView(new ModelImpl(), this);
                break;
            case R.id.change:
                if (flag) {
                    recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this, 2));
                    change.setImageResource(R.drawable.kind_liner);
                } else {
                    recyclerView.setLayoutManager(new LinearLayoutManager(this));
                    change.setImageResource(R.drawable.kind_grid);
                }
                flag = !flag;
                break;
        }
    }
}
3.5(主佈局)
<LinearLayout 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:orientation="vertical"
    tools:context="com.daydayup.day15_lianxi.view.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="商品列表"
            android:textSize="25sp" />
        <ImageView
            android:id="@+id/change"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/kind_grid" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.75dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <com.daydayup.day15_lianxi.view.MySearchView
            android:id="@+id/mysearch"
            android:layout_width="0dp"
            android:layout_height="55dp"
            android:layout_weight="1"></com.daydayup.day15_lianxi.view.MySearchView>
        <TextView
            android:id="@+id/sousuo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="搜尋"
            android:textSize="20sp" />
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
4.MyAdapter (介面卡)
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private Context context;
    private List<GoodsListBean.DataBean> list;
    public MyAdapter(Context context, List<GoodsListBean.DataBean> list) {
        this.list = list;
        this.context = context;
    }

    @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(MyViewHolder holder, int position) {
        String pic_url = list.get(position).getImages().split("\\|")[0];
        Glide.with(context).load(pic_url).into(holder.getImageView());

        holder.getTitle().setText(list.get(position).getTitle());
        holder.getPrice().setText(list.get(position).getPrice() + "");
        holder.getPrice2().setText(list.get(position).getBargainPrice() + "");
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder {
        private final ImageView imageView;
        private final TextView title;
        private final TextView price;
        private final TextView price2;
        public MyViewHolder(View itemView) {
            super(itemView);
            //找控制元件
            imageView = itemView.findViewById(R.id.item_pic);
            title = itemView.findViewById(R.id.itme_title);
            price = itemView.findViewById(R.id.item_price);
            price2 = itemView.findViewById(R.id.item_price2);
        }
        public MyViewHolder(View itemView, ImageView imageView, TextView title, TextView price, TextView price2) {
            super(itemView);
            this.imageView = imageView;
            this.title = title;
            this.price = price;
            this.price2 = price2;
        }
        public ImageView getImageView() {
            return imageView;
        }
        public TextView getTitle() {
            return title;
        }
        public TextView getPrice() {
            return price;
        }
        public TextView getPrice2() {
            return price2;
        }
    }

}
4.5  item_layout(對應的佈局)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/item_pic"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/itme_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="標題" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/item_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="價格" />
            <TextView
                android:id="@+id/item_price2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="折扣價格" />
        </LinearLayout>

    </LinearLayout>
</LinearLayout>
5.MySearchView (搜尋框)
public class MySearchView extends LinearLayout {
    private EditText search_content;
    //1.
    //直接new的時候
    public MySearchView(Context context) {
        this(context, null);
    }
    public MySearchView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    //在佈局檔案裡面時候控制元件的時候
    public MySearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化
        View view = View.inflate(context, R.layout.layout_search, this);
        search_content = view.findViewById(R.id.search_content);
//        search_content.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                search_content.setFocusable(true);
//            }
//        });
    }
    //獲取輸入的內容
    public String getContent() {
        return search_content.getText().toString();
    }
}
5.5  layout_search(搜尋框對應的佈局)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/search_bg"
    android:orientation="horizontal"
    android:padding="8dp">
    <ImageView
        android:layout_width="@dimen/search_height"
        android:layout_height="@dimen/search_height"
        android:src="@drawable/a_4" />
    <EditText
        android:id="@+id/search_content"
        android:layout_width="0dp"
        android:layout_height="@dimen/search_height"
        android:layout_weight="1"
        android:background="@null" />
    <ImageView
        android:layout_width="@dimen/search_height"
        android:layout_height="@dimen/search_height"
        android:src="@drawable/root" />
</LinearLayout>

一些補充
1.寫在drawable裡的(search_bg)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="50dp"
        android:height="@dimen/search_height" />
    <solid android:color="#55999999" />
    <corners android:radius="18dp" />
</shape>
2.寫在values裡的(dimens)
<resources>
    <dimen name="search_height">30dp</dimen>
</resources>
3.依賴
 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:26.0.0-alpha1'
4.許可權
<uses-permission android:name="android.permission.INTERNET"/>
//需要把SplashActivity設為主啟動頁面
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".view.MainActivity"></activity>
        <activity android:name=".view.SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

相關文章