day7+第一周 周总结

本周总要是学习android的一些简单布局——LinearLayout, RelativeLayout, FrameLayout

LinearLayout :线性布局

orientation="horizontal"//水平方向
只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。
android:orientation="vertical"//竖直方向
只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

RelativeLayout:相对布局,要有参照物,一些重要的属性(这些天我写布局用到的比较重要的属性)

android:layout_below      在某元素的下方

android:layout_above      在某元素的的上方

android:layout_toLeftOf   在某元素的左边 

android:layout_toRightOf  在某元素的右边 

还有就是一些margin的属性

android:visibility="nvisible":不显示,但保留所占的空间
android:visibility="visible":正常显示
android:visibility="gone":不显示,且不保留所占的空间

实现商品展示

布局代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:background="#EFFBFB">
    <RelativeLayout
        android:id="@+id/ralat"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/line_lay"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:orientation="vertical"
        >
        <SearchView
            android:id="@+id/searchView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:iconifiedByDefault="false"
            android:queryHint="搜索内容" />


    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/buy_1"
        android:layout_below="@+id/line_lay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    </RelativeLayout>
</FrameLayout>

BuyFragment

package Home;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.TextView;

import com.example.expressdelivery.R;

import java.util.List;

import Home.Data.Goods;
import Home.Data.GoodsFactory;

/**
 * A simple {@link Fragment} subclass.

 * create an instance of this fragment.
 */
public class BuyFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private RecyclerView mRecyc1;
    private BuyAdapter buyAdapter;
    private Context mcontext;
    private SearchView mSearch;
    private List<Goods> goods;

    public BuyFragment(Context context) {
        this.mcontext = context;
        // Required empty public constructor
    }

    public BuyFragment() {

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_buy, container, false);
        goods= GoodsFactory.createMeizis(10);

        buyAdapter=new BuyAdapter(getActivity(),goods);
        mRecyc1=(RecyclerView) view.findViewById(R.id.buy_1);
        mSearch = (SearchView) view.findViewById(R.id.searchView);
        mSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            // 当点击搜索按钮时触发该方法

            @Override

            public boolean onQueryTextSubmit(String query) {

                return false;

            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        mRecyc1.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
        mRecyc1.setAdapter(buyAdapter);
        return view;
    }
}

BuyAdapter

package Home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.expressdelivery.HomeActivity;
import com.example.expressdelivery.R;

import java.io.File;
import java.util.List;

import Home.Data.Goods;
import Home.Data.Zuoye;


public class BuyAdapter extends RecyclerView.Adapter<BuyAdapter.BuyViewHolder>{
    private Context mcontext;
    private List<Goods> goods;
    private List<Zuoye> Zuoyes;
    private ImageView imageView2;
    public BuyAdapter(Context context,List<Goods> goods){
        this.mcontext=context;
        this.goods=goods;
    }

    @NonNull
    @Override
    public BuyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BuyViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.layout_buy_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull BuyViewHolder holder, final int position) {
        final Goods good=goods.get(position);
        Glide.with(mcontext)
                .load(good.getImageUrl())
                .into(holder.imageView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mcontext,"欢迎来到"+(good.getName())+"商店",Toast.LENGTH_SHORT).show();
                DetailFragment detailFragment=new DetailFragment(mcontext,good,position);
                FragmentTransaction transaction=((HomeActivity)mcontext).getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container,detailFragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });
        holder.textView.setText(good.getName());
        holder.textView2.setText("$"+good.getPrice());
        holder.textView3.setText(good.getAddress());
    }

    @Override
    public int getItemCount() {
        return goods.size();
    }



    class BuyViewHolder extends RecyclerView.ViewHolder{
        private TextView textView;
        private TextView textView2;
        private ImageView imageView;
        private TextView textView3;
        public BuyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=(TextView)itemView.findViewById(R.id.tv_goods_name);
            textView2=(TextView)itemView.findViewById(R.id.tv_goods_price);
            textView3=(TextView)itemView.findViewById(R.id.tv_type_size);
            imageView=(ImageView)itemView.findViewById(R.id.iv_pic);
        }
    }
}

同时每一个item设计了点击事件进入下个Fragment也就是详情展示

效果图

 

posted @ 2020-08-09 23:51  晨起  阅读(158)  评论(0编辑  收藏  举报