瀑布流实现

master
bullfrog 3 years ago
parent 67b2ef13f6
commit 60f32b1980

@ -0,0 +1 @@
Android_Couser_Design

@ -3,6 +3,8 @@
<component name="DesignSurface">
<option name="filePathToZoomLevelMap">
<map>
<entry key="..\:/Users/HP/Desktop/android_Course_setting/androidF/android_course_design/app/src/main/res/layout/goods_show_item.xml" value="0.25" />
<entry key="..\:/Users/HP/Desktop/android_Course_setting/androidF/android_course_design/app/src/main/res/layout/item_stagger.xml" value="0.5" />
<entry key="..\:/andriod/Android_Couser_Design/app/src/main/res/drawable/baseline_account_circle_18.xml" value="0.1345" />
<entry key="..\:/andriod/Android_Couser_Design/app/src/main/res/drawable/baseline_account_circle_20.xml" value="0.1345" />
<entry key="..\:/andriod/Android_Couser_Design/app/src/main/res/drawable/baseline_account_circle_24.xml" value="0.1345" />

@ -85,7 +85,6 @@ public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.ViewHolder>
return tvTitle;
}
public TextView getTvCount() {
return tvCount;
}

@ -0,0 +1,133 @@
package com.android.activity.adapter;
import static android.content.ContentValues.TAG;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.android.R;
import com.android.bean.Good;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
/**
* @author: HP
* @date: 2022/6/20
*/
public class StaggerAdapter extends RecyclerView.Adapter<StaggerAdapter.InnerHolder> {
protected final ArrayList<Good> mData;
private RecyclerView mList;
Context context;
public StaggerAdapter(ArrayList<Good> mData, Context context) {
Log.d("TAG", "StaggerAdapter: ");
this.context=context;
this.mData = mData;
}
public StaggerAdapter(List<Good> mData) {
this.context=context;
this.mData = (ArrayList<Good>) mData;
}
@NonNull
@Override
public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Log.d("TAG", "onCreateViewHolder: ");
//传入这个view是item的界面
View view = View.inflate(parent.getContext(), R.layout.item_stagger, null);
//创建InnerHolder
return new InnerHolder(view);
}
//绑定holder一般用来设置数据
@Override
public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
String strTvTitle;
String strTvCount;
String imageUrl;
if (mData.get(position) != null){
strTvTitle = mData.get(position).getContent();
strTvCount = mData.get(position).getPrice().toString();
imageUrl = (String) mData.get(position).getImageUrlList().get(0);
Log.d("TAG", "Title:"+strTvTitle);
Log.d("TAG", "Count:"+strTvCount);
Log.e("imageUrl", (String) mData.get(position).getImageUrlList().get(0));
holder.getTvTitle().setText(strTvTitle);
holder.getTvCount().setText(strTvCount);
Glide.with(context)
.load(imageUrl)
.into(holder.getIvGoods());
}
}
@Override
public int getItemCount() {
if (mData != null) {
return mData.size();
}
return 0;
}
public class InnerHolder extends RecyclerView.ViewHolder {
private final ImageView mImage;
private final TextView mContent;
private final TextView mPrice;
public InnerHolder(View itemView){
super(itemView);
//找到条目的空间
mImage = itemView.findViewById(R.id.iv_image);
mContent = itemView.findViewById(R.id.tv_content);
mPrice = itemView.findViewById(R.id.tv_price);
}
public void setData(Good good) {
//开始设置数据
mContent.setText(good.getContent());
mPrice.setText(good.getPrice());
}
public TextView getTvTitle() {
return mContent;
}
public TextView getTvCount() {
return mPrice;
}
public ImageView getIvGoods() {
return mImage;
}
}
// //实现瀑布流效果
// private void showStagger() {
// //准备布局管理器
// StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
// //设置布局管理器的方向
// layoutManager.setReverseLayout(false);
// //设置布局管理器到RecyclerView中
// mList.setLayoutManager(layoutManager);
//
// //创造适配器
// StaggerAdapter adapter = new StaggerAdapter(mData);
// //设置适配器
// mList.setAdapter(adapter);
//
// }
}

@ -6,6 +6,7 @@ import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Parcelable;
import android.util.Log;
@ -16,6 +17,7 @@ import android.widget.TextView;
import com.android.R;
import com.android.activity.adapter.GoodsAdapter;
import com.android.activity.adapter.StaggerAdapter;
import com.android.bean.Good;
import com.android.utils.eventBus.EventMsg;
@ -42,7 +44,8 @@ public class GoodsTypeFragment extends Fragment {
private EventMsg<Good> eventMsg;
private ArrayList<Good> typeGoodList;
private RecyclerView recyclerView;
private GoodsAdapter goodsAdapter;
private StaggerAdapter goodsAdapter;
// private GoodsAdapter goodsAdapter;
View rootView;
@ -97,8 +100,18 @@ public class GoodsTypeFragment extends Fragment {
recyclerView = rootView.findViewById(R.id.rl_goods_show);
if (goodArrayList != null){
goodsAdapter = new GoodsAdapter(typeGoodList,getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
Log.d("TAG", "initView");
goodsAdapter = new StaggerAdapter(typeGoodList,getContext());
// goodsAdapter = new GoodsAdapter(typeGoodList,getContext());
// recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
// recyclerView.setAdapter(goodsAdapter);
//准备布局管理器
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
//设置布局管理器的方向
layoutManager.setReverseLayout(false);
//设置布局管理器到RecyclerView中
recyclerView.setLayoutManager(layoutManager);
//设置适配器
recyclerView.setAdapter(goodsAdapter);
}

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="280dp"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="15dp"
android:background="@color/white">
<ImageView
android:id="@+id/iv_image"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tv_content"
android:layout_width="180dp"
android:layout_height="25dp"
android:layout_below="@+id/iv_image"
android:layout_marginStart="13dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="15dp"
android:textSize="20dp" />
<TextView
android:id="@+id/str"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_content"
android:layout_alignLeft="@+id/tv_content"
android:text="价格: "
android:textColor="@color/red" />
<TextView
android:id="@+id/tv_price"
android:layout_width="100dp"
android:layout_height="18dp"
android:layout_below="@+id/tv_content"
android:layout_toRightOf="@+id/str"
android:textColor="@color/red" />
<!-- <TextView-->
<!-- android:id="@+id/iv_delete"-->
<!-- android:layout_width="30dp"-->
<!-- android:layout_height="30dp"-->
<!-- android:layout_alignEnd="@+id/tv_content"-->
<!-- android:layout_alignBottom="@+id/tv_price"-->
<!-- android:src="@drawable/ic_baseline_close_24" />-->
</RelativeLayout>
</RelativeLayout>

@ -7,4 +7,6 @@
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="grey">#C8C5C5</color>
<color name="red">#DC0707</color>
</resources>
Loading…
Cancel
Save