简体   繁体   English

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

[英]How to get Layoutmanager state in onBindviewholder so as to update my fields based on layoutmanager

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;
}

the above code is in mainactivity, so in my Adapter how should i get the layoutmanager state so as to update my fields such as adjusting textsize to fit when using gridlayoutmanager and increasing the text size when using linear layoutmanager. 上面的代码处于mainactivity中,因此在我的适配器中,应如何获取layoutmanager状态,以便更新我的字段,例如在使用gridlayoutmanager时调整文本大小以适合其大小,在使用线性layoutmanager时增加文本大小。

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 linearlayoutmanager图像

I want to use if else in onbindviewholder and set field properties based on layoutmanager, is it possible. 我想在onbindviewholder中使用else并根据layoutmanager设置字段属性,是否可能。

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;

}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM