Android在Recycler视图适配器中

时间:2016-04-25 00:03:19

标签: java android android-layout android-fragments android-recyclerview

我试图在一个回收站视图中膨胀两种不同的布局。这是我的适配器代码。

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;

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<myHome> homeData;
private static final int TYPE_FEATURED = 1;
private static final int TYPE_OTHER = 2;

public HomeAdapter(ArrayList<myHome> myDataset) {
    this.homeData = myDataset;
}

public class FeaturedViewHolder extends RecyclerView.ViewHolder {
    public TextView title;
    public TextView author;

    public FeaturedViewHolder(View itemView) {
        super(itemView);
        this.title = (TextView)itemView.findViewById(R.id.title);
        this.author = (TextView)itemView.findViewById(R.id.recvid_auth);

    }
}

public class otherVidViewHolder extends RecyclerView.ViewHolder {
    public TextView rec_title;
    public TextView rec_author;

    public otherVidViewHolder(View itemView) {
        super(itemView);
        this.rec_title = (TextView)itemView.findViewById(R.id.recvid_title);
        this.rec_author = (TextView)itemView.findViewById(R.id.description);
    }
}

@Override
public int getItemViewType(int position) {
    // Just as an example, return 0 or 2 depending on position
    // Note that unlike in ListView adapters, types don't have to be contiguous
    return (position == 0? TYPE_FEATURED : TYPE_OTHER);
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    RecyclerView.ViewHolder viewHolder;
    LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

    switch (viewType) {
        case TYPE_FEATURED :
            View v1 = inflater.inflate(R.layout.layout_home_view, viewGroup, false);
            viewHolder = new FeaturedViewHolder(v1);
            //return new FeaturedViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_home_view, viewGroup, false));
            break;
        case TYPE_OTHER :
            View v2 = inflater.inflate(R.layout.layout_home_other, viewGroup, false);
            viewHolder = new otherVidViewHolder(v2);
            //return new otherVidViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_home_other, viewGroup, false));
            break;
        default: return null;
    }
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {

        if(viewHolder.getItemViewType() == TYPE_FEATURED) {
            FeaturedViewHolder featuredViewHolder = (FeaturedViewHolder) viewHolder;
            featuredViewHolder.title.setText(homeData.get(i).getTitle());
            featuredViewHolder.author.setText(homeData.get(i).getUser());
        } else {
            otherVidViewHolder otherViewHolder = (otherVidViewHolder) viewHolder;
            otherViewHolder.rec_title.setText(homeData.get(i).getViews());
        }
}

@Override
public int getItemCount() {
    return homeData.size();
}

代码正常运行但它只显示一个布局,另一个布局是layout_home_other没有显示。是否可以显示两种布局?这两个布局也有不同的数据显示。我将如何在getItemCount()中重新调整第二个布局的大小?谢谢你能得到的所有帮助。

这是数据集。      公共课myHome {

String title;
String user;
String views;
String url;
String duration;

public myHome(String title, String user, String views, String url, String duration) {
    this.title = title;
    this.user = user;
    this.views = views;
    this.url = url;
    this.duration = duration;
}

public String getTitle() {
    return title;
}

public String getUser() {
    return user;
}

public String getViews() {
    return views;
}

public String getUrl() {
    return url;
}

public String getDuration() {
    return duration;
}

}

1 个答案:

答案 0 :(得分:2)

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

  // CONSTRUCTOR
  public RecyclerViewAdapter(Context context,"OTHER PARAMETERS) {
    this.context = context;
  }

  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view ;
    switch (viewType){
        case -1:
            view = inflater.inflate(R.layout.LAYOUTONE, null, false);
            return new ExpiredViewHolder(view);
        case 0:
            view = inflater.inflate(R.layout.LAYOUTTWO, null, false);
            return new ActiveViewHolder(view);
    }
  }

  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof ExpiredViewHolder) {
        ((ExpiredViewHolder) holder).bind(tickerList.get(position));
    } else {
        if (holder instanceof ActiveViewHolder) {
            ((ActiveViewHolder) holder).bind(tickerList.get(position));
        }
        else {
            ((FutureViewHolder) holder).bind(tickerList.get(position));
        }
    }
  }

  @Override
  public int getItemViewType(int position) {
    return List.get(position).status;
  }

  @Override
  public int getItemCount() {
    return List.size();
  }

  static class ActiveViewHolder extends RecyclerView.ViewHolder {
    //Your layout components
    public ActiveViewHolder(View view) {
        super(view);
        //Instantiate you layout components fromyout view
    }

    public void bind(Params) {
        //PUT TEXT TO YOUR FIELDS OR COMPONENTS
    }
  }

  class ExpiredViewHolder extends RecyclerView.ViewHolder  {
    public ExpiredViewHolder(View view) {
        super(view);
    }
    public void bind(Params) {
    }
  }

  static class FutureViewHolder extends RecyclerView.ViewHolder  {
    public FutureViewHolder( View view) {
        super(view);
    }

    public  void bind(Params) {
    }
  }
}
public class MainClass{

    RecyclerView mRecyclerView ;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new RecyclerViewAdapter(getActivity(), arrayTicker);
        mRecyclerView.setAdapter(adapter);
        return view;
    }

}