我需要像在可扩展列表视图中那样将数据膨胀到父子关系中,但是在同一张卡片视图中,这设计起来相当复杂。因此,我尝试将数据排列在Hashmap<String,List<object>>
中,成为键值对。但是现在无法理解如何将数据映射到recyclerview适配器。我不想使用SectionedRecyclerViewAdapter或任何第三方库。
输出结果如图所示,但根据地图数据,每个类别下应包含2个类别以及3和4个孩子。
我也尝试过此solution,但没有用。
我的地图数据:
Dry Clean
---------
Punjabi Set
Blazer
Safari suit
Wash & Iron
-----------
Suit (3 pcs set)
Denim-Short
Dhoti
Waist Coat
适配器类别:
public class OrderDetailsAdapter extends RecyclerView.Adapter<OrderDetailsAdapter.MyViewHolder> {
private HashMap<String, List<OrderDetailsModel.CartDatum>> moviesList;
Context mContext;
MyViewHolder holder1;
ProgressDialog progressDialog;
ConnectionDetector cd;
private SparseBooleanArray expandState = new SparseBooleanArray();
HashMap<String, List<OrderDetailsModel.CartDatum>> hashMap;
public OrderDetailsAdapter(HashMap<String, List<OrderDetailsModel.CartDatum>> moviesList, Context mContext) {
this.moviesList = moviesList;
this.mContext = mContext;
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
cd = new ConnectionDetector(mContext);
this.hashMap = moviesList;
for (int i = 0; i < moviesList.size(); i++) {
expandState.append(i, false);
}
}
private ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
animator.setDuration(300);
animator.setInterpolator(new LinearInterpolator());
return animator;
}
private void onClickButton(final LinearLayout expandableLayout, final RelativeLayout buttonLayout, final int i) {
//Simply set View to Gone if not expanded
//Not necessary but I put simple rotation on button layout
if (expandableLayout.getVisibility() == View.VISIBLE) {
createRotateAnimator(buttonLayout, 180f, 0f).start();
expandableLayout.setVisibility(View.GONE);
expandState.put(i, false);
} else {
createRotateAnimator(buttonLayout, 0f, 180f).start();
expandableLayout.setVisibility(View.VISIBLE);
expandState.put(i, true);
}
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tv_productname, tv_packetsize, tv_position;
AppCompatImageView iv_product;
RelativeLayout buttonLayout;
LinearLayout expandableLayout;
TextView tvCatName;
public MyViewHolder(View view) {
super(view);
tvCatName = view.findViewById(R.id.tvCatName);
tv_productname = view.findViewById(R.id.tv_productname);
iv_product = view.findViewById(R.id.iv_product);
tv_packetsize = view.findViewById(R.id.tv_packetsize);
buttonLayout = view.findViewById(R.id.button);
expandableLayout = view.findViewById(R.id.expandableLayout);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_orderdetails, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder1 = holder;
final boolean isExpanded = expandState.get(position);
holder.expandableLayout.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
holder.buttonLayout.setRotation(expandState.get(position) ? 180f : 0f);
holder.buttonLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
onClickButton(holder.expandableLayout, holder.buttonLayout, position);
}
});
for (Map.Entry<String, List<OrderDetailsModel.CartDatum>> entry : hashMap.entrySet()) {
//System.out.println(entry.getKey()+" : "+entry.getValue());
Log.e("VAluess", entry.getKey() + " : " + entry.getValue().size());
holder.tvCatName.setText(entry.getKey());
for (int j = 0; j < entry.getValue().size(); j++) {
holder.tv_productname.setText(entry.getValue().get(j).getDressName());
try {
Picasso.with(mContext)
.load(entry.getValue().get(j).getDressPhoto())
.into(holder.iv_product, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
// holder.progressbar.setVisibility(View.GONE);
}
@Override
public void onError() {
holder.iv_product.setImageResource(R.mipmap.ic_launcher);
}
});
} catch (Exception e) {
}
}
}
}
@Override
public int getItemCount() {
return moviesList.size();
}
}
Hashmap<String,<Objects>>
,但是我的解决方案对我自己来说似乎有点bug,因为我正在适配器类中动态扩展子视图,这当然不是应该的方式,但可以满足您的目的。任何建议或帮助将使代码变得更好...//主要活动类别
public class MainActivity extends AppCompatActivity {
List<Data> listData1 = new ArrayList<>();
List<Data> listData2 = new ArrayList<>();
List<Data> listData3 = new ArrayList<>();
DataAdapter adapter;
MapAdapter mapAdapter;
Context mcontext;
AlphabetIndexFastScrollRecyclerView alphabetIndexFastScrollRecyclerView;
HashMap<String, List<Data>> listHash = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcontext = this;
listData1.add(new Data("Avik", 20.1));
listData1.add(new Data("Arnab", 21.1));
listData1.add(new Data("Anikit", 2));
listData2.add(new Data("Rajjoy", 3));
listData2.add(new Data("Sandipan", 4));
listData2.add(new Data("Arindam", 54));
listData2.add(new Data("Abhishek", 23));
listData3.add(new Data("Raju",23));
listData3.add(new Data("Nagarjuna",21));
listHash.put("Dry Wash", listData1);
listHash.put("Iron", listData2);
alphabetIndexFastScrollRecyclerView = findViewById(R.id.recyclerView);
alphabetIndexFastScrollRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
mapAdapter = new MapAdapter(mcontext, listHash);
alphabetIndexFastScrollRecyclerView.setAdapter(mapAdapter);
}
}