礼物墙
This commit is contained in:
@@ -0,0 +1,100 @@
|
|||||||
|
package com.xscm.modulemain.activity.user.fragment;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.GridView;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.fragment.app.Fragment;
|
||||||
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
|
||||||
|
import com.xscm.modulemain.activity.user.PageViewModel;
|
||||||
|
import com.xscm.modulemain.adapter.GiftWallAdapter;
|
||||||
|
import com.xscm.modulemain.databinding.FragmentGiftWallBinding;
|
||||||
|
import com.xscm.moduleutil.bean.GiftUserWallBean;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A placeholder fragment containing a simple view.
|
||||||
|
*/
|
||||||
|
public class PlaceholderGiftFragment extends Fragment {
|
||||||
|
|
||||||
|
private static final String ARG_SECTION_NUMBER = "section_number";
|
||||||
|
|
||||||
|
private PageViewModel pageViewModel;
|
||||||
|
private FragmentGiftWallBinding binding;
|
||||||
|
|
||||||
|
private GiftWallAdapter mGiftWallAdapter;
|
||||||
|
|
||||||
|
public static PlaceholderGiftFragment newInstance(int index) {
|
||||||
|
PlaceholderGiftFragment fragment = new PlaceholderGiftFragment();
|
||||||
|
// Bundle bundle = new Bundle();
|
||||||
|
// bundle.putInt(ARG_SECTION_NUMBER, index);
|
||||||
|
// fragment.setArguments(bundle);
|
||||||
|
return fragment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttach(@NonNull @NotNull Context context) {
|
||||||
|
super.onAttach(context);
|
||||||
|
pageViewModel = new ViewModelProvider(this).get(PageViewModel.class);
|
||||||
|
int index = 1;
|
||||||
|
if (getArguments() != null) {
|
||||||
|
index = getArguments().getInt(ARG_SECTION_NUMBER);
|
||||||
|
|
||||||
|
// 获取传递的礼物列表数据
|
||||||
|
List<GiftUserWallBean.GiftWallBean> giftList =
|
||||||
|
(List<GiftUserWallBean.GiftWallBean>) getArguments().getSerializable("gift_list");
|
||||||
|
|
||||||
|
// 将数据设置到 ViewModel
|
||||||
|
if (giftList != null) {
|
||||||
|
if (index == 1){
|
||||||
|
pageViewModel.setLiangDataList(giftList);
|
||||||
|
}else if (index == 2){
|
||||||
|
pageViewModel.setNoLiangDataList(giftList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pageViewModel.setIndex(index);
|
||||||
|
|
||||||
|
mGiftWallAdapter= new GiftWallAdapter(new ArrayList<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View onCreateView(
|
||||||
|
@NonNull LayoutInflater inflater, ViewGroup container,
|
||||||
|
Bundle savedInstanceState) {
|
||||||
|
|
||||||
|
binding = FragmentGiftWallBinding.inflate(inflater, container, false);
|
||||||
|
View root = binding.getRoot();
|
||||||
|
// 设置 GridView
|
||||||
|
GridView gridView = binding.gridView;
|
||||||
|
gridView.setAdapter(mGiftWallAdapter);
|
||||||
|
// 观察数据变化并更新 GridView
|
||||||
|
pageViewModel.getDataList().observe(getViewLifecycleOwner(), dataList -> {
|
||||||
|
if (dataList != null) {
|
||||||
|
mGiftWallAdapter.updateData(dataList);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
super.onDestroyView();
|
||||||
|
binding = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import androidx.fragment.app.Fragment;
|
|||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
import androidx.viewpager2.adapter.FragmentStateAdapter;
|
import androidx.viewpager2.adapter.FragmentStateAdapter;
|
||||||
|
|
||||||
|
import com.xscm.modulemain.activity.user.fragment.PlaceholderGiftFragment;
|
||||||
import com.xscm.moduleutil.bean.GiftUserWallBean;
|
import com.xscm.moduleutil.bean.GiftUserWallBean;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@@ -40,7 +41,7 @@ public class SectionsGifPagerAdapter extends FragmentStateAdapter {
|
|||||||
@Override
|
@Override
|
||||||
public Fragment createFragment(int position) {
|
public Fragment createFragment(int position) {
|
||||||
// 创建 Fragment 实例并传递参数
|
// 创建 Fragment 实例并传递参数
|
||||||
PlaceholderFragment fragment = PlaceholderFragment.newInstance(position + 1);
|
PlaceholderGiftFragment fragment = PlaceholderGiftFragment.newInstance(position + 1);
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
|
|
||||||
// 根据位置传递不同的数据
|
// 根据位置传递不同的数据
|
||||||
|
|||||||
Reference in New Issue
Block a user