| @ -1,6 +0,0 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project version="4"> | ||||
|   <component name="RenderSettings"> | ||||
|     <option name="showDecorations" value="true" /> | ||||
|   </component> | ||||
| </project> | ||||
| @ -0,0 +1,114 @@ | ||||
| package com.example.register; | ||||
| 
 | ||||
| import android.app.Activity; | ||||
| import android.app.FragmentManager; | ||||
| import android.app.FragmentTransaction; | ||||
| import android.os.Bundle; | ||||
| import android.widget.ListView; | ||||
| 
 | ||||
| import androidx.annotation.Nullable; | ||||
| 
 | ||||
| import com.example.register.R; | ||||
| import com.example.register.entity.Product; | ||||
| import com.example.register.adapter.Adapter; | ||||
| import com.example.register.fragment.SetDetailFragment; | ||||
| 
 | ||||
| import java.math.BigDecimal; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| 
 | ||||
| public class CategoryActivity extends Activity { | ||||
|     public OnChangeListener onchangedListener; | ||||
|     private List<Product> productList; | ||||
|     private List<String> productCategory = new ArrayList<>(); | ||||
|     private ListView titleList; | ||||
|     private Adapter adapter; | ||||
| 
 | ||||
|     @Override | ||||
|     public void onCreate(@Nullable Bundle savedInstanceState) { | ||||
|         super.onCreate(savedInstanceState); | ||||
|         setContentView(R.layout.content_category); | ||||
|         initData(); | ||||
|         init(); | ||||
|         SetDetailFragment fragment = new SetDetailFragment(); | ||||
|         FragmentManager fragmentManager = getFragmentManager(); | ||||
|         FragmentTransaction transaction = fragmentManager.beginTransaction(); | ||||
|         transaction.replace(R.id.category_detail, fragment); | ||||
|         transaction.commit(); | ||||
|         titleList.setOnItemClickListener((parent, view, position, id) -> { | ||||
|             adapter.setSelectedPosition(position); | ||||
|             adapter.notifyDataSetInvalidated(); | ||||
|             if (onchangedListener != null) { | ||||
|                 onchangedListener.changeText(productList.get(position)); | ||||
|             } | ||||
|         }); | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     public void setOnChangeListener(OnChangeListener onChangeListener) { | ||||
|         this.onchangedListener = onChangeListener; | ||||
|     } | ||||
| 
 | ||||
|     public interface OnChangeListener { | ||||
|         void changeText(Product product); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 初始化数据 | ||||
|      */ | ||||
|     private void initData() { | ||||
|         productList = new ArrayList<>(); | ||||
|         productCategory.add("热销"); | ||||
|         productCategory.add("严选"); | ||||
|         productCategory.add("小食"); | ||||
|         productCategory.add("啤酒"); | ||||
|         productCategory.add("饮料"); | ||||
| 
 | ||||
| 
 | ||||
|         Product product = new Product(); | ||||
|         product.setImageUrlId(R.drawable.rou); | ||||
|         product.setProductName("肥肉饭"); | ||||
|         product.setProductPrice(new BigDecimal("19.9")); | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|         Product product1 = new Product(); | ||||
|         product1.setImageUrlId(R.drawable.haorou); | ||||
|         product1.setProductName("严选套餐"); | ||||
|         product1.setProductPrice(new BigDecimal("29.9")); | ||||
| 
 | ||||
| 
 | ||||
|         Product product2 = new Product(); | ||||
|         product2.setImageUrlId(R.drawable.xiaoshi); | ||||
|         product2.setProductName("小食"); | ||||
|         product2.setProductPrice(new BigDecimal("9.9")); | ||||
| 
 | ||||
|         Product product3 = new Product(); | ||||
|         product3.setImageUrlId(R.drawable.shui); | ||||
|         product3.setProductName("矿泉水"); | ||||
|         product3.setProductPrice(new BigDecimal("1.9")); | ||||
| 
 | ||||
| 
 | ||||
|         Product product4 = new Product(); | ||||
|         product4.setImageUrlId(R.drawable.yinliao); | ||||
|         product4.setProductName("雪碧"); | ||||
|         product4.setProductPrice(new BigDecimal("1.9")); | ||||
| 
 | ||||
| 
 | ||||
|         productList.add(product); | ||||
|         productList.add(product1); | ||||
|         productList.add(product2); | ||||
|         productList.add(product3); | ||||
|         productList.add(product4); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 初始化组件 | ||||
|      */ | ||||
|     private void init() { | ||||
|         titleList = findViewById(R.id.category_title_list); | ||||
|         adapter = new Adapter(productCategory, CategoryActivity.this); | ||||
|         titleList.setAdapter(adapter); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,50 @@ | ||||
| package com.example.register; | ||||
| 
 | ||||
| import android.annotation.SuppressLint; | ||||
| import android.app.Fragment; | ||||
| import android.os.Bundle; | ||||
| import android.util.Log; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.ImageView; | ||||
| import android.widget.TextView; | ||||
| 
 | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.annotation.Nullable; | ||||
| 
 | ||||
| import java.util.Objects; | ||||
| 
 | ||||
| public class SetDetailFragment extends Fragment { | ||||
|     private View view; | ||||
|     private ImageView imageView; | ||||
|     private TextView nameText, priceText; | ||||
| 
 | ||||
|     @SuppressLint("SetTextI18n") | ||||
|     @Nullable | ||||
|     @Override | ||||
|     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||||
|         view = inflater.inflate(R.layout.categoty_detail_content, container, false); | ||||
|         if (view != null) { | ||||
|             init(); | ||||
|         } | ||||
|         CategoryActivity categoryActivity = (CategoryActivity) getActivity(); | ||||
|         Objects.requireNonNull(categoryActivity).setOnChangeListener(product -> { | ||||
|             Log.i("sss", "onCreateView: " + product.getProductName()); | ||||
|             imageView.setBackgroundResource(product.getImageUrlId()); | ||||
|             nameText.setText(product.getProductName()); | ||||
|             priceText.setText(product.getProductPrice().toString()); | ||||
|         }); | ||||
|         return view; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 内容组件初始化 | ||||
|      */ | ||||
|     private void init() { | ||||
|         imageView = view.findViewById(R.id.category_product_image); | ||||
|         nameText = view.findViewById(R.id.category_product_name); | ||||
|         priceText = view.findViewById(R.id.category_product_price); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,69 @@ | ||||
| package com.example.register.adapter; | ||||
| 
 | ||||
| import android.content.Context; | ||||
| import android.graphics.Color; | ||||
| import android.util.Log; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.BaseAdapter; | ||||
| import android.widget.TextView; | ||||
| 
 | ||||
| import com.example.register.R; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class Adapter extends BaseAdapter { | ||||
|     private List<String> productCategory; | ||||
|     private LayoutInflater layoutInflater; | ||||
|     private int selectionPosition = -1; | ||||
| 
 | ||||
|     public Adapter(List<String> productCategory, Context context) { | ||||
|         this.productCategory = productCategory; | ||||
|         this.layoutInflater = LayoutInflater.from(context); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public int getCount() { | ||||
|         return productCategory.size(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public Object getItem(int position) { | ||||
|         return productCategory.get(position); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public long getItemId(int position) { | ||||
|         return position; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public View getView(int position, View convertView, ViewGroup parent) { | ||||
|         ViewHolder viewHolder = null; | ||||
|         if (convertView == null) { | ||||
|             viewHolder = new ViewHolder(); | ||||
|             convertView = layoutInflater.inflate(R.layout.category_list_item, null); | ||||
|             Log.i("adapts", "getView: " + convertView); | ||||
|             viewHolder.tv = convertView.findViewById(R.id.categor_titles); | ||||
|             convertView.setTag(viewHolder); | ||||
|         } else { | ||||
|             viewHolder = (ViewHolder) convertView.getTag(); | ||||
|         } | ||||
|         viewHolder.tv.setText(productCategory.get(position)); | ||||
|         if (selectionPosition == position) { | ||||
|             viewHolder.tv.setBackgroundColor(Color.YELLOW); | ||||
|         } else { | ||||
|             viewHolder.tv.setBackgroundColor(Color.WHITE); | ||||
|         } | ||||
|         return convertView; | ||||
|     } | ||||
| 
 | ||||
|     public void setSelectedPosition(int position) { | ||||
|         this.selectionPosition = position; | ||||
|     } | ||||
| 
 | ||||
|     class ViewHolder { | ||||
|         TextView tv; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,34 @@ | ||||
| package com.example.register.entity; | ||||
| 
 | ||||
| import java.math.BigDecimal; | ||||
| 
 | ||||
| public class Product { | ||||
| 
 | ||||
|     public String getProductName() { | ||||
|         return productName; | ||||
|     } | ||||
| 
 | ||||
|     public void setProductName(String productName) { | ||||
|         this.productName = productName; | ||||
|     } | ||||
| 
 | ||||
|     public BigDecimal getProductPrice() { | ||||
|         return productPrice; | ||||
|     } | ||||
| 
 | ||||
|     public void setProductPrice(BigDecimal productPrice) { | ||||
|         this.productPrice = productPrice; | ||||
|     } | ||||
| 
 | ||||
|     public Integer getImageUrlId() { | ||||
|         return imageUrlId; | ||||
|     } | ||||
| 
 | ||||
|     public void setImageUrlId(Integer imageUrlId) { | ||||
|         this.imageUrlId = imageUrlId; | ||||
|     } | ||||
| 
 | ||||
|     private Integer imageUrlId; | ||||
|     private String productName; | ||||
|     private BigDecimal productPrice; | ||||
| } | ||||
| @ -0,0 +1,53 @@ | ||||
| package com.example.register.fragment; | ||||
| 
 | ||||
| import android.annotation.SuppressLint; | ||||
| import android.app.Fragment; | ||||
| import android.os.Bundle; | ||||
| import android.util.Log; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.ImageView; | ||||
| import android.widget.TextView; | ||||
| 
 | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.annotation.Nullable; | ||||
| 
 | ||||
| import com.example.register.R; | ||||
| import com.example.register.CategoryActivity; | ||||
| 
 | ||||
| import java.util.Objects; | ||||
| 
 | ||||
| public class    SetDetailFragment extends Fragment { | ||||
|     private View view; | ||||
|     private ImageView imageView; | ||||
|     private TextView nameText, priceText; | ||||
| 
 | ||||
|     @SuppressLint("SetTextI18n") | ||||
|     @Nullable | ||||
|     @Override | ||||
|     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||||
|         view = inflater.inflate(R.layout.categoty_detail_content, container, false); | ||||
|         if (view != null) { | ||||
|             init(); | ||||
|         } | ||||
|         CategoryActivity categoryActivity = (CategoryActivity) getActivity(); | ||||
|         Objects.requireNonNull(categoryActivity).setOnChangeListener(product -> { | ||||
|             Log.i("sss", "onCreateView: " + product.getProductName()); | ||||
|             imageView.setBackgroundResource(product.getImageUrlId()); | ||||
|             nameText.setText(product.getProductName()); | ||||
|             priceText.setText(product.getProductPrice().toString()); | ||||
|         }); | ||||
|         return view; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 内容组件初始化 | ||||
|      */ | ||||
|     private void init() { | ||||
|         imageView = view.findViewById(R.id.category_product_image); | ||||
|         nameText = view.findViewById(R.id.category_product_name); | ||||
|         priceText = view.findViewById(R.id.category_product_price); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| After Width: | Height: | Size: 231 B | 
| After Width: | Height: | Size: 297 KiB | 
| After Width: | Height: | Size: 10 KiB | 
| After Width: | Height: | Size: 1.3 KiB | 
| After Width: | Height: | Size: 51 KiB | 
| After Width: | Height: | Size: 293 KiB | 
| After Width: | Height: | Size: 258 KiB | 
| @ -0,0 +1,16 @@ | ||||
| <?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"> | ||||
| 
 | ||||
|     <GridView | ||||
|         android:id="@+id/category_detail_list" | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:numColumns="3" | ||||
|         android:verticalSpacing="10dp" | ||||
|         android:horizontalSpacing="10dp" | ||||
|         android:gravity="center"/> | ||||
| 
 | ||||
| </LinearLayout> | ||||
| @ -0,0 +1,14 @@ | ||||
| <?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"> | ||||
| 
 | ||||
|     <TextView | ||||
|         android:id="@+id/categor_titles" | ||||
|         android:layout_width="match_parent" | ||||
|         android:gravity="center" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:textSize="18sp" | ||||
|         android:text="标题" /> | ||||
| </LinearLayout> | ||||
| @ -0,0 +1,31 @@ | ||||
| <?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"> | ||||
| 
 | ||||
|     <ImageView | ||||
|         android:id="@+id/category_product_image" | ||||
|         android:layout_width="90dp" | ||||
|         android:layout_height="90dp" | ||||
|         android:layout_gravity="center" /> | ||||
| 
 | ||||
|     <TextView | ||||
|         android:id="@+id/category_product_name" | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="30dp" | ||||
|         android:layout_gravity="center" | ||||
|         android:layout_marginTop="2dp" | ||||
|         android:textColor="#050505" | ||||
|         android:textSize="16sp" /> | ||||
| 
 | ||||
|     <TextView | ||||
|         android:id="@+id/category_product_price" | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="30dp" | ||||
|         android:layout_gravity="center" | ||||
|         android:layout_marginTop="2dp" | ||||
|         android:gravity="center" | ||||
|         android:textColor="#050505" | ||||
|         android:textSize="16sp" /> | ||||
| </LinearLayout> | ||||
| @ -0,0 +1,54 @@ | ||||
| <?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:background="#E8E8E8" | ||||
|     android:orientation="vertical"> | ||||
| 
 | ||||
|     <!--标题--> | ||||
|     <LinearLayout | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="50dp" | ||||
|         android:background="#EFB81C" | ||||
|         android:orientation="horizontal"> | ||||
| 
 | ||||
|         <ImageView | ||||
|             android:id="@+id/category_return" | ||||
|             android:layout_width="wrap_content" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:layout_gravity="center" | ||||
|             android:src="@drawable/arrow_left" /> | ||||
| 
 | ||||
|         <TextView | ||||
|             android:layout_width="0dp" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:layout_marginTop="12dp" | ||||
|             android:layout_weight="1" | ||||
|             android:gravity="center" | ||||
|             android:text="商品种类" | ||||
|             android:textColor="#FFF" | ||||
|             android:textSize="20sp" /> | ||||
|     </LinearLayout> | ||||
| 
 | ||||
|     <!--分类标题和内容--> | ||||
|     <LinearLayout | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="match_parent" | ||||
|         android:orientation="horizontal"> | ||||
|         <!--标题--> | ||||
|         <!--内容--> | ||||
| 
 | ||||
|         <ListView | ||||
|             android:id="@+id/category_title_list" | ||||
|             android:layout_width="0dp" | ||||
|             android:layout_height="match_parent" | ||||
|             android:layout_weight="1" /> | ||||
| 
 | ||||
|         <FrameLayout | ||||
|             android:id="@+id/category_detail" | ||||
|             android:layout_width="0dp" | ||||
|             android:layout_height="match_parent" | ||||
|             android:layout_weight="3" /> | ||||
|     </LinearLayout> | ||||
| 
 | ||||
| </LinearLayout> | ||||