我在回收站视图中遇到处理多行布局的问题。我希望有4行不同的布局,这个适配器只显示2个布局,并没有显示下一个2.请帮我修复此问题。我在下面添加了适配器代码:
> package com.example.sikandar.recycler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Sikandar on 10/1/2015.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<Datalist> dataList;
private static final int TYPE_holder1 = 1;
private static final int TYPE_OPTION_holder2 = 2;
private static final int TYPE_OPTION_holder3 = 3;
private static final int TYPE_OPTION_holder4 = 4;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public View view;
public ViewHolder(View v) {
super(v);
view = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<Datalist> dataList) {
this.dataList = dataList;
}
@Override
public int getItemViewType(int position) {
switch (position) {
case 0:
return TYPE_holder1;
case 1:
return TYPE_OPTION_holder2;
case 2:
return TYPE_OPTION_holder3;
case 3:
return TYPE_OPTION_holder4;
default:
return 0;
}
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
switch (viewType) {
case TYPE_holder1:
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_viewholder1, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
case TYPE_OPTION_holder2:
View v1 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_viewholder2, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh1 = new ViewHolder(v1);
return vh1;
case TYPE_OPTION_holder3:
View v2 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_viewholder3, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh2 = new ViewHolder(v2);
return vh2;
case TYPE_OPTION_holder4:
View v3 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_viewholder4, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh3 = new ViewHolder(v3);
return vh3;
default:
return null;
}
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
if(holder.getItemViewType()==TYPE_holder1) {
TextView Email = (TextView) holder.view.findViewById(R.id.emailbox);
TextView phone = (TextView) holder.view.findViewById(R.id.phonebox);
TextView mobile = (TextView) holder.view.findViewById(R.id.mobilebox);
TextView buemail = (TextView) holder.view.findViewById(R.id.buemailbox);
TextView Padress = (TextView) holder.view.findViewById(R.id.Paddress);
TextView Caddress = (TextView) holder.view.findViewById(R.id.Caddress);
Email.setText(dataList.get(position).getEmail());
phone.setText(dataList.get(position).getPhone());
mobile.setText(dataList.get(position).getMobile());
buemail.setText(dataList.get(position).getBuemail());
Padress.setText(dataList.get(position).getPadress());
Caddress.setText(dataList.get(position).getCadress());
}
else if (holder.getItemViewType()==TYPE_OPTION_holder2){
TextView enrollment = (TextView) holder.view.findViewById(R.id.enrollbox);
TextView classs = (TextView) holder.view.findViewById(R.id.classbox);
TextView reg = (TextView) holder.view.findViewById(R.id.regbox);
TextView intake = (TextView) holder.view.findViewById(R.id.intakebox);
TextView degdur = (TextView) holder.view.findViewById(R.id.degreebox);
enrollment.setText(dataList.get(position).getEnrollment());
classs.setText(dataList.get(position).getClasss());
reg.setText(dataList.get(position).getReg());
intake.setText(dataList.get(position).getIntake());
degdur.setText(dataList.get(position).getDegDur());
}
else if (holder.getItemViewType()==TYPE_OPTION_holder3){
TextView father = (TextView) holder.view.findViewById(R.id.fatherbox);
father.setText(dataList.get(position).getFatherName());
}
else if (holder.getItemViewType()==TYPE_OPTION_holder4){
TextView comwork = (TextView) holder.view.findViewById(R.id.combox);
comwork.setText(dataList.get(position).getComWork());
}
}
public int getViewTypeCount() {
return 4;
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return dataList.size();
}
}
这是数据主义者
package com.example.sikandar.recycler;
/**
* Created by Sikandar on 10/1/2015.
*/
class Datalist {
String email;
String Mobile;
String Phone;
String DegDur;
String Intake;
String enrollment;
String reg;
String classs;
String buemail;
String Padress;
String Cadress;
String FatherName;
String ComWork;
public Datalist(String DegDur,String Cadress,String Paddress,String enroll,String FatherName,String clas,String mobile,String email,String reg,String intake,String Comwork,String buemail){
this.DegDur=DegDur;
this.Intake=intake;
this.ComWork=Comwork;
this.buemail=buemail;
this.FatherName=FatherName;
this.enrollment=enroll;
this.Padress=Paddress;
this.Cadress=Cadress;
this.classs=clas;
this.Mobile=mobile;
this.email=email;
this.reg=reg;
}
public String getDegDur(){
return DegDur;
}
public String getIntake(){
return Intake;
}
public String getComWork(){
return ComWork;
}
public String getBuemail(){
return buemail;
}
public String getFatherName(){
return FatherName;
}
public String getEnrollment(){
return enrollment;
}
public String getPadress(){
return Padress;
}
public String getCadress(){
return Cadress;
}
public String getClasss(){
return classs;
}
public String getEmail(){
return email;
}
public String getMobile(){
return Mobile;
}
public String getPhone(){
return Phone;
}
public String getReg(){
return reg;
}
}
这是在datalist中添加项目的地方
recyclerView = (RecyclerView) findViewById(R.id.scrollableview);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
email="Sikandar44iqbal@yahoo.com";
Mobile="+923115117085";
Phone="05178601";
DegDur="4 Years";
Intake="2013 Fall";
enrollment="01-134132-180";
reg="3375";
classs="BSCS 5B";
buemail="1234466465@bui.com";
Padress="Houe 03, main college road, F-7/3 Islamabad.";
Cadress="Houe 03, main college road, F-7/3 Islamabad.";
FatherName="Zafar Iqbal";
ComWork="Not Completed!";
ArrayList<Datalist> dataList = new ArrayList<Datalist>();
for (int i = 0; i < 2; i ++ ) {
dataList.add(new Datalist(DegDur,Cadress,Padress,enrollment,FatherName,classs,Mobile,email,reg,Intake,ComWork,buemail));
}
if (simpleRecyclerAdapter == null) {
simpleRecyclerAdapter = new MyAdapter(dataList);
recyclerView.setAdapter(simpleRecyclerAdapter);
}