我有这个布局(有静态数据):
这是垂直回收站视图中的水平回收站视图,我使用虚假数据填充此回收站视图,但我应该告诉Web程序员json架构进行此项工作。
这是我的Horizontal Recycler视图适配器:
public class HorizontalRecyclerAdapter extends RecyclerView.Adapter<HorizontalRecyclerAdapter.ViewHolder> {
private Context context;
private ArrayList<SingleItemModel> singleItemModelha;
////////////////////////////////////////////////////////////
//================== constructor ===========
////////////////////////////////////////////////////////////
public HorizontalRecyclerAdapter(Context context, ArrayList<SingleItemModel> singleItemModelha) {
this.context = context;
this.singleItemModelha = singleItemModelha;
}
////////////////////////////////////////////////////////////
//================== View holder class ===========
////////////////////////////////////////////////////////////
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView txtTitle;
private ImageView imgItem;
public ViewHolder(View itemView) {
super(itemView);
txtTitle = (TextView)itemView.findViewById(R.id.tvTitle);
imgItem = (ImageView)itemView.findViewById(R.id.itemImage);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "You are clicked at : " + txtTitle.getText() , Toast.LENGTH_SHORT).show();
}
});
}
}
////////////////////////////////////////////////////////////
//================== on create view holder ===========
////////////////////////////////////////////////////////////
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item_card,parent,false);
return new ViewHolder(view);
}
////////////////////////////////////////////////////////////
//================== on bind view holder ===========
////////////////////////////////////////////////////////////
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.txtTitle.setText(singleItemModelha.get(position).getName());
//glide
}
////////////////////////////////////////////////////////////
//================== get item count ===========
////////////////////////////////////////////////////////////
@Override
public int getItemCount() {
if(singleItemModelha != null){
return singleItemModelha.size();
}else {
return 0;
}
}
}
这是立式回收器适配器:
public class VerticalRecyclerAdapter extends RecyclerView.Adapter<VerticalRecyclerAdapter.ViewHolder> {
private Context context;
private ArrayList<ListRowDataModel> listRowDataModelha;
////////////////////////////////////////////////////////////
//================== Constructor ===========
////////////////////////////////////////////////////////////
public VerticalRecyclerAdapter(Context context, ArrayList<ListRowDataModel> listRowDataModelha) {
this.context = context;
this.listRowDataModelha = listRowDataModelha;
}
////////////////////////////////////////////////////////////
//================== View Holder class ===========
////////////////////////////////////////////////////////////
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView txtTitle;
private Button btnMore;
private RecyclerView recyclerView;
public ViewHolder(View itemView) {
super(itemView);
txtTitle = (TextView)itemView.findViewById(R.id.itemTitle);
btnMore =(Button)itemView.findViewById(R.id.btnMore);
recyclerView = (RecyclerView)itemView.findViewById(R.id.recycler_view_horizontal);
}
}
////////////////////////////////////////////////////////////
//================== on create view holder ===========
////////////////////////////////////////////////////////////
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row,parent,false);
return new ViewHolder(view);
}
////////////////////////////////////////////////////////////
//================== on bind view holder ===========
////////////////////////////////////////////////////////////
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.txtTitle.setText(listRowDataModelha.get(position).getSectionTitle());
holder.btnMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "You clicked at block : " + listRowDataModelha.get(position).getSectionTitle(), Toast.LENGTH_SHORT).show();
}
});
holder.recyclerView.setHasFixedSize(true);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setAdapter(new HorizontalRecyclerAdapter(context,listRowDataModelha.get(position).getSingleItemModelha()));
}
////////////////////////////////////////////////////////////
//================== get item count ===========
////////////////////////////////////////////////////////////
@Override
public int getItemCount() {
if(listRowDataModelha != null){
return listRowDataModelha.size();
}else {
return 0;
}
}
}
这是Recycler视图活动:
public class MainActivity extends AppCompatActivity {
private ArrayList<ListRowDataModel> allSampleData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view_main);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
allSampleData = new ArrayList<ListRowDataModel>();
createDummyData();
recyclerView.setAdapter(new VerticalRecyclerAdapter(MainActivity.this,allSampleData));
}
private void createDummyData() {
for (int i = 1; i <= 10; i++) {
ListRowDataModel dm = new ListRowDataModel();
dm.setSectionTitle("Section " + i);
ArrayList<SingleItemModel> singleItem = new ArrayList<SingleItemModel>();
for (int j = 0; j <= 10; j++) {
singleItem.add(new SingleItemModel("Item " + j, "URL " + j));
}
dm.setSingleItemModelha(singleItem);
allSampleData.add(dm);
}
}
}
感谢所有&lt; 3
答案 0 :(得分:1)
您的JSON结构应该是一个数组数组。
每个数组(节)将包含多个对象(项)。
这样的事情:
[
[{}, {}],
[{}, {}]
]