如何在onBindviewholder中获取Layoutmanager状态,以便根据layoutmanager更新我的字段

时间:2017-01-16 05:41:54

标签: java android android-recyclerview

public boolean onOptionsItemSelected(MenuItem item) {
    int id=item.getItemId();
    switch (id){
        case R.id.item_grid:
            layoutManager=new GridLayoutManager(this,2);
            recyclerView.setLayoutManager(layoutManager);break;
        case R.id.item_staggered_grid:
            layoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(layoutManager);
            break;
        case R.id.item_horizontal:

            layoutManager=new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
            recyclerView.setLayoutManager(layoutManager);
            break;
        case R.id.item_vertical:
            layoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,true);
            recyclerView.setLayoutManager(layoutManager);
            break;
    }
    return true;
}

上面的代码是mainactivity,所以在我的Adapter中我应该如何获取layoutmanager状态以更新我的字段,例如调整textsize以适应使用gridlayoutmanager并在使用线性布局管理器时增加文本大小。

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

private List<Nfl> teams;
private Context context;
private Resources rsc;



static SQLiteDatabase db;
CupboardNFLSQLiteOpenHelper dbhelper;

@Override
public NflAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewitem,parent,false);
    return new ViewHolder(v);

}
public NflAdapter(List<Nfl> teams) {
    //this.context= context;
    this.teams = teams;
}

public NflAdapter(Context context,Resources rsc,List<Nfl> teams) {
   this.context=context;
    this.rsc=rsc;
    this.teams = teams;

}

@Override
public void onBindViewHolder(NflAdapter.ViewHolder holder, final int position) {



    final Nfl obj=teams.get(position);
    holder.teamname.setText(obj.getTeamName());
    holder.teamid.setText("# "+position);
  //holder.teamimage.setImageResource(obj.getImage_id());

   holder.teamimage.setImageBitmap(ImageGenie.decodeSampledBitmapFromResource(rsc, obj.getImage_id(),300,300));
    holder.removeteam_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("hello","clicked button and position is:"+position);
            teams.remove(position);
            dbhelper = new CupboardNFLSQLiteOpenHelper(context);
            db = dbhelper.getWritableDatabase();
            cupboard().withDatabase(db).delete(obj);
            notifyDataSetChanged();



        }
    });




}

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

public static class ViewHolder extends RecyclerView.ViewHolder{
    TextView teamname,teamid;
    ImageView teamimage;
    Button removeteam_button;

    public ViewHolder(View itemView) {
        super(itemView);
        teamname=(TextView)itemView.findViewById(R.id.teamname_tv);
        teamid=(TextView)itemView.findViewById(R.id.teamid_tv);
        teamimage=(ImageView)itemView.findViewById(R.id.team_iv);
        removeteam_button=(Button)itemView.findViewById(R.id.remove_bt);

    }
}

}

Grid layoutmanager image

linearlayoutmanager image

我想在onbindviewholder中使用if else,并基于layoutmanager设置字段属性,是否可能。

1 个答案:

答案 0 :(得分:0)

public boolean onOptionsItemSelected(MenuItem item) {
    int id=item.getItemId();
    switch (id){
        case R.id.item_grid:

            youradpater.updateType("Grid");
            recyclerView.setLayoutManager(null);
            recyclerView.setLayoutManager(new GridLayoutManager(this,2));
            recyclerView.setAdapter(null);
            recyclerView.setAdapter(youradpater);


        case R.id.item_staggered_grid:


            youradpater.updateType("Stagggered");
            recyclerView.setLayoutManager(null);
            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
            recyclerView.setAdapter(null);
            recyclerView.setAdapter(youradpater);


            break;
        case R.id.item_horizontal:
            youradpater.updateType("Horizontal");
            layoutManager=new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);

            recyclerView.setLayoutManager(null);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(null);
            recyclerView.setAdapter(youradpater);
            break;
        case R.id.item_vertical:

            youradpater.updateType("vertical");
            layoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,true);
            recyclerView.setLayoutManager(null);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(null);
            recyclerView.setAdapter(youradpater);
            break;
    }
    return true;
}




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

private List<Nfl> teams;
private Context context;
private Resources rsc;



static SQLiteDatabase db;
CupboardNFLSQLiteOpenHelper dbhelper;


String layouttype="vertical";

@Override
public NflAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewitem,parent,false);
    return new ViewHolder(v);

}
public NflAdapter(List<Nfl> teams) {
    //this.context= context;
    this.teams = teams;
}

public NflAdapter(Context context,Resources rsc,List<Nfl> teams) {
   this.context=context;
    this.rsc=rsc;
    this.teams = teams;

}

@Override
public void onBindViewHolder(NflAdapter.ViewHolder holder, final int position) {

    //conditions can be put here


    if(layouttype.equals("Vertical")){


    }else if(layouttype.equals("Horizontal")){



    }

    // and so on




    final Nfl obj=teams.get(position);
    holder.teamname.setText(obj.getTeamName());
    holder.teamid.setText("# "+position);
  //holder.teamimage.setImageResource(obj.getImage_id());

   holder.teamimage.setImageBitmap(ImageGenie.decodeSampledBitmapFromResource(rsc, obj.getImage_id(),300,300));
    holder.removeteam_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("hello","clicked button and position is:"+position);
            teams.remove(position);
            dbhelper = new CupboardNFLSQLiteOpenHelper(context);
            db = dbhelper.getWritableDatabase();
            cupboard().withDatabase(db).delete(obj);
            notifyDataSetChanged();



        }
    });




}

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

public static class ViewHolder extends RecyclerView.ViewHolder{


    TextView teamname,teamid;
    ImageView teamimage;
    Button removeteam_button;

    public ViewHolder(View itemView) {
        super(itemView);
        teamname=(TextView)itemView.findViewById(R.id.teamname_tv);
        teamid=(TextView)itemView.findViewById(R.id.teamid_tv);
        teamimage=(ImageView)itemView.findViewById(R.id.team_iv);
        removeteam_button=(Button)itemView.findViewById(R.id.remove_bt);

    }
}

void updateType(Sting layouttype){

    this.layouttype=layouttype;

}

}